For Loops
Jake Shoudy
Sept 16, 2022
CSCI 110 - Lecture 13
Announcements
Project 1: Part 3
Due Sunday 9/18 11:59PM
Using functions to improve our program!
Last chance to get help at office hours is today
HW4
Due 2 days ago…
Last day to turn in is Saturday at 11:59pm (using late days)
HW5
Posted last night
For loops and simple lists (will learn Monday)
Due 9/22 (Thursday) because it is posted a day late
STEP Application Party!
Exam 1 Scores
| Before Partial | Final Grade |
Mean | 70.1% | TBD (>74) |
Min | 10% | |
Max | 100% | |
Median | 74% | |
Percent of class above 70% | 55% | |
Percent of class above 90% | 30% | |
Google 20% TAs
Didn’t like your exam 1 score? Just plain lost?
Some folks from Google are volunteering their time to help you with your computer science scoolwork!
If you feel that you are falling behind and want more 1:1 attention let me know and I will try to pair you with someone who can help!
For Loops - in
Why Loops?
Would you rather type and read this:
print(1) print(5) print(9)
print(2) print(6) print(10)
print(3) print(7) print(11)
print(4) print(8) print(12)
Or this:
Print from 1 to 12
Cases for Loops
Walk my dog 100 steps
Repeat a task a certain number of times
Cases for Loops
Walk my dog as many steps as it needs until it poops
Repeat task until a condition is satisfied
Types of Loops
Repeat task a certain number of times
Repeat task until a condition is satisfied
Strings and while loops
Iterate over, or traverse, characters in a string using a loop
name = 'cat'
index = 0
while index < len(name):
print(name[index])
index = index + 1
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')
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
index = 0
while index < len(word):
print(word[index])
index = index + 1
print('All done!')
for letter in word:
print(letter)
print('All done!')
Accumulator Pattern
word = 'friends'
result = ''
for char in word:
result = result + char + '-'
print(result)
Accumulator Pattern
word = 'friends'
result = ''
for char in word:
result = result + char + '-'
print(result)
Accumulator Pattern
word = 'friends'
result = ''
for char in word:
result = result + char + '-'
print(result)
2. iterate over the string
Accumulator Pattern
word = 'friends'
result = ''
for char in word:
result = result + char + '-'
print(result)
3. update the result
Accumulator Pattern
word = 'friends'
result = ''
for char in word:
result = result + char + '-'
print(result)
f-r-i-e-n-d-s-
in operator
different than for x in ‘my_string’
in operator: checks if value exists in sequence, evaluates to boolean
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 = 'CS110'
char in word
For Loops - range
Baking Cookies
def bake_cookies():
print('Baked one batch of cookies!')
Baking Cookies
# Bake 400 batches of cookies.
count = 0
while count < 400:
bake_cookies()
count = count + 1
Baking Cookies
# Bake 400 batches of cookies.
for i in ‘01234567890123456789…’:
bake_cookies()
Baking Cookies
# Bake 400 batches of cookies.
for i in range(0, 400):
bake_cookies()
range()
range(): returns sequence from start to end, incrementing by step (3rd parameter)
Use range() with for loops to repeat a loop when need numbers
range(5, 8)
range(0, 5)
range(0, 5, 2)
range(5)
5 6 7
0 1 2 3 4
0 2 4
0 1 2 3 4
index = 0
while index < 10:
print(index)
index = index + 1
print('All done!')
for i in range(0, 10):
print(i)
print('All done!')
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
for … in ...
for … in range(...)
for [variable] in range(...):
[code]
for [variable] in [sequence]:
[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
Let’s Code!
https://replit.com/team/csci110-01
For Loops
Review: For Loop
What is it?
Control structure that runs block of code repeatedly by iterating over elements in sequence
for [variable] in [sequence]:
[execute code]
Why is it important?
Write code once and run many times - do not need to copy and paste same code over and over
For vs While
for runs code predetermined / known number of times
while runs until certain condition is no longer met
Questions?