Introduction To CS and Programming in Python, Lecture 3 - Iteration - Mit6 - 100l - f22 - Lec03
Introduction To CS and Programming in Python, Lecture 3 - Iteration - Mit6 - 100l - f22 - Lec03
1
LAST LECTURE RECAP
6.100L Lecture 3
BRANCHING RECAP
if <condition>: if <condition>: if <condition>:
< code > < code > < code >
< code > < code > < code >
... ... ...
elif <condition>: elif <condition>:
if <condition>: < code > < code >
< code > < code > < code >
< code > ... ...
... elif <condition>: else:
else: < code > < code >
< code > < code > < code >
< code > ... ...
...
6.100L Lecture 3
If you keep going right, you are
stuck in the same spot forever
To exit, take a chance and go
Zelda, Lost Woods tricks you the opposite way
© Nintendo. All rights reserved. This content is excluded from our Creative
Commons license. For more information, see https://ocw.mit.edu/help/faq-fair-use/
if <exit right>:
<set background to woods_background>
if <exit right>:
<set background to woods_background>
if <exit right>:
<set background to woods_background>
and so on and on and on...
else:
<set background to exit_background>
else:
<set background to exit_background>
else:
<set background to exit_background>
4
6.100L Lecture 3
If you keep going right, you are
stuck in the same spot forever
To exit, take a chance and go
Zelda, Lost Woods tricks you the opposite way
© Nintendo. All rights reserved. This content is excluded from our Creative
Commons license. For more information, see https://ocw.mit.edu/help/faq-fair-use/
while <exit_right>:
<set background to woods_background>
<ask user which way to go>
<set background to exit_background>
6.100L Lecture 3
while LOOPS
6.100L Lecture 3
BINGE ALL EPISODES OF ONE SHOW
6.100L Lecture 3
CONTROL FLOW: while LOOPS
while <condition>:
<code>
<code>
...
<condition> evaluates to a Boolean
If <condition> is True, execute all the steps inside the
while code block
Check <condition> again
Repeat until <condition> is False
If <condition> is never False, then will loop forever!!
8
6.100L Lecture 3
while LOOP EXAMPLE
"left"
PROGRAM:
where = input("You're in the Lost Forest. Go left or right? ")
while where == "right":
where = input("You're in the Lost Forest. Go left or right? ")
print("You got out of the Lost Forest!")
6.100L Lecture 3
YOU TRY IT!
What is printed when you type "RIGHT"?
10
6.100L Lecture 3
while LOOP EXAMPLE
11
6.100L Lecture 3
while LOOP EXAMPLE
To terminate:
Hit CTRL-c or CMD-c in the shell
Click the red square in the shell
12
6.100L Lecture 3
YOU TRY IT!
Run this code and stop the infinite loop in your IDE
while True:
print("noooooo")
13
6.100L Lecture 3
BIG IDEA
while loops can repeat
code inside indefinitely!
Sometimes they need your intervention to end the program.
14
6.100L Lecture 3
YOU TRY IT!
Expand this code to show a sad face when the user entered the
while loop more than 2 times.
Hint: use a variable as a counter
where = input("Go left or right? ")
while where == "right":
where = input("Go left or right? ")
print("You got out!")
15
6.100L Lecture 3
CONTROL FLOW: while LOOPS
n = 0
while n < 5:
print(n)
n = n+1
16
6.100L Lecture 3
A COMMON PATTERN
Find 4!
i is our loop variable
factorial keeps track of the product
x = 4
i = 1
factorial = 1
while i <= x:
factorial *= i
i += 1
print(f'{x} factorial is {factorial}')
6.100L Lecture 3
for LOOPS
18
6.100L Lecture 3
ARE YOU STILL WATCHING?
Netflix while falling asleep
(it plays only 4 episodes if
you’re not paying attention)
6.100L Lecture 3
CONTROL FLOW:
while and for LOOPS
6.100L Lecture 3
STRUCTURE of for LOOPS
6.100L Lecture 3
A COMMON SEQUENCE of VALUES
for n in range(5):
print(n)
6.100L Lecture 3
A COMMON SEQUENCE of VALUES
2
for n in range(5): 3
print(n)
4
6.100L Lecture 3
range
24
6.100L Lecture 3
YOU TRY IT!
What do these print?
for i in range(1,4,1):
print(i)
for j in range(1,4,2):
print(j*2)
for me in range(4,0,-1):
print("$"*me)
25
6.100L Lecture 3
RUNNING SUM
mysum = 0 i 0
for i in range(10):
mysum += i
print(mysum)
mysum 0
26
6.100L Lecture 3
RUNNING SUM
mysum = 0 i 0
for i in range(10): 1
mysum += i
print(mysum)
mysum 0
1
27
6.100L Lecture 3
RUNNING SUM
mysum = 0 i 0
for i in range(10): 1
mysum += i 2
print(mysum)
mysum 1
3
28
6.100L Lecture 3
RUNNING SUM
mysum = 0 i 0
for i in range(10): 1
mysum += i 2
print(mysum) 3
mysum 3
6
29
6.100L Lecture 3
RUNNING SUM
mysum = 0 i 0
for i in range(10): 1
mysum += i 2
print(mysum)
…
3
mysum 36
45
30
6.100L Lecture 3
YOU TRY IT!
Fix this code to use variables start and end in the range, to get
the total sum between and including those values.
For example, if start=3 and end=5 then the sum should be 12.
mysum = 0
start = ??
end = ??
for i in range(start, end):
mysum += i
print(mysum)
31
6.100L Lecture 3
for LOOPS and range
x = 4
factorial = 1
for i in range(1, x+1, 1):
factorial *= i
print(f'{x} factorial is {factorial}')
32
6.100L Lecture 3
BIG IDEA
for loops only repeat
for however long the
sequence is
The loop variables takes on these values in order.
33
6.100L Lecture 3
SUMMARY
Looping mechanisms
while and for loops
Lots of syntax today, be sure to get lots of practice!
While loops
Loop as long as a condition is true
Need to make sure you don’t enter an infinite loop
For loops
Can loop over ranges of numbers
Can loop over elements of a string
Will soon see many other things are easy to loop over
34
6.100L Lecture 3
MITOpenCourseWare
https://ocw.mit.edu
35