CS 104:
Introduction to Computers and Ethics
For Loops
Announcements & Reminders
Announcements & Reminders
# I didn’t know what return does, but I have since learned that it does…x, y, z
Announcements & Reminders
in operator
in operator: checks if value exists in sequence
in expression evaluates to True if value exists
word = 'banana'
if 'n' in word:
print('Found an n')
else:
print('Did not find a n')
Practice: in operator
'n' in 'Nashville, TN'
'N' in 'Nashville, TN'
True
False
True
False
char = ' '
char in 'Intro to Computer Science'
word = 'CS104'
char in word
for loops
for loop: control structure that runs block of code repeatedly by iterating over elements in sequence
for letter in 'cat':
print(letter)
Tracing for loops
for letter in 'cat':
print(letter)
print('done')
'c'
letter
Tracing for loops
for letter in 'cat':
print(letter)
print('done')
'c'
letter
c
Tracing for loops
for letter in 'cat':
print(letter)
print('done')
'a'
letter
c
Tracing for loops
for letter in 'cat':
print(letter)
print('done')
'a'
letter
c
a
Tracing for loops
for letter in 'cat':
print(letter)
print('done')
't'
letter
c
a
Tracing for loops
for letter in 'cat':
print(letter)
print('done')
't'
letter
c
a
t
Tracing for loops
for letter in 'cat':
print(letter)
print('done')
't'
letter
c
a
t
No elements left!
Tracing for loops
for letter in 'cat':
print(letter)
print('done')
't'
letter
c
a
t
done
for vs while
Similarity: repeating code in a loop
Difference:
while loop goes until condition is no longer met
for loop goes for a predetermined / known number of times
for vs while
for letter in word:
print(letter)
print('All done!')
index = 0
while index < len(word):
print(word[index])
index = index + 1
print('All done!')
range()
range(): returns sequence from start to end (not including end)
Use range() with for loops to repeat a loop when need numbers
range(5, 8)
range(0, 5)
range(5)
5 6 7
0 1 2 3 4
0 1 2 3 4
range()
range(): returns sequence from start to end (not including end)
Use range() with for loops to repeat a loop when need numbers
i = 0
while i < 10:
print(i)
i = i + 1
for i in range(0, 10):
print(i)
these are equivalent!
Tracing for loops
for num in range(3, 5):
print(num)
print('exited loop')
num
Tracing for loops
for num in range(3, 5):
print(num)
print('exited loop')
3
num
Tracing for loops
for num in range(3, 5):
print(num)
print('exited loop')
3
num
3
Tracing for loops
for num in range(3, 5):
print(num)
print('exited loop')
4
num
3
Tracing for loops
for num in range(3, 5):
print(num)
print('exited loop')
4
num
3
4
Tracing for loops
for num in range(3, 5):
print(num)
print('exited loop')
4
num
3
4
Reached 5. Nothing left!
Tracing for loops
for num in range(3, 5):
print(num)
print('exited loop')
4
num
3
4
exited loop
Try it yourself: write a for loop that sums the numbers 1 to 10, and then prints out the answer
*use range!
sum_1_to_10 = 0
for i in range(1, 11):
sum_1_to_10 += i
print(sum_1_to_10)
for … in ...
for … in range(...)
for [variable] in [sequence]:
[code]
for [variable] in range(...):
[code]
Iterating over a sequence, like a string
for letter in 'dog'
letter will be each character in 'dog'
Iterating over numbers
for number in range(10)
number will be a number from 0 to 10
Break
0
1
CONSOLE:
Continue
0
1
3
CONSOLE: