Python · control flow

If Statements

Run a block of code only when a condition is true, and choose between branches.

Basic if


        # simple if
        x = 10
        if x > 5:
            print("x is greater than 5")
        # ans: output: x is greater than 5
      
x =

The indented line runs only when the condition is True.

If-else


        # if with else
        age = 18
        if age >= 18:
            print("Adult")
        else:
            print("Minor")
        # ans: output: Adult
      
age =

Exactly one of the two branches runs.

If-elif-else


          # multiple conditions
          score = 85
          if score >= 90:
              print("A")
          elif score >= 80:
              print("B")
          elif score >= 70:
              print("C")
          else:
              print("F")
          # ans: output: B
        
True True True False False False score = 85 score >= 90 score >= 80 score >= 70 print("A") print("B") print("C") print("F")
score =

Conditions are checked top to bottom; the first True one wins and the rest are skipped.

Nested if


        # if inside if
        x = 15
        if x > 10:
            if x < 20:
                print("Between 10 and 20")
        # ans: output: Between 10 and 20
      
x =

The inner if is only checked when the outer one is True.

Multiple elif


        # many elif branches
        num = 3
        if num == 1:
            print("One")
        elif num == 2:
            print("Two")
        elif num == 3:
            print("Three")
        # ans: output: Three
      
num =

With no else, it is possible that nothing runs at all.

Exercises

Predict the output, then press → to trace the code and check.

Exercise 1


        # what is printed?
        x = 5
        if x > 10:
            print("Big")
        else:
            print("Small")
        # ans: Small
      

Exercise 2


        # what about this?
        score = 90
        if score >= 90:
            print("A")
        elif score >= 80:
            print("B")
        # ans: A (only the first matching condition runs)
      

Exercise 3


        # edge case?
        x = 0
        if x:
            print("True")
        else:
            print("False")
        # ans: False (0 is falsy)
      

Exercise 4


        # what prints?
        age = 18
        if age >= 18:
            print("Adult")
        if age < 21:
            print("Not 21 yet")
        # ans: Adult
        # ans: Not 21 yet (two separate ifs: both run)
      

Exercise 5


        # tricky: what happens?
        if True:
            x = 5
        else:
            x = 10
        y = 20
        # ans: x is 5, y is 20 (y = 20 is outside the if)
      

Exercise 6


        # what is result?
        x = 15
        if x > 10:
            if x < 20:
                result = "Between"
            else:
                result = "Big"
        # ans: result is "Between"
      

Exercise 7


        # what prints?
        score = 70
        if score >= 70:
            pass
        else:
            print("Fail")
        # ans: nothing (pass does nothing)
      

Exercise 8


        # condition evaluation?
        x = None
        if x:
            print("Yes")
        else:
            print("No")
        # ans: No (None is falsy)
      

Exercise 9


        # multiple conditions?
        x = 5
        if x > 0 and x < 10:
            print("In range")
        # ans: In range
      

Exercise 10


        # what happens?
        if False:
            print("A")
        elif True:
            print("B")
        elif True:
            print("C")
        # ans: B (stops at the first True)
      

Exercise 11


        # empty list check?
        items = []
        if items:
            print("Has items")
        else:
            print("Empty")
        # ans: Empty (an empty list is falsy)
      

Remember

Indentation is the block

Only the indented lines belong to the if; the next unindented line always runs.

First True wins

In an if / elif chain, Python stops at the first True condition and skips the rest.

Separate ifs are separate

Two if statements in a row are both checked.

Falsy values

0, None and an empty list [] all count as False.

pass

Does nothing: a placeholder for a block that must exist.