Conditionals Cheatsheet
Complete Python Conditionals cheatsheet with clear examples for quick revision
Conditionals are used to make decisions in a program based on True / False conditions. Python uses several forms of conditionals:
1
if Statement
Used to execute code only when a condition is True. Note : Condition must evaluate to True or False. Indentation is mandatory in Python.
# if Statement Example
age = 18
if age >= 18: # condition is True
print("You are eligible to vote")
# NOTE -> if condition is False, there will be no output2
if-else Statement
Used when you want two possible outcomes.
# if-else Example
num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even number")
else:
print("Odd number")
# Examples:
# num = 4 -> Output: Even number
# num = 9 -> Output: Odd number
# Note: else runs only when if condition is False3
4
5
Nested Conditions
A condition inside another condition.
Example 1
num = 10
if num > 0:
if num % 2 == 0:
print("Even Number")
else:
print("Odd Number")
else:
print("Negative Number")Example 2
Marks
Attendance
Result
Grade
Scholarship
< 40
Any
Fail
F
No
40 – 59
Any
Pass
C
No
60 – 84
Any
Pass
B
No
≥ 85
< 75
Pass
A
No
≥ 85
≥ 75
Pass
A
Yes
marks = int(input("Enter marks: "))
attendance = int(input("Enter attendance percentage: "))
if marks >= 40: # First condition (pass/fail)
print("Result: Pass")
if marks >= 85: # Nested if-elif-else
if attendance >= 75:
print("Grade: A")
print("Scholarship: Eligible")
else:
print("Grade: A")
print("Scholarship: Not eligible (low attendance)")
elif marks >= 60:
print("Grade: B")
print("Scholarship: Not eligible")
else:
print("Grade: C")
print("Scholarship: Not eligible")
else:
print("Result: Fail")
print("Grade: F")Last updated