Conditional Statements
Conditional statements let your program choose between different code paths based on conditions. The basic structure is: if condition: followed by an inden...
if, elif, and else
Conditional statements let your program choose between different code paths based on conditions. The basic structure is: if condition: followed by an indented block of code. An optional elif (else if) clause handles additional conditions. An optional else clause handles the case where none of the conditions are True. Python evaluates conditions top-to-bottom and executes only the first matching block. Indentation (4 spaces by convention) defines which lines belong to each block.
Writing Conditions
Conditions are any expression that evaluates to a boolean. Common conditions use comparison operators: if age >= 18:, if score != 0:. You can combine conditions with logical operators: if x > 0 and x < 100:, if name == "Alice" or name == "Bob":. Membership testing with in is also very natural: if "admin" in user_roles:. The condition does not need to be boolean — any truthy or falsy value is valid.
Nested Conditionals
You can place if statements inside other if statements to handle more complex logic. For example, first check if a user is logged in, and if so, check whether they are an admin. However, deep nesting makes code hard to read. A common refactoring technique is to use guard clauses: return early from a function if a condition is not met, which reduces nesting and makes the happy path clearer. Python's elif chain is preferable to many levels of nested if-else.
Ternary (Conditional) Expression
Python supports a one-line conditional expression: value_if_true if condition else value_if_false. Example: label = "adult" if age >= 18 else "minor". This is the Python ternary operator and is useful for simple conditional assignments. Avoid using it for complex conditions — multi-line if-else is clearer. Note the order: the true value comes first, then the condition, then the false value, which is the reverse of some other languages.
Worked Example
Classify a score as grade A, B, C, D, or F: score = int(input("Score: ")); if score >= 90: grade = "A"; elif score >= 80: grade = "B"; elif score >= 70: grade = "C"; elif score >= 60: grade = "D"; else: grade = "F"; print(f"Grade: {grade}"). Note that elif chains are evaluated in order, so placing the highest threshold first means we only reach lower ones if the score did not qualify for a higher grade.
Exam Tip
Conditional statement exams often test indentation errors. In Python, every if, elif, and else must have at least one statement in its block. The pass keyword is used as a placeholder when you want an empty block. A common question: what happens if two elif conditions both evaluate to True? Only the first one executes — Python does not check remaining conditions after finding a match. Another common trap: using = (assignment) instead of == (comparison) inside a condition will raise a SyntaxError in Python, unlike in C or JavaScript.