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
  • Combined Examples

Was this helpful?

  1. Loops

for loop - range (two arguments)

Objective

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

  • Write Python code using the for loop using the range function with two arguments.

Introductory Problem

Using a for loop, print numbers 0 - 6

intro.py
done = False
while not done:
    print("Menu")
    print("I - Introductory Problem")
    print("Q - Quit")
    choice = input("Choice: ")
    if choice == "I":
        print("Introductory Problem")
        for x in range(7):
            print(x)
    elif choice == "Q":
        print("Exiting Game!")
        done = True

for loop

With two arguments in the range function, the sequence starts at the first value and ends one before the second argument.

Programmers usexorias a stepper variable.

Examples

Example 1

Use a for loop to print numbers from 1 to 6.

example1.py
done = False
while not done:
    print("Menu")
    print("E1 - Example 1")
    print("Q - Quit")
    choice = input("Choice: ")
    if choice == "E1":
        print("Example 1")
        for x in range(1,7):
            print(x)
    elif choice == "Q":
        print("Exiting Game!")
        done = True

Example 1 Output

Example 2

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

print("Number\tSquared")

example2.py
done = False
while not done:
    print("Menu")
    print("E2 - Example 2")
    print("Q - Quit")
    choice = input("Choice: ")
    if choice == "E2":
        print("Example 2")
        print("Number\tSquared")
        for x in range(-5,6):
            print(x,"\t",x**2)
    elif choice == "Q":
        print("Exiting Game!")
        done = True

Example 2 Output

Example 3

Ask the user for a range, meaning a minimum and maximum, of numbers . Determine how many numbers are divisible by a number within that range. Divisible means capable of being divided by another number without a remainder. How many questions do you need to ask? Hint: use the modulus operator %.

example3.py
done = False
while not done:
    print("Menu")
    print("E3 - Example 3")
    print("Q - Quit")
    choice = input("Choice: ")
    if choice == "E3":
        print("Example 3")
        count = 0
        min = int(input("Min: "))
        max = int(input("Max: "))
        divisible = int(input("Divisible: "))
        for x in range(min,max+1):
            if x % divisible == 0:
                count = count + 1
        print("Count:",count)
    elif choice == "Q":
        print("Exiting Game!")
        done = True

Example 3 Output

Example output if you choose 5 for the minimum, 10 for the maximum and divisible of 2.

Example 4

From yesterday’s lesson, using a for loop, ask the user for 5 integers. Print the third input.

example4.py
done = False
while not done:
    print("Menu")
    print("E4 - Example 4")
    print("Q - Quit")
    choice = input("Choice: ")
    if choice == "E4":
        for x in range(5):
            num = int(input("Number: "))
            if x == 2:
                third = num
        print("Third Number:",third)
    elif choice == "Q":
        print("Exiting Game!")
        done = True

Example 4 Output

Example output if you choose the numbers 10, 3, 4, 11 and 3.

Combined Examples

done = False
while not done:
    print("Menu")
    print("E1 - Example 1")
    print("E2 - Example 2")
    print("E3 - Example 3")
    print("E4 - Example 4")
    print("Q - Quit")
    choice = input("Choice: ")
    if choice == "E1":
        print("Example 1")
        for x in range(1,7):
            print(x)
    elif choice == "E2":
        print("Example 2")
        print("Number\tSquared")
        for x in range(-5,6):
            print(x,"\t",x**2)
    elif choice == "E3":
        print("Example 3")
        count = 0
        min = int(input("Min: "))
        max = int(input("Max: "))
        divisible = int(input("Divisible: "))
        for x in range(min,max+1):
            if x % divisible == 0:
                count = count + 1
        print("Count:",count)
    elif choice == "E4":
        for x in range(5):
            num = int(input("Number: "))
            if x == 2:
                third = num
        print("Third Number:",third)
    elif choice == "Q":
        print("Exiting Game!")
        done = True

PreviousProblemsNextProblems

Last updated 5 years ago

Was this helpful?