PAI Lecture 5
PAI Lecture 5
PAI
W
E Loops
[ Play Slide Show to view the animations and listen to the narration.
E Such slides have an Audio icon on the top right corner. ]
K
PROGRAMMING FOR AI (PAI)
Oct 2022
5
Objectives
Official (Closed) - Non Sensitive
Lecture 5
Slide 2
Topics
Official (Closed) - Non Sensitive
while loop
for loop
range()
Nested loop
break
continue
Lecture 5
Slide 3
Recap if-elif-else
Official (Closed) - Non Sensitive
Lecture 5
Slide 4
while loop – 1
Official (Closed) - Non Sensitive
Sequence of execution:
while loop structure Line No. Action
while condition : Output: 1 error set to 20
expression 6.666666666666667 2 condition is True
2.222222222222223 3 error = 6.66…
Repeating expression 0.740740740740740
as long as 4 condition is
prints error
end 2 condition is True
True
3 error = 2.22…
Example: 4 prints error
1: error = 20
Error starts at 20 2: while error > 1 :
2 condition is True
3 error = 0.74…
Divide error by 3 on every iteration 3: error = error / 3
4 prints error
Continue until error no longer > 1 4: print(error)
2 condition is False
5: print('end')
5 prints end
Lecture 5
Slide 5
while loop – 2
Official (Closed) - Non Sensitive
Definition:
for loop structure
for var in seq :
expression
for each var in seq, execute expression
Interpretation:
Lecture 5
Slide 7
for loop – 2
Official (Closed) - Non Sensitive
Recall list:
family_height = [ 1.72, 1.58, 1.69, 1.79 ] Output:
print(family_height) [ 1.72, 1.58, 1.69, 1.79 ]
Lecture 5
Slide 8
for loop – 3
Official (Closed) - Non Sensitive
Lecture 5
Slide 9
range() – 1
Official (Closed) - Non Sensitive
Lecture 5
Slide 10
range() – 2
Official (Closed) - Non Sensitive
range(stop)
range(start,
range(start, stop)
stop)
range(start, stop, step)
range(start, stop, step)
start: Starting number of the sequence
stop: Generate numbers up to, but not including this number
step: Difference between each number
Examples:
for x in range(5) : for x in range(3, 6) : for x in range(3 , 8, 2) :
print(x) print(x) print(x)
Lecture 5
Slide 12
range() – 4
Official (Closed) - Non Sensitive
Use enumerate to loop over a collection while keeping track of the index
in a variable:
family_height = [ 1.72, 1.58, 1.69, 1.79 ] Output:
index 0: 1.72
for index, height in enumerate(family_height) index 1: 1.58
print(f'index {index}: {height}') index 2: 1.69
index 3: 1.79
Lecture 5
Slide 14
Loop over string
Official (Closed) - Non Sensitive
Lecture 5
Slide 15
Nested Loop
Official (Closed) - Non Sensitive
for i in range(3):
outer loop
for j in range(7):
print('=', end='')
inner loop
print()
Lecture 5
Slide 17
Nested Loop – Example 2
Official (Closed) - Non Sensitive
Lecture 5
Slide 18
break statement
Official (Closed) - Non Sensitive
break # Example 2
To exit a loop completely when a var = 10
condition is True while var > 0 :
print('Current variable value: ', var)
Resumes execution at next statement
var = var – 1
after loop
if var == 5 :
Examples: # Example 1 break
for letter in 'Python' : print('Good bye!')
if letter == 'h' :
Output:
break Current variable value: 10
print('Current letter: ', letter) Current variable value: 9
Current variable value: 8
Output:
Current variable value: 7
Current letter: P
Current variable value: 6
Current letter: y
Good bye! Lecture 5
Current letter: t Slide 19
continue statement
Official (Closed) - Non Sensitive
# Example 2
var = 10
continue while var > 0 :
var = var – 1
To skip all remaining statements in loop
if var == 5 :
and start next iteration continue
Control goes to beginning of loop print('Current variable value: ', var)
print('Good bye!‘)
# Example 1
for letter in 'Python' : Output:
if letter == 'h' : Current variable value: 10
Current variable value: 9
continue Current variable value: 8
print('Current letter: ', letter) Output:
Current variable value: 7
Current letter: P
Current variable value: 6
Current letter: y
Current variable value: 4
Current letter: t
Current variable value: 3
Current letter: o
Current variable value: 2
Current letter: n
Current variable value: 1 Lecture 5
Good bye! Slide 20
Summary
Official (Closed) - Non Sensitive
while loop
a loop that executes the statements as long as the condition is True
for loop
a loop used for iterating over values in a sequence eg. list, etc.
range()
to specify a range of values that can be used in a loop
Nested loop
Outer loop that has a inner loop
break
to skip the rest of the statements in the loop and terminate the loop
continue
to skip the rest of the statements in the loop and continue from the beginning of the loop
Lecture 5
Slide 21