Lecture
The else Statement for When if Condition is False
The else statement is used with the if statement in conditional logic to define a code block that executes when the if condition is false.
Structure of if, else Statement
if condition1: # Code to execute if condition1 is true else: # Code to execute if all conditions are false
How is the else Statement Used?
The else statement follows an if statement and does not include a condition.
Always add a colon : at the end of the else code line to signify the block of code that the else statement applies to.
Example of else Statement
number = 3 # Check if number is divisible by 2 if number % 2 == 0: # Executes if number is divisible by 2 print("It is even.") else: # Executes if number is not divisible by 2 print("It is odd.")
Previous lessonControlling Program Flow with if StatementsNext lessonExecuting When One of Multiple Conditions is Met Using the elif Statement
Lessons in this chapter Β· Control Logical Flow with if, else, and elif
- 1. Controlling Program Flow with if Statements
- 2. The else Statement for When if Condition is False
- 3. Executing When One of Multiple Conditions is Met Using the elif Statement
- 4. Multiple-choice quiz
- 5. Which values are always considered False?
- 6. The `pass` Keyword That Does Nothing
- 7. Indicating Unimplemented Features with NotImplementedError
- 8. Coding Quiz - Python Temperature Converter
- 9. Multiple-choice quiz
- 10. Fill-in-the-blank quiz
Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help