If...else in Python
If...Else Statements in Python
The if...else statement is a fundamental part of control flow in Python, allowing you to execute code based on certain conditions. It helps you make decisions in your code by evaluating whether an expression is True or False.
Syntax of if...else Statements
If Statement:
Theifstatement checks whether a condition isTrue. If it is, the block of code inside theifstatement runs.
if condition: # Code to execute if condition is TrueElse Statement:
Theelsestatement is optional and runs when theifcondition isFalse.
if condition: # Code to execute if condition is Trueelse: # Code to execute if condition is FalseElif Statement:
Theelif(else if) statement is also optional and checks another condition if theifcondition isFalse.
if condition1: # Code to execute if condition1 is Trueelif condition2: # Code to execute if condition1 is False and condition2 is Trueelse: # Code to execute if both conditions are FalseExamples
1. Basic If-Else
age = 18if age >= 18: print("You are an adult.")else: print("You are a minor.")Output:
You are an adult.In this example, the condition age >= 18 is True, so the first block of code is executed.
2. If-Elif-Else
x = 15if x < 10: print("x is less than 10.")elif x == 10: print("x is equal to 10.")else: print("x is greater than 10.")Output:
x is greater than 10.In this case, the condition x < 10 is False, so it moves to the elif statement and checks if x == 10. Since both conditions are False, it finally executes the else block.
3. Nested If-Else
You can also nest if statements within other if statements.
x = 20y = 10if x > 15: if y < 15: print("x is greater than 15 and y is less than 15.") else: print("x is greater than 15 but y is not less than 15.")else: print("x is not greater than 15.")Output:
x is greater than 15 and y is less than 15.In this example, since x > 15 is True, the program enters the nested if block and checks the condition y < 15. Because y is 10, the condition is True, so it executes the corresponding block.
4. Multiple Conditions with and and or
You can combine multiple conditions using logical operators like and, or, and not.
and: Both conditions must beTrue.or: At least one condition must beTrue.
x = 10y = 20if x > 5 and y < 25: print("Both conditions are True.")else: print("At least one condition is False.")Output:
Both conditions are True.In this case, both x > 5 and y < 25 are True, so the if block is executed.
Comparison Operators in If Statements
Commonly used comparison operators in if statements include:
==: Equal to!=: Not equal to>: Greater than<: Less than>=: Greater than or equal to<=: Less than or equal to
Example:
a = 10b = 20if a != b: print("a and b are not equal.")else: print("a and b are equal.")Output:
a and b are not equal.Conclusion
The if...else structure is essential for controlling the flow of a program based on conditions. It allows your code to make decisions and execute different blocks of code based on whether certain conditions are true or false. You can combine multiple conditions with elif and logical operators to handle more complex scenarios.
Let me know if you have any more questions or need further examples!