Iterate for loop with the given number to print n numbers in Python using for loop. Simple example code print first n numbers in Python. First, initialize a variable “numbers” with the number of numbers you want to print. Then use for loop and print the values. Output:
numbers = 10
for num in range[1, numbers + 1]:
print[num, end=' ']
Print numbers from 1 to N using the input function
With the input[] function you can take e user input value.
n = int[input["Please Enter any Number: "]]
print["The List of Natural Numbers from 1", "to", n]
for i in range[1, n + 1]:
print[i, end=' ']
Output:
Please Enter any Number: 5
The List of Natural Numbers from 1 to 5
1 2 3 4 5
Do comment if you have any doubts or suggestions on this Python number topic.
Note: IDE: PyCharm 2021.3.3 [Community Edition]
Windows 10
Python 3.10.1
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.
Python program to get input n and print natural numbers till n.
Sample Input 1:
7
Sample Output 1:
1 2 3 4 5 6 7
Program or Solution
n=int[input["Enter n value:"]]
for i in range[1,n+1]:
print[i,end=" "]
Program Explanation
For Statement is used to execute the sequence of instruction repeatedly.
Range[] method gives list of elements, here range[] method gives list which has 1,2,3... to n. for statement executes the instructions iteratively and for takes the elements one by one as value of i in sequential manner.
so it prints 1 to n.
Python program to print numbers from n to 1 and 1 to n; In this tutorial, you will learn how to print numbers from n to 1 and n to 1 using for loop and while loop.
- Python program to print numbers from 1 to N using for loop
- Python program to print numbers from N to 1 using while loop
Python program to print numbers from 1 to N using for loop
- Take the input from the user by using python input[] function.
- Iterate for loop with the user input number.
- Increment for loop iteration value by 1, as well as print iteration value.
# Python program to print numbers from 1 to n n = int[input["Please Enter any Number: "]] print["The List of Natural Numbers from 1", "to", n] for i in range[1, n + 1]: print [i, end = ' ']
Output
Please Enter any Number: 15 The List of Natural Numbers from 1 to 15 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Python program to print numbers from n to 1 using while loop
- Take the input from the user by using python input[] function.
- Iterate while loop with the user input number.
- Decrement while loop iteration value by 1, as well as print iteration value.
# Python program to print numbers from n to 1 number = int[input["Please Enter any Number: "]] i = number while [ i >= 1]: print [i, end = ' '] i = i - 1
Output
Please Enter any Number: 5 5 4 3 2 1
Recommended Python Programs
My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.
View all posts by Admin