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
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 = Truefor loop
With two arguments in the range function, the sequence starts at the first value and ends one before the second argument.


Examples
Example 1
Use a for loop to print numbers from 1 to 6.
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")
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 %.
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.
Example 4 Output
Example output if you choose the numbers 10, 3, 4, 11 and 3.

Combined Examples
Last updated
Was this helpful?