Iterative Patterns - Python Programming Exercise

This Python program allows the user to input two numbers and displays all the numbers between them (inclusive) three times using different types of loops: "for", "while", and "do while". The program is a great way to practice looping structures in Python, as it shows how to iterate through a range of numbers using different techniques. By using the for loop, the program iterates over a specified range, while the while loop continues until a condition is met, and the do while loop ensures the loop runs at least once before checking the condition. This exercise demonstrates the flexibility of looping constructs in Python, making it an essential skill for controlling program flow. By prompting the user to enter the first and last number, the program gives users a practical understanding of how to handle user inputs and perform repetitive tasks efficiently. Whether you are using a for loop, a while loop, or a do while loop, this program illustrates how you can achieve the same result with different looping techniques in Python.

 Category

Mastering Flow Control

 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

 Copy 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

 More Python Programming Exercises of Mastering Flow Control

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.

  •  Numerical Digits

    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...

  •  Empty Square

    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...

  •  Output

    This Python program prompts the user for two integer numbers and calculates their product without using the "*" operator. Instead, it uses consecutiv...

  •  Absolute Magnitude

    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,...

  •  Empty Rectangle

    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...

  •  Data Analysis

    This Python program calculates various basic statistical operations such as sum, average, minimum, and maximum based on user input. The pr...