0% found this document useful (0 votes)
31 views58 pages

Unit3 1

1) The document discusses for loops and while loops in Python. It provides examples of using loops to iterate through lists and examples of updating variables within loops like counters, sums, and averages. 2) Key aspects covered include using an iteration variable to move through a sequence, executing the loop body for each item. Examples demonstrate initializing variables before loops and printing results after. 3) Searching loops are discussed, with an example of setting a flag variable when a target value is found within the loop iteration.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views58 pages

Unit3 1

1) The document discusses for loops and while loops in Python. It provides examples of using loops to iterate through lists and examples of updating variables within loops like counters, sums, and averages. 2) Key aspects covered include using an iteration variable to move through a sequence, executing the loop body for each item. Examples demonstrate initializing variables before loops and printing results after. 3) Searching loops are discussed, with an example of setting a flag variable when a target value is found within the loop iteration.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 58

❑ Remember to increment i, or else the loop will continue forever.

❑ The while loop requires relevant variables to be ready, in this example we


need to define an indexing variable, i, which we set to 1.
❑ Remember to increment i, or else the loop will continue forever.
❑ The while loop requires relevant variables to be ready, in this example we
need to define an indexing variable, i, which we set to 1.
n=5

No Yes Program:
n>0? Output:

print n n=5 5
while n > 0 : 4
print (n) 3
n = n -1
n=n–1 2
print ('Blastoff!’) 1
print (n) Blastoff!
print 'Blastoff' 0
Loops (repeated steps) have iteration variables that
change each time through a loop. Often these
iteration variables go through a sequence of numbers.
n=5

No Yes
n>0?

print 'Lather' n=5


while n > 0 :
print 'Rinse' print ('Lather’)
print ('Rinse’)
print ('Dry off!’)

print 'Dry off!'

What is wrong with this loop?


n=0

No Yes
n>0?

print 'Lather' n=0


while n > 0 :
print 'Rinse' print ('Lather’)
print ('Rinse’)
print ('Dry off!’)

print 'Dry off!'


What does this loop do?
while True: > hello there
line = input( ) hello there
if line == 'done' : > finished
break finished
print (line) > done
print ('Done!’) Done!
while True: > hello there
line = input( ) hello there
if line == 'done' : > finished
break Finished
print (line) > done
print ('Done!’) Done!
while True:
line = input( ) No Yes
True ?
if line == 'done' :
break ....
print (line)
print ('Done!’)
break

...

print 'Done'
while True: > hello there
line = input(‘ ') hello there
if line == '#' : >#
continue >
if line == 'done': > print this
break > print this!
print (line) > done
print ('Done!’) Done!
while True: > hello there
line = input( ) hello there
if line == '#' : >#
continue >
if line == 'done': > print this
break > print this!
print (line) > done
print ('Done!’) Done!
No
True ? Yes
while True:
line = input( ) ....
if line == '#' :
continue
if line == 'done': continue
break
print (line)
...
print ('Done!’)

print 'Done'
5
4
for i in [5, 4, 3, 2, 1] :
3
print (i)
2
print ('Blastoff!’)
1
Blastoff!
friends = ['Joseph', 'Glenn', 'Sally']
for friend in friends : Happy New Year: Joseph
print ('Happy New Year:', friend) Happy New Year: Glenn
print ('Done!’) Happy New Year: Sally
Done!
No
Yes
Done? Move i ahead 5
4
for i in [5, 4, 3, 2, 1] :
3
print i print (i)
2
print ('Blastoff!’)
1
Blastoff!

print 'Blast off!'


Definite loops (for loops) have explicit iteration
variables that change each time through a loop. These
iteration variables move through the sequence or set.
Five-element sequence
Iteration variable

for i in [5, 4, 3, 2, 1] :
print i
No
Yes • The iteration variable “iterates”
Done? Move i ahead though the sequence (ordered
set)
print i
• The block (body) of code is
executed once for each value in
the sequence

• The iteration variable moves


through all of the values in the
for i in [5, 4, 3, 2, 1] sequence
: print (i)
i=5
No print i
Yes
Done? Move i ahead i=4
print i
print i
i=3
print i

i=2
for i in [5, 4, 3, 2, 1] : print i
print (i)
i=1
print i
Set some variables to initial
values
for thing in data:
Look for something or do
something to each entry
separately, updating a
variable.

Look at the variables.


$ python basicloop.py
Before
9
print ('Before’)
41
for thing in [9, 41, 12, 3, 74, 15] :
12
print (thing)
3
print ('After’)
74
15
After
3 41 12 9 74 15

largest_so_far -13 41 74
zork = 0 $ python countloop.py
print ('Before', zork) Before 0
for thing in [9, 41, 12, 3, 74, 15] : 19
zork = zork + 1 2 41
print (zork, thing) 3 12
print ('After', zork) 43
5 74
6 15
After 6
To count how many times we execute a loop we introduce a counter
variable that starts at 0 and we add one to it each time through the loop.
$ python countloop.py
zork = 0 Before 0
print ('Before', zork) 99
for thing in [9, 41, 12, 3, 74, 15] : 50 41
zork = zork + thing 62 12
print (zork, thing) 65 3
print ('After', zork) 139 74
154 15
After 154

To add up a value we encounter in a loop, we introduce a sum variable that


starts at 0 and we add the value to the sum each time through the loop.
count = 0 $ python averageloop.py
sum = 0 Before 0 0
print ('Before', count, sum) 199
for value in [9, 41, 12, 3, 74, 15] : 2 50 41
count = count + 1 3 62 12
sum = sum + value 4 65 3
print (count, sum, value) 5 139 74
print ('After', count, sum, sum / count) 6 154 15
After 6 154 25
An average just combines the counting and sum patterns
and divides when the loop is done.
$ python search1.py
found = False Before False
print ('Before', found) False 9
for value in [9, 41, 12, 3, 74, 15] : False 41
if value == 3 : False 12
found = True True 3
print (found, value) True 74
print ('After', found) True 15
After True
If we just want to search and know if a value was found - we use a variable that starts
at False and is set to True as soon as we find what we are looking for.
9 41 12 3 74 15

smallest_so_far -1
9 41 12 3 74 15

largest_so_far None 9 3

You might also like