Exercise
Iterative Patterns
Objective
Develop a Python program that prompts the user for two numbers and displays the numbers between them (inclusive) two times using "for" and "while" loops.
Enter the first number: 6
Enter the last number: 12
6 7 8 9 10 11 12
6 7 8 9 10 11 12
6 7 8 9 10 11 12
Example Python Exercise
Show Python Code
#Using "for"
# Prompt the user to enter the first number
start = int(input("Enter the first number: "))
# Prompt the user to enter the last number
end = int(input("Enter the last number: "))
# Use a for loop to display the numbers between start and end (inclusive)
for i in range(start, end + 1):
print(i, end=" ")
print() # Move to the next line
#Using "while"
# Prompt the user to enter the first number
start = int(input("Enter the first number: "))
# Prompt the user to enter the last number
end = int(input("Enter the last number: "))
# Initialize the counter variable
i = start
# Use a while loop to display the numbers between start and end (inclusive)
while i <= end:
print(i, end=" ")
i += 1
print() # Move to the next line
Output
Enter the first number: 6
Enter the last number: 12
6 7 8 9 10 11 12
6 7 8 9 10 11 12
6 7 8 9 10 11 12
Share this Python Exercise
Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.
This Python program demonstrates how to calculate the number of digits in a positive integer by repeatedly dividing the number by 10. If the user enters a n...
This Python program prompts the user to input a symbol and a width, then displays a hollow square with the specified width. The square's outer border is...
This Python program prompts the user for two integer numbers and calculates their product without using the "*" operator. Instead, it uses consecutiv...
This Python program calculates and displays the absolute value of a number x. The absolute value of a number is defined as the number itself if it is positive,...
This Python program prompts the user for a symbol, width, and height, and then displays a hollow rectangle using that symbol for the outer border...
This Python program calculates various basic statistical operations such as sum, average, minimum, and maximum based on user input. The pr...