Mastering Python Conditional Statements: If, Elif, Else Explained
Conditional statements allow a program to make decisions based on certain conditions. They help in controlling the flow of execution by running specific blocks of code only if the conditions are met.

1. if Statement
Description:
if
statement executes a block of code only if the condition is True.
Syntax:
if condition:
# Code to execute if condition is True
Example 1.
temperature = 20
if temperature > 20:
print("outside! too hot")
Example 2
age=18
if age>=18
print("you are allow to eligible for vote in election")
2. else Statement
Description :
if a specified condition evaluates to True, the program executes one block of code; if the condition is False, it executes an alternate block instead, allowing the program to make decisions based on different scenarios.
Syntax:
if condition:
# Code if condition is True
else:
# Code if condition is False
Example 1:
age=16
if age>=18:
print("you are eligible for vote!")
else:
print("you are under 18, not eligible for vote")
Example 2:
temperature = 25
if temperature > 30:
print("It's hot outside!")
else:
print("It's a pleasant day.")
3. if-elif-else Statement:
Description:
The if-elif-else
structure in Python enables the program to evaluate multiple conditions one after another. It starts by checking the first if
condition—if it’s False, it moves to the next elif
condition, and so on. If none of the conditions are True, the else
block runs as a fallback, making it ideal for handling several possible outcomes in a clear and organized way.
Syntax:
if condition1:
# Code if condition1 is True
elif condition2:
# Code if condition2 is True
else:
# Code if none of the conditions are True
Example:
age=20
if age<13:
print("you are child")
elif age<18:
print("you are tenager")
else:
print("you are adult")
4.Nested if Statement:
An if
statement inside another if creates complex logic.
Syntax:
if condition1:
if condition2:
# Code executes if both conditions are True
Example :
num=20
if num>=0:
print("given number is positive")
if num%2=0
print("given number is even")
else
print("given number is odd")
else
print("number is zero or negative")