Overview
Teaching: 5 min Exercises: 10 minQuestions
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.
if
statements to control whether or not a block of code is executed.if
statement (more properly called a conditional statement)
controls whether some block of code is executed or not.for
statement:
if
and ends with a colonmass = 3.54
if mass > 3.0:
print(mass, 'is large')
mass = 2.07
if mass > 3.0:
print (mass, 'is large')
3.54 is large
masses = [3.54, 2.07, 9.22, 1.86, 1.71]
for m in masses:
if mass > 3.0:
print(mass, 'is large')
3.54 is large
9.22 is large
else
to execute a block of code when an if
condition is not true.else
is always attached to if
.if
branch isn’t taken.masses = [3.54, 2.07, 9.22, 1.86, 1.71]
for m in masses:
if mass > 3.0:
print(mass, 'is large')
else:
print(mass, 'is small')
3.54 is large
2.07 is small
9.22 is large
1.86 is small
1.71 is small
elif
to specify additional tests.elif
(short for “else if”) and a condition to specify these.if
.else
(which is the “catch all”).I can also generate more complex conditional statements with boolean operators like and and or, and use comparators like “<”, “>”
masses = [3.54, 2.07, 9.22, 1.86, 1.71]
for m in masses:
if mass > 9.0:
print(mass, 'is HUGE')
elif mass > 3.0:
print(mass, 'is large')
else:
print(mass, 'is small')
3.54 is large
2.07 is small
9.22 is HUGE
1.86 is small
1.71 is small
grade = 85
if grade >= 70:
print('grade is C')
elif grade >= 80:
print('grade is B')
elif grade >= 90:
print('grade is A')
grade is C
velocity = 10.0
if velocity > 20.0:
print('moving too fast')
else:
print('adjusting velocity')
velocity = 50.0
adjusting velocity
velocity = 10.0
for i in range(5): # execute the loop 5 times
print(i, ':', velocity)
if velocity > 20.0:
print('moving too fast')
velocity = velocity - 5.0
else:
print('moving too slow')
velocity = velocity + 10.0
print('final velocity:', velocity)
0 : 10.0
moving too slow
1 : 20.0
moving too slow
2 : 30.0
moving too fast
3 : 25.0
moving too fast
4 : 20.0
moving too slow
final velocity: 30.0
i | velocity |
0 | 10.0 |
20.0 | |
1 | |
30.0 | |
2 | |
25.0 | |
3 | |
20.0 | |
4 | |
30.0 |
print
statement outside the body of the loop
to show the final value of velocity
,
since its value is updated by the last iteration of the loop.Tracing Execution
What does this program print?
pressure = 71.9 if pressure 50.0: pressure = 25.0 elif pressure <= 50.0: pressure = 0.0 print(pressure)
Trimming Values
Fill in the blanks so that this program creates a new list containing zeroes where the original list’s values were negative and ones where the origina list’s values were positive.
original = [-1.5, 0.2, 0.4, 0.0, -1.3, 0.4] result = ____ for value in original: if ____: result.append(0) else: ____ print(result)
[0, 1, 1, 1, 0, 1]
Processing Small Files
Modify this program so that it only processes files with fewer than 50 records.
import glob import pandas for filename in glob.glob('data/*.csv'): contents = pandas.read_csv(filename) ____: print(filename, len(contents))
Initializing
Modify this program so that it finds the largest and smallest values in the list no matter what the range of values originally is.
values = [...some test data...] smallest, largest = None, None for v in values: if ____: smallest, largest = v, v ____: smallest = min(____, v) largest = max(____, v) print(smallest, largest)
What are the advantages and disadvantages of using this method to find the range of the data?
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 anif
condition is not true.Use
elif
to specify additional tests.Conditions are tested once, in order.
Create a table showing updates to variables’ values to trace the execution of a program.