1 of 29

CSE 160 Section 2

Loops and Range!

TA1 & TA2

2 of 29

Logistics

  • How was HW0? Everything has been set up?

  • Coding Practice 1 (due Friday January 17)
    • Coding Practice 2 (due Wednesday January 22)

  • HW1 (due Friday January 17) expressions, printing, loops
    • Submitting on Gradescope
    • Wait for autograder!

3 of 29

Hw 1 Tips

  • Watch out for formatting errors - flake8
  • Your output should exactly match the expected output.
    • (blanks should be replaced with your answers)

Diffchecker.com

4 of 29

Lecture Review: Loops

5 of 29

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

6 of 29

Loops

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

or number of times loop is repeated

7 of 29

Lecture Review: Range

8 of 29

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

9 of 29

Kahoot!

Link

10 of 29

Section Handout Problems

11 of 29

Lists, Ranges, and Loops

3. What is the output printed by the following program?

for value in [1, 3, 5]:

print(value + value ** 2)

12 of 29

Lists, Ranges, and Loops

3. What is the output printed by the following program?

for value in [1, 3, 5]:

print(value + value ** 2)

Output:

2

12

30

13 of 29

Lists, Ranges, and Loops

4. Write a for loop that will print the result of multiplying 3 by the numbers 8 through 12.

The example solution is two lines long. Your output should read:

24

27

30

33

36

14 of 29

Lists, Ranges, and Loops

4. Possible solutions appear below:

for value in [8, 9, 10, 11, 12]:

print(3 * value)

for value in range(8, 13):

print(3 * value)

for value in range(24, 37, 3):

print(value)

15 of 29

Nested Loops

5. What is the output printed by the following program?

for i in [1, 2, 3]:

for j in [1, 2, 3]:

print(i + j)

Answer: Python Tutor

16 of 29

Nested Loops

6. What is the output printed by the following program?

sum = 0

for i in [1, 2, 3]:

for j in [1, 2, 3]:

sum = sum + i

print(sum)

Answer: Python Tutor

17 of 29

Nested Loops

8. Write code to print the number of people over 20 years old in the list ages

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

18 of 29

Nested Loops

8. Write code to print the number of people over 20 years old in the list ages

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

count = 0;

for age in ages:

if (age > 20):� count += 1;

print(count)

19 of 29

Mod Operator Overview

20 of 29

Mod

Modulo operator, denoted as %, is a mathematical operation that calculates the remainder when one number is divided by another.

  • 2 % 5

  • 3 % 7

  • 5 % 2

  • 8 % 3

  • 20 % 5

21 of 29

Mod

  • 2 % 5 = 2

  • 3 % 7 = 3

  • 5 % 2 = 1

  • 8 % 3 = 2

  • 20 % 5 = 0

22 of 29

Mod

What is the output of the following code?

for i in range(5):

print(i % 3)

23 of 29

Mod

What is the output of the following code?

for i in range(5):

print(i % 3)

Answer:

0

1

2

0

1

24 of 29

If / Else Overview

25 of 29

If/ Else Structure

  • Checks that condition is True/False, and executing code based on that condition

is_raining = True

is_sprinkling = False

if is_raining:

print(“Bring an umbrella”)

elif is_sprinkling:

print(“Bring a raincoat”)

else:

print(“Bring sunglasses”)

26 of 29

If/ Else Structure

  • Checks that condition is True/False, and executing code based on that condition

is_raining = True

is_sprinkling = False

if is_raining:

print(“Bring an umbrella”)

elif is_sprinkling:

print(“Bring a raincoat”)

else:

print(“Bring sunglasses”)

Output:

Bring an umbrella

27 of 29

Coding Practice 1

28 of 29

# ~~~ Begin Problem 1 ~~~

'''

Given a list of integers, prints the sum of the even ints in the list.

Note: the % "mod" operator computes the remainder, e.g. 5 % 2 is 1.

Assumptions:

nums: a variable containing list of integers.

Prints: a integer representing the sum of the even numbers in the list

Examples:

nums = [5, 2, 6, 3, 4] should print 12

nums = [3, 6, 11, 2, 5] should print 8

nums = [] should print 0

nums = [7] should print 0

nums = [5, 8, 5] should print 8

nums = [72, 2, 6, 1] should print 80

nums = [6] should print 6

nums = [4, 4, 4] should print 12

nums = [1, 1, 1, 2] should print 2

Try copying these values into the nums variable to see if your code works.

'''

nums = []

sum = 0

# Write your code for Problem 1 here!

print(sum)

# ~~~ End Problem 1 ~~~

29 of 29

Section Code

** Note that section codes are only given in-person and are not given through email, Ed, or other mediums.