Python Program to Print Numbers Without Using Loop

1. Introduction

Printing a sequence of numbers typically involves using iterative structures like loops. However, in Python, it's possible to print a sequence of numbers without utilizing loops, by using recursion or built-in functions that can iterate internally. This can be a fun and educational exercise in thinking about alternative ways to achieve common tasks.

Recursion is a programming technique where a function calls itself to perform a task. This can replace loops for tasks like printing numbers, by calling the function repeatedly with different arguments until a base case is reached.

2. Program Steps

1. Define the starting and ending values for the sequence of numbers to print.

2. Create a recursive function that prints the current number and calls itself with the next number.

3. Implement a base case in the function to stop the recursion when the end of the sequence is reached.

4. Call the recursive function with the starting number.

3. Code Program

# Recursive function to print numbers from start to end
def print_numbers_recursively(start, end):
    # Print the current number
    print(start, end=' ')
    # Base case: if start is less than end, call the function with the next number
    if start < end:
        print_numbers_recursively(start + 1, end)

# Starting and ending values
start_value = 1
end_value = 10
# Call the recursive function to print the numbers
print_numbers_recursively(start_value, end_value)


1 2 3 4 5 6 7 8 9 10


1. print_numbers_recursively is a function that takes two arguments, start and end, which define the range of numbers to print.

2. The function prints the start value, followed by a space for separation.

3. The base case for the recursion is checking if start is less than end.

4. If the base case is True, the function calls itself with start + 1, incrementing the number to print.

5. The recursion continues until start equals end, at which point the recursion stops, as the base case is no longer true.

6. Initially, print_numbers_recursively is called with start_value set to 1 and end_value set to 10.

7. The output shows the numbers from 1 to 10 printed on the same line, demonstrating recursion's ability to handle iteration tasks without explicit loops.