π Day 26/150 – Print Numbers from 1 to N in Python
Printing numbers from 1 to N is one of the most basic and important programming exercises. It helps you understand loops, iteration, and how Python executes repeated tasks.
Let’s explore different ways to achieve this π
πΉ Method 1 – Using for Loop
The most common and beginner-friendly approach.
n = 10 for i in range(1, n + 1): print(i)
✅ Explanation:
-
range(1, n + 1)generates numbers from 1 to N - The loop prints each number one by one
πΉ Method 2 – Taking User Input
Make the program dynamic by taking input from the user.
✅ Explanation
input()takes value from the userint()converts it into an integer- Loop prints numbers accordingly
πΉ Method 3 – Using while Loop
A condition-based approach.
n = 10 i = 1 while i <= n: print(i) i += 1
✅ Explanation:
-
Starts from
i = 1 -
Runs until
i <= n -
Increments
iafter each iteration
πΉ Method 4 – Using List Comprehension
A more compact and Pythonic way.
n = 10 numbers = [i for i in range(1, n + 1)] print(numbers)
✅ Explanation:
- Creates a list of numbers from 1 to N
- Prints all values at once
π― Final Thoughts
-
Use
for loopfor simple iteration ✅ -
Use
while loopwhen working with conditions π - Use list comprehension for compact code π§

.png)
.png)
.png)
