Python Programs | IT Developer
IT Developer

Python Programs



Share with a Friend

Python Programs - Looping Statements

Print numbers from 1 to 100 - Python Program

Using a Basic For Loop

Example 1 :

A for loop is one of the simplest and most efficient ways to print numbers from 1 to 100. It's straightforward:

# Print numbers from 1 to 100 # Using for loop with range() for num in range(1, 101): print(num, end=" ") print("\nDone printing 1 to 100.")

Output

 
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 
Done printing 1 to 100. 
 

Explanation

  • range(1, 101) generates numbers from 1 to 100 (upper limit excluded).
  • end=" " ensures numbers are printed in one line separated by spaces.
  • Time complexity: O(n) where n = 100.

print(num)

Using print(num) is used to print numbers from 1 to 100 in vertical form.

print(num, end=" ")

Using end=" " is used to print numbers from 1 to 100 in horizontal form.

Using a While Loop

Example 2 :

A while loop provides another way to achieve the same result. This loop is useful when the condition isn't fixed, providing more control over the iteration process:

# Print numbers from 1 to 100 # Using while loop num = 1 while num <= 100: print(num, end=" ") num += 1 print("\nDone printing 1 to 100.")

Output

 
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 
Done printing 1 to 100. 
 

Explanation

In while loopnum starts at 1 and increments by 1 in each loop iteration until it exceeds 100. The while loop checks the condition num <= 100 before proceeding with each iteration.

Utilizing List Comprehension

Example 3 :

List comprehension is a concise, Pythonic way to work with lists:

# Print numbers from 1 to 100 # Using List [print(i, end=" ") for i in range(1, 101)]

Output

 
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 
 

Recursive Function Approach

Example 4 :

Recursion is an advanced concept, often used for complex problems. However, it can print numbers too:

# Print numbers from 1 to 100 # Using Recursion def print_numbers(n): if n > 100: return print(n, end=" ") print_numbers(n + 1) print_numbers(1)

Output

 
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 
 

This function calls itself with incremented values until n exceeds 100. Recursion expresses elegance in solving problems by breaking them into smaller, similar tasks but watch for Python's recursion limit, which can be restrictive with large sequences.

Using Built-in Functions and Itertools

Example 5 :

Python offers powerful built-in libraries. Here\'s how itertools can be used:

# Print numbers from 1 to 100 # Using built-in functions import itertools for i in itertools.islice(itertools.count(1), 100): print(i, end=" ")

Output

 
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 
 

itertools showcases Python’s ability to handle sequences creatively. The count function starts at 1, and islice specifies the range. It’s an exploration into Python's advanced capabilities, useful for more complex scenarios beyond simple printing.