coding180 icon
Coding180
The Else Statement: Adding Alternative Actions to Your Code

The Else Statement: Adding Alternative Actions to Your Code

Home > Tutorials > Python for beginners > Python Conditional Looping


The else statement is a powerful tool in programming that allows you to specify alternative actions for your code. This statement can be used in many different situations, from controlling the flow of loops to handling errors and exceptions.

What is the else statement?

The else statement is a keyword in Python that is used in conjunction with conditional statements such as if, elif, and while. It provides an alternative action to execute when the condition of the preceding statement is not satisfied.

Here's an example:

x = 5

if x < 0:
    print("x is negative")
else:
    print("x is non-negative")

In this case, if the value of x is less than zero, the first statement will be executed. Otherwise, the second statement will be executed.

Using the else statement with loops

The else statement can also be used with loops to provide an additional action after the loop has finished executing. For example:

numbers = [1, 2, 3, 4, 5]

for number in numbers:
    print(number)
else:
    print("All done!")

In this case, the loop will iterate through each item in the list numbers and print it to the console. Once the loop is finished, the else statement will be executed, printing "All done!".

Handling exceptions with the else statement

The else statement can also be used in conjunction with try and except blocks to handle exceptions. Here's an example:

try:
    x = int(input("Enter a number: "))
    y = 10 / x
except ZeroDivisionError:
    print("You cannot divide by zero.")
else:
    print("The result is:", y)

In this case, the user is prompted to enter a number. If the number is zero, the except block will be executed and an error message will be printed. Otherwise, the division will be performed, and the result will be printed using the else statement.

Exercises

Now that you understand how the else statement works, here are some exercises to help you practice using it:

  1. Write a program that prompts the user to enter a password. If the password is "12345", print "Access granted". Otherwise, print "Access denied".

  2. Write a program that generates a random number between 1 and 10 and prompts the user to guess the number. If the user guesses correctly, print "You win!". Otherwise, print "You lose.".

  3. Write a program that prompts the user to enter a string. If the string contains the letter "a", print "The string contains the letter a". Otherwise, print "The string does not contain the letter a".

Conclusion

The else statement is a powerful tool in programming that allows you to specify alternative actions for your code. It can be used in many different situations, from controlling the flow of loops to handling errors and exceptions. By practicing these exercises, you'll gain a better understanding of how to use the else statement effectively in your own programs.


user

Robort Gabriel

Lagos, Nigeria

Freelance Web Developer, Native Android Developer, and Coding Tutor.

Skills
  • UI / UX
  • JavaScript
  • Python
  • PHP
  • Kotlin
  • Java
  • Bootrap
  • Android
  • Laravel