Execution Control
Execution Control
§ Conditional Structures
§ Iteration Patterns, Part I
§ Two-Dimensional Lists
§ while Loop
§ Iteration Patterns, Part II
One-way if statement
if <condition>:
if temp > 86:
<indented code block>
print('It is hot!')
<non-indented statement>
print('Be sure to drink liquids.')
print('Goodbye.')
True
temp > 86:
print('It is hot!')
False
Print('Goodbye.')
Introduction to Computing Using Python
Two-way if statement
if <condition>: if temp > 86:
<indented code block 1> print('It is hot!')
else: print('Be sure to drink liquids.')
<indented code block 2> else:
<non-indented statement> print('It is not hot.')
print('Bring a jacket.')
print('Goodbye.')
False True
temp > 86:
print(‘Goodbye.')
Introduction to Computing Using Python
Multi-way if statement
def temperature(t):
if t > 86: True
print('It is hot') t > 86:
elif t > 32:
print('It is cool')
else: False
print('It is freezing’)
print('Goodbye')
ch5_codes.py True
print('It is hot')
t > 32:
print('It is cool')
False
print('It is freezing')
print('Goodbye')
Introduction to Computing Using Python
Ordering of conditions
What is the wrong with this re-implementation of temperature()?
6
Introduction to Computing Using Python
The Body Mass Index is the value (weight * 703)/height2 . Indexes below 18.5 or
above 25.0 are assessed as underweight and overweight, respectively; indexes in
between are considered normal.
Iteration
• If <sequence> is a list then the items are the objects in the list
word = 'stop'
word = 'desktop'
>>>
stop
word = 'post' desktop
top
word = 'top'
Introduction to Computing Using Python
0 1 2 3 4 5 6 7 8 9
7 24 41 58 75 92
def checkSorted(lst):
'return True if sequence lst is increasing, False otherwise'
for num
i inin
range(len(lst)-1):
range(0,
range(len(lst)):
lst: len(lst)-1):
# compare
i = 0, 1,
num
lst[i]
2,with
...,
with
next
len(lst)-2
lst[i+1]
number in list
#
ifcompare
lst[i] >
lst[i]
<=lst[i+1]:
lst[i+1]:
with lst[i+1]
return
# correctly
Falseordered, continue on
return
# all
else:
adjacent
True pairs are correctly ordered, return true
# incorrectly ordered, return false ch5_codes.py
Let’s practice!
• Jupyter Notebook:
Canvas –> Modules –> Execution Control
15
Introduction to Computing Using Python
19
Introduction to Computing Using Python
>>> factorial(0)
1
>>> factorial(1) def factorial(n):
1 'returns n! for input integer n'
>>> factorial(3) res = 1
6 for i in range(2, n+1):
>>> factorial(6) res *= i
720 return res
ch5_codes.py
Introduction to Computing Using Python
>>> divisors(1)
[1]
>>> divisors(6)
[1, 2, 3, 6]
>>> divisors(11)
[1, 11]
Introduction to Computing Using Python
>>> n = 5
>>> nested(n)
0 1 2 3 4
>>>
def nested(n):
for i
j in range(n):
for i in end='
print(i, range(n):
')
print(i, end=' ’)
')
print()
ch5_codes.py
Introduction to Computing Using Python
>>> n = 5
>>> nested(n)
0 1 2 3 4 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4
>>>
def nested(n):
for j in range(n):
for i in range(n):
print(i, end=' ')
ch5_codes.py
Introduction to Computing Using Python
>>> n = 5
>>> nested(n)
0 1 2 3 4
>>>
0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
def nested(n):
for j in range(n):
for i in range(n):
print(i, end=' ’)
')
print()
ch5_codes.py
Introduction to Computing Using Python
def nested2(n):
for j in range(n):
for i in range(j+1):
range(n):
print(i, end=' ’)
print()
ch5_codes.py
Introduction to Computing Using Python
Two-dimensional lists
def print2D(t):
'prints values in 2D list t as a 2D table' >>> table = [[3, 5, 7, 9],
# for
for row
every
in t:
row of t [0, 2, 1, 6],
# for
for item
every
in row
object in the row [3, 8, 3, 1]]
# print object
print(item, end=' ') >>> print2D(table)
print() 3 5 7 9
ch5_codes.py
0 2 1 6
(Using the iteration loop pattern) 3 8 3 1
>>> incr2D(t)
>>> print2D(t)
def incr2D(t): 4 6 8 10
'increments each number in 2D list t' 1 3 2 7
nrows
# nrows
= =
len(t)
number of rows in t 4 9 4 2
#
ncols
for
ncols
every
= len(t[0])
= number
row index
of columns
i in t >>>
# for every column index j
for i int[i][j]
range(nrows):
+= 1
for j in range(ncols):
t[i][j] += 1 ch5_codes.py
(Using the counter loop pattern)
Introduction to Computing Using Python
>>> lst = [[0, 156, 0, 0], [34, 0, 0, 0], [23, 123, 0, 34]]
>>> pixels(lst)
5
>>> l = [[123, 56, 255], [34, 0, 0], [23, 123, 0], [3, 0, 0]]
>>> pixels(lst)
7
Introduction to Computing Using Python
while loop
if <condition>:
<indented code block>
<non-indented statement>
while <condition>:
<indented code block>
<non-indented statement> True
condition
<non-indented statement>
Introduction to Computing Using Python
while loop
>>> i
i += 7
42 False
i
Introduction to Computing Using Python
Fibonacci sequence
1 1 2 3 5 8 13 21 34 55 . . .
+ + + + + + + +
Goal: the first Fibonnaci number greater than some bound ch5_codes.py
def fibonacci(bound):
'returns the smallest Fibonacci number greater than bound'
bound’
previous = 1 # previous Fibonacci number
current = 1 # current Fibonacci number
while current <= bound:
# current
not donebecomes
yet, make
previous,
currentand
be new
nextcurrent
Fibonacci
is computed
number
previous, current = current, previous+current
return current
Introduction to Computing Using Python
def hello2():
'''a greeting service; it repeatedly requests the name
of the user and then greets the user''’
while True:
name = input('What is your name? ')
print('Hello {}'.format(name))
ch5_codes.py
Introduction to Computing Using Python
Loop-and-a-half pattern
def cities():
lst = []
return lst
ch5_codes.py ch5_codes.py
Introduction to Computing Using Python
Loop-and-a-half pattern
def cities():
lst = []
return lst
ch5_codes.py ch5_codes.py
Introduction to Computing Using Python
Loop-and-a-half pattern
def cities2():
lst = []
lst.append(city) lst.append(city)
return lst
ch5_codes.py
Introduction to Computing Using Python
ch5_codes.py ch5_codes.py
Introduction to Computing Using Python
ch5_codes.py ch5_codes.py
Introduction to Computing Using Python
>>> power(2)
2 4
>>> power(3)
2 4 8
>>> power(10)
2 4 8 16 32 64 128 256 512 1024