Happy Friday!!

Warmup

Write a while loop that prints the numbers 1 through 100 and then prints 'done!' Skip multiples of 5 using 'continue'

a number is a multiple of 5 if

number % 5 == 0 

Agenda / Looking ahead

  • Unit 2 Quiz 1
  • Grades: behind, sync ... will complete, and issues hopefully resolved, this weekend
  • Lesson 2.6 'for loops'
  • Module 2 Test due today (do for homework if not complete already)
  • Begin Assignment 2.6

Lesson 2.6

Introduction to For Loops

Why For Loops?

  • while is good when we don’t know how many times
  • for is good when we want to loop over a sequence
  • Commonly used with ranges and collections

Syntax of a for loop

for variable in sequence:
    # code runs once for each item in sequence

Example with range()

for i in range(5):
    print(i)
  • Prints 0, 1, 2, 3, 4

Example with strings

for letter in "cat":
    print(letter)
  • Prints each letter on its own line

Indentation

  • Code inside the for loop must be indented

Classroom Question

What will this print?

for num in range(2, 6):
    print(num)

Practice Challenge

Write a program that:

  • Prints the numbers 1 through 10
  • Prints "Even" if the number is even, "Odd" if odd