1 of 32

CS 104:

Introduction to Computers and Ethics

For Loops

2 of 32

Announcements & Reminders

  • Homework 3 grades on Blackboard
  • Homework 5 due Friday, Sep 30
    • Can submit up to 3 days late for 10% off per day
  • Google STEP and SWE internship applications open!
    • All you need is a resume + unofficial transcript to apply
    • You should apply, even if you don’t feel you have enough experience yet!
    • More internships listed at cs104.org

3 of 32

Announcements & Reminders

  • Opportunity to get points back on your quiz
    • Quiz reopened Tuesday evening until Friday evening
    • You can re-submit coding questions (not multiple choice), but must include in a comment what you learned that you did not know at the time of the quiz

# I didn’t know what return does, but I have since learned that it does…x, y, z

    • Feel free to ask questions in office hours
    • If you missed the original quiz, cannot resubmit

4 of 32

Announcements & Reminders

    • Darian (Google recruiting partner for Alabama A&M) will be visiting campus on Monday, Oct 3!
    • Office hours w/ Darian from 12:30-3:30 in my office (AJB 320)
      • Drop by to discuss internships, career advice, and more
    • We will also hold an event in the evening at 5pm
      • Please RSVP so we can order food accordingly
      • This event is targeted towards ALL years (not just those applying to STEP)

5 of 32

6 of 32

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

7 of 32

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

8 of 32

for loops

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

for letter in 'cat':

print(letter)

9 of 32

Tracing for loops

for letter in 'cat':

print(letter)

print('done')

'c'

letter

10 of 32

Tracing for loops

for letter in 'cat':

print(letter)

print('done')

'c'

letter

c

11 of 32

Tracing for loops

for letter in 'cat':

print(letter)

print('done')

'a'

letter

c

12 of 32

Tracing for loops

for letter in 'cat':

print(letter)

print('done')

'a'

letter

c

a

13 of 32

Tracing for loops

for letter in 'cat':

print(letter)

print('done')

't'

letter

c

a

14 of 32

Tracing for loops

for letter in 'cat':

print(letter)

print('done')

't'

letter

c

a

t

15 of 32

Tracing for loops

for letter in 'cat':

print(letter)

print('done')

't'

letter

c

a

t

No elements left!

16 of 32

Tracing for loops

for letter in 'cat':

print(letter)

print('done')

't'

letter

c

a

t

done

17 of 32

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

18 of 32

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

19 of 32

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

20 of 32

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!

21 of 32

Tracing for loops

for num in range(3, 5):

print(num)

print('exited loop')

num

22 of 32

Tracing for loops

for num in range(3, 5):

print(num)

print('exited loop')

3

num

23 of 32

Tracing for loops

for num in range(3, 5):

print(num)

print('exited loop')

3

num

3

24 of 32

Tracing for loops

for num in range(3, 5):

print(num)

print('exited loop')

4

num

3

25 of 32

Tracing for loops

for num in range(3, 5):

print(num)

print('exited loop')

4

num

3

4

26 of 32

Tracing for loops

for num in range(3, 5):

print(num)

print('exited loop')

4

num

3

4

Reached 5. Nothing left!

27 of 32

Tracing for loops

for num in range(3, 5):

print(num)

print('exited loop')

4

num

3

4

exited loop

28 of 32

Try it yourself: write a for loop that sums the numbers 1 to 10, and then prints out the answer

*use range!

29 of 32

sum_1_to_10 = 0

for i in range(1, 11):

sum_1_to_10 += i

print(sum_1_to_10)

30 of 32

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

31 of 32

Break

  • break is used to stop and exit loops early

0

1

CONSOLE:

32 of 32

Continue

  • continue is used to skip the current loop iteration and go back to check the loop condition

0

1

3

CONSOLE: