- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program to Print Numbers in a Range (1,upper) Without Using any Loops
When it is required to print the numbers in a given range without using any loop, a method is defined, that keeps displays numbers from higher range by uniformly decrementing it by one after every print statement.
Below is the demonstration of the same −
Example
def print_nums(upper_num): if(upper_num>0): print_nums(upper_num-1) print(upper_num) upper_lim = 6 print("The upper limit is :") print(upper_lim) print("The numbers are :") print_nums(upper_lim)
Output
The upper limit is : 6 The numbers are : 1 2 3 4 5 6
Explanation
A method named ‘print_nums’ is defined.
It checks if the upper limit is greater than 0.
If so, keeps displaying the elements.
After every display, the upper range value is decremented by 1.
Outside the function, a value for upper limit is defined.
This method is called by passing the parameter.
The output is displayed on the console.
Advertisements