Python Classroom
  • Introduction
  • About Python Classroom
  • Python Cloud Options
    • CS50 IDE
      • CS50 IDE Overview
      • CS50 IDE and Python
      • CS50 IDE Debugging
      • CS50 IDE Shortcuts
      • Linux Bash Shell
      • Vim Mode
        • Vim Tutorial
    • CS50 Sandbox
    • PythonAnywhere
    • Repl.it
  • Python Curriculum Map
    • Pedagogy
      • Python Professional Development
      • Teaching Tips
      • Assessment Tips
      • Rubrics
      • Activities
        • Picture Activity
        • Map Activity
        • Crossing the Bridge
        • NIM
        • Mastermind
        • Cards
          • Card Deck Algorithms
          • Sorting Cards
        • River Crossing
          • Jealous Boyfriends
          • Cannibals and Priests
          • Family
          • Police and the Thief
          • Humans and Monkeys
          • Moving Money
        • Crossing the River
        • Traveling Salesperson
        • Logic Problems
        • CIA Crack the Code
        • IQ Test
        • Puzzles
    • AP Computer Science Principles Framework
  • Python Philosphy
    • How to Practice Python
  • microbit
  • Turtle Graphics
    • Turtle Examples
    • Turtle Activities
    • Turtle Maze Problems
    • Turtle Graphics with loops
    • Turtle Snake
    • Turtle Graphics with Conditionals
  • Output
    • Output Examples
    • Output Mistakes
  • Variables
    • Variable Data Type Examples
    • Variable Role Examples
    • Variables Mistakes
    • Variables Problems
  • Math
    • Math Examples
    • Math Mistakes
    • Math Problems
    • Math Self Check
  • Input
    • Input Examples
    • Input Mistakes
    • Input Problems
  • Decisions
    • if
      • if Examples
      • if Mistakes
      • if Problems
    • if else
      • if else Examples
      • if else Problems
      • if / if else Problems
    • if elif else
      • if elif else Examples
      • if elif else Problems
    • nested if
      • nested if Examples
      • nested if Problems
    • Logical Operators
      • Logical Operators Examples
    • Adventure Game Project
  • Loops
    • while loop - count up
      • Examples
      • Problems
    • while loop - countdown
      • Examples
      • Problems
    • while loop - sentinel value
      • Problems
    • while loop - sentinel menu
      • Problems
    • for loop - range (one argument)
      • Examples
      • Problems
    • for loop - range (two arguments)
      • Problems
    • for loop - range (three arguments)
      • Problems
  • Lists
    • Lists - Numbers
      • Problems
    • Lists - Strings
      • Problems
      • Shopping Project
  • Dictionaries
  • String Methods
  • Functions
    • Variable Scope
    • Functions - no parameters
    • Functions - one parameter
    • Functions - return
    • Functions - lists
  • Files
  • Classes
    • Inheritance
  • Python Projects
    • Adventure Game
    • Restaurant Project
    • Trivia Game
    • Family Tree Project
  • Raspberry Pi
    • Raspberry Pi Models
    • Raspberry Pi Setup
  • Roblox
  • Glossary
Powered by GitBook
On this page
  • Objective
  • Introductory Problem
  • for loop
  • Examples
  • Example 1
  • Example 2
  • Example 3
  • Example 4
  • Conclusion

Was this helpful?

  1. Loops

for loop - range (one argument)

PreviousProblemsNextExamples

Last updated 5 years ago

Was this helpful?

Objective

After working through this lesson, you’ll be able to

  • Write Python code using the for loop using the range function with one argument.

Introductory Problem

You worked on while loop menu code previously. Run the code below and make sure you understand how it works.

menu.py
done = False
while not done:
    print("Menu")
    print("S - Start")
    print("Q - Quit")
    choice = input("Choice: ")
    if choice == "S":
        print("Start")
    elif choice == "Q":
        print("Exiting Menu!")
        done = True

done is a Boolean variable and True is the sentinel value.

Add code for the Intro menu item when the user selects I.

Using a while loop, print your name 5 times.

intro.py
done = False
while not done:
    print("Menu")
    print("I - Intro")
    print("Q - Quit")
    choice = input("Choice: ")
    if choice == "I":
        print("Introductory Problem")
        x = 0
        name = input("What's your name? ")
        while x < 5:
            print(name)
    elif choice == "Q":
        print("Exiting Menu!")
        done = True

for loop

Previously, you used while loops to iterate over a range of integer values. Now you will use a for loop with the range function with one argument.

Programmers usexorias a stepper variable.

Examples

Example 1

Add a Menu Item E1 - Example 1

Using a for loop, print your name 5 times.

example1.py
done = False
while not done:
    print("Menu")
    print("I - Intro")
    print("Q - Quit")
    choice = input("Choice: ")
    if choice == "I":
        print("Introductory Problem")
        x = 0
        name = input("What's your name? ")
        while x < 5:
            print(name)
    elif choice == "Q":
        print("Exiting Menu!")
        done = True
    elif choice == "E1":
        print("Example 1")
        name = input("What is your name? ")
        for i in range(5):
            print(name)

Example 2

Add a Menu Item E2 - Example 2

Using a for loop, print numbers from 0 to 4.

example2.py
done = False
while not done:
    print("Menu")
    print("S - Start")
    print("E2 - Example 2")
    print("Q - Quit")
    choice = input("Choice: ")
    if choice == "S":
        print("Starting Game!")
    elif choice == "Q":
        print("Exiting Menu!")
        done = True
    elif choice == "E2":
        print("Example 2")
        for i in range(5):
            print(i)

Example 3

Add a Menu Item E3 - Example 3

Using a for loop, ask the user for 3 numbers. Print the sum of numbers.

Initialize the variable sum to 0.

example3.py
done = False
while not done:
    print("Menu")
    print("S - Start")
    print("E3 - Example 3")
    print("Q - Quit")
    choice = input("Choice: ")
    if choice == "S":
        print("Starting Game!")
    elif choice == "Q":
        print("Exiting Menu!")
        done = True
    elif choice == "E3":
        print("Example 3")
        sum = 0
        for i in range(3):
            num = int(input("Number: "))
            sum = sum + num
        print(sum)

Example 4

Add a Menu Item E4 - Example 4

Using a for loop, create the table to show the numbers 0 to 10 and their squares. Create a header:

print("Number\tSquared")

example4.py
done = False
while not done:
    print("Menu")
    print("S - Start")
    print("E4 - Example 4")
    print("Q - Quit")
    choice = input("Choice: ")
    if choice == "S":
        print("Starting Game!")
    elif choice == "Q":
        print("Exiting Menu!")
        done = True
    elif choice == "E4":
        print("Number\tSquared")
        for i in range(11):
            print(i,"\t",i**2)

Conclusion

  • In your own words, what is a for loop?

  • What are the differences between a while loop and a for loop.