A conditional lets your program make decisions — executing different code depending on whether a condition is True or False.
Indentation is the block. Python uses 4-space indentation (not curly braces) to mark what belongs inside an if. Wrong indentation = bug or SyntaxError.
| Operator | Meaning | Example | Result |
|---|---|---|---|
== | equal to | 5 == 5 | True |
!= | not equal | 5 != 3 | True |
> / < | greater / less | 7 > 3 | True |
>= / <= | greater-or-equal / less-or-equal | 5 >= 5 | True |
and | both must be True | True and False | False |
or | at least one True | True or False | True |
not | inverts Boolean | not True | False |
Common mistake: = assigns a value. == compares. Writing if x = 5: is a SyntaxError in Python (C programmers watch out).
score = int(input("Enter score: "))print(f"Grade: {grade}")score >= 60 first, a score of 95 would get "D" and never reach the "A" check. Always go most-restrictive first.Output: 2024 is a leap year
2000 is divisible by 400 (leap). 1900 is divisible by 100 but not 400 (not leap). 2024 is divisible by 4 but not 100 (leap).
Write an if/else that prints "odd" or "even" for a number n.
% gives the remainder of division. Even numbers have remainder 0 when divided by 2.For a number n: print "FizzBuzz" if divisible by both 3 and 5, "Fizz" if only by 3, "Buzz" if only by 5, otherwise print the number.
BMI is weight(kg) / height(m)^2. Print: "Underweight" (<18.5), "Normal" (18.5-24.9), "Overweight" (25-29.9), "Obese" (>=30).
What does elif mean?
or, not elif.break does inside loops.elif = 'else if'. It only runs when the preceding if (and any earlier elifs) were False.
elif only executes when every condition above it was False AND its own condition is True.
break exits loops. elif is a conditional branch.
What is the output of: x = 7; print('big' if x > 5 else 'small')?
The ternary form is val_if_true if condition else val_if_false. Since 7 > 5 is True, output is 'big'.
Python supports one-line conditionals: a if cond else b. No error here.
The condition (7>5) is True, but the expression returns the string 'big', not the boolean.
Which operator checks equality without assigning?
x = 5 stores 5 in x. x == 5 asks 'is x equal to 5?' Use == for comparison.
is tests if two variables point to the exact same object in memory. For value comparison use ==.
Python comparison operators are: ==, !=, <, >, <=, >=. There is no standalone 'eq' operator.
What does not (True and False) evaluate to?
Step by step: True and False = False. not False = True.
Boolean expressions always return True or False. not(False) = True.
Parentheses are valid here: not (True and False) first evaluates the inner expression, then applies not.
If score = 85, which branch runs in the grade classifier?
The A branch requires score >= 90. 85 < 90, so Python moves to the next elif.
elif chains stop at the first True condition. 85 >= 80 is True, so 'B' is assigned and the elif for C is skipped.
elif is not like a switch - once one branch matches, all remaining elif/else branches are skipped.