Conditionals in Python
Conditionals allow us to make decisions in code.
We can run different blocks of code depending on whether a condition is True or False.
Why use conditionals?
- Conditionals allow the program to respond differently based on input or situation.
- They are essential for decision-making, validation, and branching logic.
- Widely used in real-world applications where outcomes depend on conditions.
Examples of real-time usage:
- Checking if a user is logged in or not before granting access
- Determining grades based on marks
- Processing different payment methods in an e-commerce app
- Running a program only if certain conditions are met
- Validating form inputs or user commands
We will cover:
ifstatementif-elsestatementif-elif-elseladder- Nested
if - Shorthand
ifexpressions (Ternary operator) - Combining conditions with
and/or
Example: if statement
# ---- Example: if statement ----
# We use "if" to check a condition.
# If the condition is True, the indented block of code runs.
x = 10
if x > 0: # condition: is x greater than 0?
print("x is positive") # this runs because 10 > 0x is positive
Example: if-else
# ---- Example: if-else ----
# Use "if-else" when we want two possible outcomes:
# - if condition is True → run the 'if' block
# - if condition is False → run the 'else' block
num = -5
if num >= 0: # check if number is non-negative
print("Number is non-negative")
else:
print("Number is negative")Number is negative
Example: if-elif-else
# ---- Example: if-elif-else ----
# Use "if-elif-else" when we want to check multiple conditions in sequence.
# Only the FIRST matching condition will run.
score = 85
if score >= 90:
print("Grade: A") # runs if score is 90 or above
elif score >= 75:
print("Grade: B") # runs if score is between 75 and 89
elif score >= 50:
print("Grade: C") # runs if score is between 50 and 74
else:
print("Grade: Fail") # runs if score is below 50Grade: B
Example: nested if
# ---- Example: nested if ----
# A "nested if" means one if-statement inside another.
# Useful when we want to check multiple levels of conditions.
number = 15
if number > 0: # check if positive
print("Number is positive")
if number % 2 == 0: # inside positive check, we check even/odd
print("It is even")
else:
print("It is odd")
else:
print("Number is negative or zero")Number is positive
It is odd
Example: shorthand if (ternary operator)
# ---- Example: shorthand if (ternary operator) ----
# Use this when we want to assign a value based on a condition in ONE line.
age = 20
status = "Adult" if age >= 18 else "Minor"
# if age >= 18 → "Adult", otherwise "Minor"
print("Status:", status)Status: Adult
Example: combining conditions
# ---- Example: combining conditions ----
# Use logical operators:
# - "and" → both conditions must be True
# - "or" → at least one condition must be True
marks = 70
if marks >= 50 and marks <= 100: # both conditions must hold
print("Pass")
else:
print("Fail")
temperature = 35
if temperature < 0 or temperature > 30: # either condition can hold
print("Extreme weather")Pass
Extreme weather
Summary
We learned about conditional statements:
if→ check a single conditionif-else→ two possible outcomesif-elif-else→ multiple conditions, only first match runs- Nested
if→ one condition inside another - Shorthand
if(ternary operator) → compact one-line condition and/or→ combine multiple conditions
Next notebook: 05_loops.ipynb
