1 of 23

CSE 160 Section 3

Loops and Range

​

CSE 160

CSE 160: Section 3

2 of 23

Logistics

  • How is HW 1 going? Any questions?

​

  • HW1 (due Tuesday, October 20th @ 11:59 pm)
    • Read the autograder messages!

​

  • Practice 2 (due Friday, October 16th @ 11:59 pm)

​

  • Week 3 Exit Ticket (due before you leave today)

​

​

​

​

CSE 160: Section 3

3 of 23

Lecture Review: Loops

CSE 160: Section 3

4 of 23

Loops

  • Any variable name can be used in place of i (for good style, pick a variable name that fits the context)
  • Indentation is a requirement to indicate loop body

for i in [0, 1, 2, 3]:

print(i)

PythonTutor

​

​

CSE 160: Section 3

5 of 23

Loops

  • Avoid redundancy
  • More readable
  • Avoid bugs
  • Can easily make changes in code body,

or number of times loop is repeated

​

​

CSE 160: Section 3

6 of 23

Lecture Review: Range

CSE 160: Section 3

7 of 23

Range

  • range(start, stop, step)
  • range(start, stop)
    • (inclusive, exclusive)
    • Step is assumed to be 1
  • range(stop)
    • (exclusive)
    • Step is assumed to be 1; Start is assumed to be 0

Example: range(0, 6, 2) loops through [0, 2, 4]

Equivalent to: range(0, 5, 2)

Python Tutor

​

​

CSE 160: Section 3

8 of 23

Section Handout Problems

CSE 160: Section 3

9 of 23

Warm-Up Problem

What is the difference between the behavior of the two for loops provided?

​

for i in range(4): for i in [0, 1, 2, 3]: print(i) print(i)

​

​

CSE 160: Section 3

10 of 23

Warm-Up Problem

What is the difference between the behavior of the two for loops provided?

​

for i in range(4): for i in [0, 1, 2, 3]: print(i) print(i)

​

​

There is no difference! Both print

0

1

2

3

range(4) generates 0, 1, 2, 3 automatically (the stop value 4 is not included); the list writes them out by hand.

CSE 160: Section 3

11 of 23

Problem 1: Range()

Fill in each blank with the correct range() call or sequence of numbers.

​

​

Sequence

range() Call

[0, 1, 2, 3]

​

[-4, -3, -2, -1, 0]

​

​

range(0, 10, 2)

​

range(2, 11, 3)

[25, 20, 15, 10, 5, 0]

​

​

range(1000, -100, -100)

CSE 160: Section 3

12 of 23

Problem 1: Range()

Sequence

range() Call

[0, 1, 2, 3]

range(4) or range(0, 4) or range(0, 4, 1)

[-4, -3, -2, -1, 0]

range(-4, 1) or range(-4, 1, 1)

[0, 2, 4, 6, 8]

range(0, 10, 2)

[2, 5, 8]

range(2, 11, 3)

[25, 20, 15, 10, 5, 0]

range(25, -1, -5)

[1000, 900, 800, 700, 600, 500, 400, 300, 200, 100, 0]

range(1000, -100, -100)

CSE 160: Section 3

13 of 23

Problem 2: Four or For

What is the output of the following program?

​

sum = 1

for i in [1, 2, 3, 4]:

sum *= i

print(sum)

​

​

​

CSE 160: Section 3

14 of 23

Problem 2: Four or For

What is the output of the following program?

​

sum = 1

for i in [1, 2, 3, 4]:

sum *= i

print(sum)

​

24

​

CSE 160: Section 3

15 of 23

Problem 3: Skipping Numbers

Write a for loop that will print the result of multiplying 3 by the numbers 8 through 12. Your output should read:

​

24

27

30

33

36

​

CSE 160: Section 3

16 of 23

Problem 3: Skipping Numbers

Write a for loop that will print the result of multiplying 3 by the numbers 8 through 12. Your output should read:

​

24

27

30

33

36

​

​

​

for i in range(8, 13):

print (3 * i)

CSE 160: Section 3

17 of 23

Exit Ticket

Write a program to print the number of people over 30 years old in the list ages.

​

ages = [20, 21, 20, 22, 19, 18, 14, 35]

​

CSE 160: Section 3

18 of 23

Exit Ticket

Write a program to print the number of people over 30 years old in the list ages.

​

ages = [20, 21, 20, 22, 19, 18, 14, 35]

​

# "Over 30" means strictly older than 30 (age > 30). Otherwise (age >= 30), the answer should be 2

​

​

count = 0

for age in ages:

if age > 30:

count += 1

print(count)

​

​

CSE 160: Section 3

19 of 23

Problem 5: Putting It All Together

Given a sequence of numbers called ages, write a program that calculates and prints out the number of people 30 years old and above, the number of people between 20 and 29 years old (inclusive), and the number of people under 20 years old in the given sequence.

​

For example, if the sequence is : ages = [20, 21, 20, 22, 19, 18, 14, 35]

​

Then the output of the program should be:

1 people are 30 years old or older

4 people are between 20 and 29 years old

3 people are younger than 20 years old

​

If there are no ages in the sequence that fall into an age category, the number printed for that category should be 0.

​

CSE 160: Section 3

20 of 23

Problem 5: Putting It All Together

ages = [20, 21, 20, 22, 19, 18, 14, 35]

​

count_30_plus = 0

count_20_to_29 = 0

count_under_20 = 0

​

for age in ages:

if age >= 30:

count_30_plus += 1

elif age >= 20: # age < 30 here

count_20_to_29 += 1

else: # age < 20

count_under_20 += 1

​

print(count_30_plus, "people are 30 years old or older")

print(count_20_to_29, "people are between 20 and 29 years old")

print(count_under_20, "people are younger than 20 years old")

​

​

CSE 160: Section 3

21 of 23

Problem 4: What is the output?

What is the output of the following program?

​

for value in range(3):

print(value + value ** 2)

​

CSE 160: Section 3

22 of 23

Problem 4: What is the output?

What is the output of the following program?

​

for value in range(3):

print(value + value ** 2)

​

​

0

2

6

CSE 160: Section 3

23 of 23

Thanks for Coming!!

CSE 160: Section 3