Python Conditional Statements
What are Conditional Statements?
In real life, we make decisions all the time:
- If it’s raining, take an umbrella.
- If you're hungry, eat something.
- Otherwise, continue your day.
Python also lets us make decisions in our code using conditional statements.
In Python, for conditional statements, we use:
- if – is used check a condition
- elif – is used to check another condition if the first one was false
- else – is used to do specific operations if nothing above is true
1. if Statement
The if statement is used to run a block of code only when a given condition is True.
Syntax
if condition:
# code to run if condition is True
if: The keyword that starts the conditional statement.condition: An expression that evaluates to eitherTrueorFalse.- The colon
:marks the start of the indented code block. - Indented block: Code that will execute only if the condition is true.
Example
x = 10
if x > 5:
print("x is greater than 5")
x is greater than 5
The variable x holds the value 10. When Python checks the condition x > 5, it evaluates to True. As a result, the print() statement inside the if block is executed, and the message is shown in the output.
Flowchart
2. if-else Statement
The if-else statement allows you to define an alternate block of code to run when the condition is False. It provides a clear choice between two paths.
Syntax
if condition:
# code to run if condition is True
else:
# code to run if condition is False
Syntax Explanation
if: Starts the conditional check.condition: An expression that returnsTrueorFalse.:: A colon follows the condition and begins the indented block.- Indented block (under
if): Executes if the condition isTrue. else:: Specifies the alternate block to run if the condition isFalse.- Indented block (under
else): Executes if the condition isFalse.
Example
age = 16
if age >= 18:
print("You can vote.")
else:
print("You cannot vote.")
You cannot vote.
Explanation: The variable age is assigned the value 16. Python checks the condition age >= 18, which evaluates to False. Therefore, the code inside the else block runs and prints You cannot vote..
3. if-elif-else Ladder
Use the if-elif-else structure when you want to test multiple conditions in sequence. Python evaluates each condition in order and executes the block under the first True condition.
Syntax
if condition1:
# code block 1
elif condition2:
# code block 2
elif condition3:
# code block 3
else:
# default code block if none above are true
Syntax Explanation
if: Checks the first condition. If it’sTrue, its block is executed and the rest are skipped.elif: Stands for “else if.” It lets you check more conditions if the previous ones areFalse.else: A fallback that runs if none of theiforelifconditions are true.- Only the first block with a
Truecondition runs; the rest are ignored.
Example
marks = 75
if marks >= 90:
print("Grade: A")
elif marks >= 70:
print("Grade: B")
elif marks >= 50:
print("Grade: C")
else:
print("Grade: F")
Grade: B
Explanation: The value of marks is 75. The first condition (marks >= 90) is false, so Python checks the next one: marks >= 70. This is true, so it prints Grade: B. The rest of the conditions are skipped.
4. Nested if Statements
You can also put one if inside another. This is called nesting.
num = 10
if num > 0:
if num % 2 == 0:
print("Positive even number")
Positive even number
Why? First, num > 0 is true. Then, 10 % 2 == 0 (means even), so the inner print runs.
Important Notes When Using Conditional Statements
- Use colons (
:) afterif,elif, andelse. - Indent the code block under each condition using spaces (usually 4).
- Conditions are usually comparisons:
==,!=,>,<,>=,<=.
Comments
Loading comments...