Warmup

  • Write a program using a 'while' loop that prints the numbers 1 through 10.

Lesson 2.7

Building on For Loops

Review: Basic for loop

for i in range(5):
    print(i)
  • Runs 5 times
  • i takes values 0 through 4

Using different ranges

for i in range(2, 7):
    print(i)
  • Starts at 2, ends at 6

Using steps in range

for i in range(0, 10, 2):
    print(i)
  • Prints even numbers 0 through 8

For loops with if

for num in range(1, 6):
    if num % 2 == 0:
        print(num, "is even")
    else:
        print(num, "is odd")

Classroom Question

What will this print?

for x in range(5, 0, -1):
    print(x)

Practice Challenge

Write a program that:

  • Prints numbers 1 through 20
  • Prints only those divisible by 4