Conditional statements in Python allow the program to make decisions based on certain conditions. The if
, elif
, and else
statements control the flow of execution by checking whether a condition is True
or False
.
1. The if
Statement
The if
statement is used to execute a block of code if a 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.
- The condition
age >= 18
isTrue
, so the indented block of code is executed.
2. The if-else
Statement
The if-else
statement adds an alternative block of code that runs when the condition is False
.
Syntax
if condition:
# Code to execute if condition is True
else:
# Code to execute 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.
- Since
age
is16
, theif
condition isFalse
, so theelse
block is executed.
3. The if-elif-else
Statement
When multiple conditions need to be checked, elif
(short for “else if”) is used.
Syntax
if condition1:
# Code to execute if condition1 is True
elif condition2:
# Code to execute if condition2 is True
else:
# Code to execute if none of the conditions are True
Example
marks = 85
if marks >= 90:
print("Grade: A")
elif marks >= 75:
print("Grade: B")
elif marks >= 60:
print("Grade: C")
else:
print("Grade: F")
Output:
Grade: B
- The first condition
marks >= 90
isFalse
. - The second condition
marks >= 75
isTrue
, so"Grade: B"
is printed.
4. Nested if
Statements
An if
statement can be placed inside another if
statement to check multiple conditions.
Syntax
if condition1:
if condition2:
# Code to execute if both conditions are True
Example
num = 10
if num > 0:
print("Positive number")
if num % 2 == 0:
print("Even number")
Output:
Positive number
Even number
- The first
if
conditionnum > 0
isTrue
, so execution continues inside. - The nested
if
conditionnum % 2 == 0
is alsoTrue
, so"Even number"
is printed.
5. Short-Hand if
and if-else
(Ternary Operator)
Single-Line if
Statement
x = 10
if x > 5: print("x is greater than 5")
Output:
x is greater than 5
Single-Line if-else
(Ternary Operator)
a = 10
b = 20
min_value = a if a < b else b
print("Minimum value:", min_value)
Output:
Minimum value: 10
6. Using Logical Operators in if
Statements
Logical operators (and
, or
, not
) can be used inside if
statements to combine multiple conditions.
Example with and
age = 25
income = 50000
if age > 18 and income > 30000:
print("Eligible for loan")
Output:
Eligible for loan
- Both conditions are
True
, so the message is printed.
Example with or
has_passport = True
has_id_card = False
if has_passport or has_id_card:
print("You can travel")
Output:
You can travel
- Since
has_passport
isTrue
, theif
condition isTrue
.
Example with not
is_raining = False
if not is_raining:
print("Go outside and enjoy!")
Output:
Go outside and enjoy!
- The
not
operator reversesFalse
toTrue
, so the block executes.
7. pass
Statement in if
Blocks
The pass
statement is used as a placeholder when no action is required inside an if
block.
Example
num = 10
if num > 5:
pass # Placeholder to avoid syntax errors
print("Code continues...")
Output:
Code continues...
- The
pass
statement prevents an error when anif
block has no content.