In this post, we will learn a python program to print numbers from 1 to 100, 1 to n, and in a given interval using for-loop and while loop both.
But before writing a program you should know about:
- while-loop
- for-loop
- how to take user input (for printing numbers from 1 to n and in a given interval)
Now let’s write a program in all the ways one by one.
1. Print Numbers from 1 to 100 in Python Using while-loop
Source Code:
i = 1
while i <= 100:
print(i, end=" ")
i += 1
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
In this example, the variable i
is initialized to 1, and the while
loop will continue to run as long as i
is less than or equal to 100. Inside the loop, the current value of i
is printed, and then i
is incremented by 1 using the +=
operator. This will continue until i
is no longer less than or equal to 100.
and end
attribute is used to print all the values of i
in one line separated by space.
2. Print Numbers from 1 to 100 in Python Using for-loop
Source Code:
for i in range(1,101):
print(i, end=" ")
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
The range()
function is used to generate a sequence of numbers, and the for
loop is used to iterate over the sequence. In this example, the range()
function is set to start at 1 and end at 101 (the range function will stop at 100), so the loop will iterate 100 times, and the variable i
will take on the value of each number in the sequence.
and end
attribute is used to print all the values of i
in one line separated by space.
3. Python Program to Print Numbers from 1 to 100 in Single line
A more pythonic way of doing this, would be using List comprehension. Here’s an example:
Source Code:
print([i for i in range(1,101)])
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]
4. Print Numbers from 1 to N in Python Using while-loop
Source Code:
n = int(input('Enter a number : '))
i = 1
while i <= n:
print(i, end=" ")
i+=1
Output:
Enter a number : 10
1 2 3 4 5 6 7 8 9 10
5. Print Numbers from 1 to N in Python Using for-loop
Source Code:
n = int(input('Enter a number : '))
for i in range(1,n+1):
print(i, end=" ")
Output:
Enter a number : 50
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
6. Python Program to Print Numbers from 1 to N in Single line
Source Code:
num = int(input('Enter number : '))
print([i for i in range(1,num+1)])
Output:
Enter number : 20
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
7. Write a python program to print numbers in a given range using a while loop
Source Code:
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
while num1 <= num2:
print(num1, end=" ")
num1 += 1
Output:
Enter first number: 10
Enter second number: 20
10 11 12 13 14 15 16 17 18 19 20
8. Write a python program to print numbers in a given range using a for loop
Source Code:
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
for i in range(num1, num2+1):
print(i, end=" ")
Output:
Enter first number: 5
Enter second number: 25
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
9. Python program to print list of numbers in the given range
Source Code:
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
print([i for i in range(num1,num2+1)])
Output:
Enter first number: 15
Enter second number: 30
[15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]