1 of 51

For Loops

Jake Shoudy

Sept 16, 2022

CSCI 110 - Lecture 13

2 of 51

Announcements

3 of 51

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

4 of 51

HW4

Due 2 days ago…

Last day to turn in is Saturday at 11:59pm (using late days)

5 of 51

HW5

Posted last night

For loops and simple lists (will learn Monday)

Due 9/22 (Thursday) because it is posted a day late

6 of 51

STEP Application Party!

  • STEP application opens 9/19
  • Darian will be on campus!
  • 9/20
  • Jubilee Hall: Appleton Room
  • 5-7pm
  • RSVP
  • Book OH with Darian
    • Already full :(
    • PLEASE go if you signed up

7 of 51

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%

8 of 51

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!

9 of 51

For Loops - in

10 of 51

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

11 of 51

Cases for Loops

Walk my dog 100 steps

Repeat a task a certain number of times

12 of 51

Cases for Loops

Walk my dog as many steps as it needs until it poops

Repeat task until a condition is satisfied

13 of 51

Types of Loops

Repeat task a certain number of times

Repeat task until a condition is satisfied

14 of 51

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

15 of 51

for loops

for loop: control structure that runs block of code repeatedly by iterating over elements in sequence

for letter in 'cat':

print(letter)

16 of 51

Tracing for loops

for letter in 'cat':

print(letter)

print('done')

letter

17 of 51

Tracing for loops

for letter in 'cat':

print(letter)

print('done')

'c'

letter

18 of 51

Tracing for loops

for letter in 'cat':

print(letter)

print('done')

'c'

letter

c

19 of 51

Tracing for loops

for letter in 'cat':

print(letter)

print('done')

'a'

letter

c

20 of 51

Tracing for loops

for letter in 'cat':

print(letter)

print('done')

'a'

letter

c

a

21 of 51

Tracing for loops

for letter in 'cat':

print(letter)

print('done')

't'

letter

c

a

22 of 51

Tracing for loops

for letter in 'cat':

print(letter)

print('done')

't'

letter

c

a

t

23 of 51

Tracing for loops

for letter in 'cat':

print(letter)

print('done')

't'

letter

c

a

t

No elements left!

24 of 51

Tracing for loops

for letter in 'cat':

print(letter)

print('done')

't'

letter

c

a

t

done

25 of 51

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

26 of 51

index = 0

while index < len(word):

print(word[index])

index = index + 1

print('All done!')

for letter in word:

print(letter)

print('All done!')

27 of 51

Accumulator Pattern

word = 'friends'

result = ''

for char in word:

result = result + char + '-'

print(result)

28 of 51

Accumulator Pattern

word = 'friends'

result = ''

for char in word:

result = result + char + '-'

print(result)

  1. initialize the accumulator

29 of 51

Accumulator Pattern

word = 'friends'

result = ''

for char in word:

result = result + char + '-'

print(result)

2. iterate over the string

30 of 51

Accumulator Pattern

word = 'friends'

result = ''

for char in word:

result = result + char + '-'

print(result)

3. update the result

31 of 51

Accumulator Pattern

word = 'friends'

result = ''

for char in word:

result = result + char + '-'

print(result)

f-r-i-e-n-d-s-

32 of 51

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')

33 of 51

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

34 of 51

For Loops - range

35 of 51

Baking Cookies

def bake_cookies():

print('Baked one batch of cookies!')

36 of 51

Baking Cookies

# Bake 400 batches of cookies.

count = 0

while count < 400:

bake_cookies()

count = count + 1

37 of 51

Baking Cookies

# Bake 400 batches of cookies.

for i in ‘01234567890123456789…’:

bake_cookies()

38 of 51

Baking Cookies

# Bake 400 batches of cookies.

for i in range(0, 400):

bake_cookies()

39 of 51

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

40 of 51

index = 0

while index < 10:

print(index)

index = index + 1

print('All done!')

for i in range(0, 10):

print(i)

print('All done!')

41 of 51

Tracing for loops

for num in range(3, 5):

print(num)

print('exited loop')

num

42 of 51

Tracing for loops

for num in range(3, 5):

print(num)

print('exited loop')

3

num

43 of 51

Tracing for loops

for num in range(3, 5):

print(num)

print('exited loop')

3

num

3

44 of 51

Tracing for loops

for num in range(3, 5):

print(num)

print('exited loop')

4

num

3

45 of 51

Tracing for loops

for num in range(3, 5):

print(num)

print('exited loop')

4

num

3

4

46 of 51

Tracing for loops

for num in range(3, 5):

print(num)

print('exited loop')

4

num

3

4

Reached 5. Nothing left!

47 of 51

Tracing for loops

for num in range(3, 5):

print(num)

print('exited loop')

4

num

3

4

exited loop

48 of 51

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

49 of 51

Let’s Code!

50 of 51

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

51 of 51

Questions?