Index
Conditional statements in Python are used to execute different blocks of code based on certain conditions. The key conditional statements are:
if
if-else
if-elif-else
- Nested
if
The if
Statement
The if
statement executes a block of code only if the condition is True
.
Syntax
if condition:
# Code to execute if condition is True
Example
age = 18
if age >= 18:
print("You are eligible to vote.") # Output: You are eligible to vote.
✅ Key Points:
- The condition inside
if
must be True for the block to execute. - Indentation is mandatory in Python (4 spaces or a tab).
The if-else
Statement
If the condition is False
, the else
block is executed.
Syntax
if condition:
# Code if condition is True
else:
# Code if condition is False
Example
age = 16
if age >= 18:
print("You are eligible to vote.")
else:
print("You are NOT eligible to vote.") # Output: You are NOT eligible to vote.
✅ Key Points:
- If the
if
condition is False, the code insideelse
runs. else
does not require a condition (it is the default case).
The if-elif-else
Statement
The elif
(short for else if) is used to check multiple conditions.
Syntax
if condition1:
# Code if condition1 is True
elif condition2:
# Code if condition2 is True
else:
# Code if none of the above conditions are True
Example
marks = 75
if marks >= 90:
print("Grade: A")
elif marks >= 80:
print("Grade: B")
elif marks >= 70:
print("Grade: C") # Output: Grade: C
else:
print("Grade: F")
✅ Key Points:
- The
if
condition is checked first. - If
if
isFalse
, the program checkselif
conditions one by one. - If none of the conditions are
True
, theelse
block executes.
Nested if
Statements
You can place one if
statement inside another.
Example
age = 20
has_id = True
if age >= 18:
if has_id:
print("You can enter the club.") # Output: You can enter the club.
else:
print("You need an ID to enter.")
else:
print("You are too young to enter.")
✅ Key Points:
- Inner
if
conditions are checked only if the outerif
isTrue
. - Useful when multiple conditions must be met.
The if
Statement with Logical Operators
You can combine multiple conditions using logical operators:
and
→ Both conditions must beTrue
.or
→ At least one condition must beTrue
.not
→ Reverses the condition.
Example Using and
age = 20
has_license = True
if age >= 18 and has_license:
print("You can drive.") # Output: You can drive.
else:
print("You cannot drive.")
Example Using or
has_passport = False
has_national_id = True
if has_passport or has_national_id:
print("You can travel.") # Output: You can travel.
else:
print("You cannot travel.")
Example Using not
is_raining = False
if not is_raining:
print("You can go outside.") # Output: You can go outside.
✅ Key Points:
and
requires both conditions to beTrue
.or
requires at least one condition to beTrue
.not
reverses the condition (True
→False
,False
→True
).
Short-Hand (Ternary Operator
)
Python allows short-hand one-line if-else
statements
Syntax
variable = value_if_true if condition else value_if_false
Example
age = 20
status = "Adult" if age >= 18 else "Minor"
print(status) # Output: Adult
✅ Best for simple conditions.
pass
Statement in if
Blocks
The pass
statement is used when an if
block is required but should do nothing.
Example
age = 18
if age >= 18:
pass # Placeholder, avoids syntax error
else:
print("You are too young.")
✅ Use pass
as a placeholder when writing incomplete code.
Summary Table
Statement | Description |
---|---|
if | Executes code if condition is True |
if-else | Runs one block for True , another for False |
if-elif-else | Checks multiple conditions |
Nested if | if inside another if |
Logical Operators | and , or , not for multiple conditions |
Ternary Operator | Short if-else in one line |
pass Statement | Placeholder inside an if block |