Conditions and if Statements
if condition: # Block of code executed if condition is True
Types of Conditions
Conditions in Python are expressions that evaluate to True or False. They are often used in if statements to control the flow of the program. Here’s a breakdown of common types of conditions:
Comparison Operators
Comparison operators are used to compare values. They return True or False based on the result of the comparison.
- Equal to (==): Checks if two values are equal.
x = 10 if x == 10: print("x is 10")
- Not equal to (!=): Checks if two values are not equal.
x = 10 if x != 5: print("x is not 5")
- Greater than (>): Checks if a value is greater than another.
x = 10 if x > 5: print("x is greater than 5")
- Less than (<): Checks if a value is less than another.
x = 10 if x < 20: print("x is less than 20")
- Greater than or equal to (>=): Checks if a value is greater than or equal to another.
x = 10 if x >= 10: print("x is greater than or equal to 10")
- Less than or equal to (<=): Checks if a value is less than or equal to another.
x = 10 if x <= 15: print("x is less than or equal to 15"
Logical Operators
Logical operators combine multiple conditions. They help create more complex conditions.
- and: Returns True if both conditions are True.
x = 10 y = 20 if x > 5 and y < 25: print("Both conditions are true")
- or: Returns True if at least one condition is True.
x = 10 y = 30 if x > 5 or y < 25: print("At least one condition is true")
- not: Returns True if the condition is False.
x = 10 if not x < 5: print("x is not less than 5")
Membership Operators
Membership operators check for membership in a sequence.
- in: Returns True if the value is present in the sequence.
fruits = ["apple", "banana", "cherry"] if "banana" in fruits: print("Banana is in the list")
- not in: Returns True if the value is not present in the sequence.
fruits = ["apple", "banana", "cherry"] if "orange" not in fruits: print("Orange is not in the list")
Identity Operators
Identity operators compare the memory location of two objects.
- is: Returns True if both variables point to the same object.
a = [1, 2, 3] b = a if a is b: print("a and b refer to the same object")
- is not: Returns True if both variables point to different objects.
a = [1, 2, 3] b = [1, 2, 3] if a is not b: print("a and b do not refer to the same object")
Using if Statements for Control Flow
if statements control the flow of your program by executing specific code blocks based on conditions.
Single if Statement
Executes a block of code if the condition is true.
Example:
temperature = 30 if temperature > 25: print("It's hot outside")
if … else Statement
Provides an alternative block of code to execute if the condition is false.
Example:
temperature = 20 if temperature > 25: print("It's hot outside") else: print("It's not hot outside")
if … elif … else Statement
Handles multiple conditions. elif stands for “else if,” allowing for more than two possible blocks of code.
Example:
temperature = 10 if temperature > 25: print("It's hot outside") elif temperature > 15: print("It's warm outside") else: print("It's cold outside")
Common Mistakes with Conditions
Improper Use of = Instead of ==: = is for assignment, not comparison. Use == for comparisons.
Incorrect:
x = 10 if x = 10: # SyntaxError: cannot use assignment here print("x is 10")
Correct:
x = 10 if x == 10: print("x is 10")
Incorrect Indentation: Python relies on indentation to define blocks of code. Incorrect indentation can lead to IndentationError.
Incorrect:
x = 10 if x > 5: print("x is greater than 5") # IndentationError
Correct:
x = 10 if x > 5: print("x is greater than 5")
Using Non-Boolean Expressions as Conditions: Ensure that conditions evaluate to boolean values.
Incorrect:
x = 0 if x: # x is 0, which is False, but this could be confusing in different contexts print("x is non-zero")
Correct:
x = 0 if x != 0: # Clearly states that x should not be zero print("x is non-zero")
Examples of Nested if Statements
Nested if statements are used when you need to evaluate multiple layers of conditions.
Example:
age = 25 income = 50000 if age > 18: if income > 30000: print("You are eligible for the loan") else: print("Income is too low for the loan") else: print("You are too young for the loan")
Using if Statements with Functions
You can use if statements inside functions to control the logic of your program.
Example:
def check_age(age): if age < 0: return "Invalid age" elif age < 18: return "Minor" elif age < 65: return "Adult" else: return "Senior" print(check_age(30))