Conditionals

Overview

Teaching: 10 min
Exercises: 15 min
Questions
  • How can programs do different things for different data?

Objectives
  • Correctly write programs that use if and else statements and simple Boolean expressions (without logical operators).

  • Trace the execution of unnested conditionals and conditionals inside loops.

Use if statements to control whether or not a block of code is executed.

filesize = 520
if filesize > 500:
    print(filesize, 'is large')

filesize = 34
if filesize > 500:
    print (filesize, 'is large')
520 is large

Conditionals are often used inside loops.

filesizes = [34, 800, 230, 1200, 11.5]
for f in filesizes:
    if f > 500:
        print(f, 'is large')
800 is large
1200 is large

Use else to execute a block of code when an if condition is not true.

filesizes = [34, 800, 230, 1200, 11.5]
for f in filesizes:
    if f > 500:
        print(f, 'is large')
    else:
        print(f, 'is small')
34 is small
800 is large
230 is small
1200 is large
11.5 is small

Use elif to specify additional tests.

filesizes = [34, 800, 230, 1200, 11.5]
for f in filesizes:
    if f > 1000:
        print(f, 'is huge')
    elif f > 500:
        print(f, 'is large')
    else:
        print(f, 'is small')

34 is small
800 is large
230 is small
1200 is huge
11.5 is small

Conditions are tested once, in order.

Compound Relations Using and, or, and Parentheses

Often, you want some combination of things to be true. You can combine relations within a conditional using and and or. Continuing the example above, suppose you have

num_pages = [120,  50,  452,  98,  850]
height = [26, 28, 45, 15, 35]

for i in range(5):
    if num_pages[i] > 300 and height[i] > 30:
        print("Thick heavy book!")
    elif num_pages[i] > 100 and num_pages[i] <= 200 and height[i] <= 30:
        print("Typical size.")
    elif num_pages[i] <= 100 and height <= 30:
        print("Light object.")
    else:
        print("Whoa!  Something is up with the data.  Check it")

Just like with arithmetic, you can and should use parentheses whenever there is possible ambiguity. A good general rule is to always use parentheses when mixing and and or in the same condition. That is, instead of:

if num_pages[i] <= 100 or num_pages[i] >= 200 and height[i] > 30:

write one of these:

if (num_pages[i] <= 100 or num_pages[i] >= 200) and height[i] > 30:
if num_pages[i] <= 100 or (num_pages[i] >= 200 and height[i] > 30):

so it is perfectly clear to a reader (and to Python) what you really mean.

Tracing Execution [SLIDE]

What does this program print?

filesize = 71
if filesize > 50.0:
    filesize = 25.0
elif filesize <= 50.0:
    filesize = 0.0
print(filesize)

Solution

25.0

Key Points

  • Use if statements to control whether or not a block of code is executed.

  • Conditionals are often used inside loops.

  • Use else to execute a block of code when an if condition is not true.

  • Use elif to specify additional tests.

  • Conditions are tested once, in order.

  • Create a table showing variables’ values to trace a program’s execution.