A conditional statement lets the program decide what to do based on a condition. A condition is any expression that evaluates to True or False.
| Operator | Meaning |
|---|---|
== != | Equal · Not equal |
> < >= <= | Standard comparisons |
and | True only if both sides True |
or | True if any side True |
not | Reverses True ↔ False |
A condition need not be a comparison — any value can be tested. Python treats the following as False and almost everything else as True:
| Falsy | Truthy |
|---|---|
| 0, 0.0 | any non-zero number |
| "" (empty string) | any non-empty string |
| None, False | True |
if marks >= 33: reads as "is marks at least 33?" — if the answer is yes, run the block.= instead of ==. Single = assigns; double == compares. x = 5 stores 5; x == 5 asks "is x equal to 5?".if StatementThe simplest conditional. Run a block only if a condition is True. If False, skip the block.
if condition: statement1 statement2 next_line # always runs
Three things to notice:
age = int(input("Age: ")) if age >= 18: print("You can vote.") print("Thank you.")If age = 20 → both lines print. If age = 15 → only "Thank you." prints.
amount = 1500 if amount > 1000: discount = amount * 0.10 print("Discount:", discount)Output:
Discount: 150.0The whole indented block is skipped. Execution jumps to the next line at the outer indent. The program does not stop; it just continues without running the if-block.
total = 320 shipping = 40 if total > 500: shipping = 0 print("Ship:", shipping)For total = 320 the if-block is skipped;
shipping stays 40. Output: Ship: 40and / or / not to combine more checks: if marks >= 33 and attendance >= 75: — passes only if both are true.if-else StatementUse if-else when you have two paths — one for True, one for False. Exactly one block runs, never both.
if condition: # True path statement_T else: # False path statement_F
n = int(input("N: ")) if n % 2 == 0: print(n, "is Even") else: print(n, "is Odd")
% is the remainder operator. If n / 2 remainder is 0, n is even.a, b = 12, 30 if a > b: print(a, "is larger") else: print(b, "is larger")Output:
30 is largerelse instead of two ifs?Writing two opposite ifs tests the condition twice and risks mistakes if you change one. A single if-else tests once and guarantees exactly one path runs.
age = 17 if age >= 18: print("Eligible") else: print("Wait", 18 - age, "yr")Output:
Wait 1 yrelse never has a condition. Don't write else condition: — it is just else:. The else covers all the cases not matched by the if.if-elif-else StatementUse this when you have more than two paths. Python checks each condition top-to-bottom, runs the first True block, and skips all the rest. If none match, the final else runs.
if condition_1: block_1 elif condition_2: block_2 elif condition_3: block_3 else: block_default
elif is short for "else if". Chain as many as you need.
p = 67 if p >= 90: g = "A" elif p >= 75: g = "B" elif p >= 60: g = "C" elif p >= 33: g = "D" else: g = "F" print(g)For p = 67: 90 fails → 75 fails → 60 passes → g = "C". The remaining branches are skipped.
p >= 33 at the top, every passing student would get "D".elif vs separate ifsThree separate ifs test their conditions independently — multiple blocks may run. An if-elif-else ladder runs at most one block. Pick elif when the conditions are mutually exclusive.
| Pattern | Branches that may run |
|---|---|
Three separate ifs | 0, 1, 2, or all 3 |
if-elif-elif-else | Exactly one |
ifAn if statement inside another if (or inside else / elif). Used when a second decision depends on the first.
if outer: if inner: block_A # both True else: block_B # outer True, inner False else: block_C # outer False
user = "vivek" pwd = "abc123" if user == "vivek": if pwd == "abc123": print("Login successful") else: print("Wrong password") else: print("User not found")Three different messages depending on the two conditions.
andMany nested ifs can be replaced by a single if using and. The flat form is shorter and easier to read.
# nested if x > 0: if x < 100: print("valid") # flat (preferred) if 0 < x < 100: print("valid")Python supports chained comparisons like
0 < x < 100, which is a built-in shortcut for x > 0 and x < 100.ifs become hard to read. Try to keep nesting at most 2 levels deep — flatten with and/or or use an elif ladder when possible.Python lets you put a small if-else on a single line. Useful when assigning one of two values based on a condition.
value = result_T if condition else result_F
Read it left-to-right: take result_T if condition is True, otherwise result_F.
a, b = 12, 30 big = a if a > b else b print(big) # 30
marks = 28 print("Pass" if marks >= 33 else "Fail")Output:
Failn = -3 print("sign:", "+" if n >= 0 else "-")Output:
sign: -if only for short value choices. Don't squeeze big multi-line logic into one line — it becomes unreadable.| Form | Use when… |
|---|---|
if | One thing to do, only when condition holds |
if-else | Exactly two paths, choose one |
if-elif-else | Many paths, only one runs |
Nested if | Second decision depends on the first |
One-line if | Pick one of two values quickly |
if/elif/else; using = instead of ==; wrong indentation; else with a condition; two else blocks for one if.if condition is False?
x = 5 if x % 2 == 0: print("A") else: print("B")
else blocks can one if have?
if marks >= 33 and attendance >= 75: prints "Pass" only when:
if / elif / else?
a = 4 and b = 4, the value of not (a == b) is:
if-elif-else ladder, how many of its blocks run during one execution?
a = 10 if a > 5: print("Big") print("Done")Output:
x = 0 if x: print("Yes") else: print("No")Output:
m = 72 if m >= 90: g = "A" elif m >= 60: g = "B" elif m >= 33: g = "C" else: g = "F" print(g)Output:
a, b = 5, 8 print(a if a > b else b)Output:
n = 7 if n > 0 and n % 2 != 0: print("positive odd") else: print("other")Output:
s = "" if s: print("non-empty") else: print("empty")Output:
y = 15 if y % 3 == 0: if y % 5 == 0: print("FizzBuzz") else: print("Fizz") else: print("-")Output:
a = 4 if a == 4: print("P", end="") if a > 0: print("Q")Output:
Each snippet has exactly one error. Mark the line number and write the corrected line.
1 age = 18 2 if age = 18: 3 print("adult")Wrong line: Correction:
1 marks = 50 2 if marks > 40 3 print("Pass")Wrong line: Correction:
1 n = 10 2 if n > 0: 3 print("positive")Wrong line: Correction:
1 x = 5 2 if x > 0: 3 print("+ve") 4 elseif x < 0: 5 print("-ve")Wrong line: Correction:
1 a, b = 3, 7 2 big = a if a > b b 3 print(big)Wrong line: Correction:
1 age = 20 2 if age >= 18: 3 print("vote") 4 else age < 18: 5 print("wait")Wrong line: Correction:
= and == with one example each.
if-elif-else and a sequence of separate if statements?
if as a single if using and:if x > 0: if x < 100: print("in range")
if-else into a one-line conditional expression:if n >= 0: sign = "+" else: sign = "-"
Each program should read input from the user and produce a clear output. Use a separate sheet if you need more space.
if / else — no built-in max().
'A' <= ch <= 'Z'.