Conditional statements allow your program to make decisions based on whether a certain condition is true or false. This is a fundamental concept for creating dynamic and responsive programs. Python uses the if, elif (else if), and else keywords for this purpose.
The if Statement
The if statement is the simplest form of a conditional. It executes a block of code only if its condition is True.
Syntax:
pythonif condition: # Code to execute if condition is True statement_1 statement_2
Example:
pythonage = 20 if age >= 18: print("You are old enough to vote.") # Output: # You are old enough to vote.
The if-else Statement
The if-else statement allows you to execute one block of code if the condition is True, and a different block if the condition is False.
Syntax:
pythonif condition: # Code to execute if condition is True else: # Code to execute if condition is False
Example:
pythontemperature = 15 if temperature > 25: print("It's a hot day!") else: print("It's not too hot today.") # Output: # It's not too hot today.
The if-elif-else Statement
When you have multiple conditions to check, the if-elif-else chain is used. Python evaluates the conditions in order from top to bottom. The first condition that evaluates to True will have its corresponding block executed, and the rest of the elif and else blocks will be skipped.
Syntax:
pythonif condition1: # Code to execute if condition1 is True elif condition2: # Code to execute if condition1 is False AND condition2 is True elif condition3: # Code to execute if condition1, condition2 are False AND condition3 is True else: # Code to execute if all conditions above are False
Example:
pythonscore = 85 if score >= 90: print("Grade: A") elif score >= 80: print("Grade: B") elif score >= 70: print("Grade: C") elif score >= 60: print("Grade: D") else: print("Grade: F") # Output: # Grade: B
Important Considerations:
-
Indentation: Remember that indentation is crucial in Python. The code blocks under
if,elif, andelsemust be consistently indented (usually 4 spaces). -
Boolean Context: The conditions in
ifstatements are evaluated in a Boolean context. Any non-zero number, non-empty string, list, or dictionary is consideredTrue.0,None, empty strings (""), empty lists ([]), and empty dictionaries ({}) are consideredFalse.python# Example of Boolean context name = "" if name: print("Name is not empty") else: print("Name is empty") # Output: Name is empty count = 0 if count: print("Count is not zero") else: print("Count is zero") # Output: Count is zero
Conditional statements are the backbone of decision-making in your programs. Mastering if, elif, and else allows your code to respond intelligently to different inputs and scenarios.
Key Takeaways:
ifexecutes code if a condition isTrue.elseprovides an alternative block for when theifcondition isFalse.elifallows checking multiple conditions sequentially.- Indentation defines code blocks for conditionals.
- Conditions are evaluated in a Boolean context; non-empty/non-zero values are
True, empty/zero values areFalse.