How to Print All Numbers in a Range in Python


In this example, we will learn how to print all given numbers (e.g 1 to n)  without using loops in Python.

Source Code

#! /usr/bin/env python3
# -*- coding: utf-8 -*-

def printNoLoop(n):
    if n > 0:
        printNoLoop(n - 1)
        print(n)

n = int(input("Please input the upper limit of the range: "))
printNoLoop(n)

Output:

Please input the upper limit of the range: 4
1
2
3
4

Please input the upper limit of the range: 8
1
2
3
4
5
6
7
8
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Advertisement