Python Program to Print Odd Numbers From 1 to 100

Python Program to Print Odd Numbers From 1 to 100
Reading Time: 7 minutes

Table of Contents

Python Program to Print Odd Numbers From 1 to 100: A Comprehensive Guide

Python is a versatile and beginner-friendly programming language that makes tasks like printing odd numbers a breeze. Whether you’re a newbie coder or someone brushing up on basics, understanding how to create a Python program to print odd numbers from 1 to 100 is a fantastic way to dive into loops, conditionals, and number manipulation. In this in-depth blog post, we’ll explore everything you need to know about printing odd numbers in Python—from simple code examples to detailed explanations and variations. Let’s get started!

Introduction to Printing Odd Numbers in Python

Odd numbers are integers that cannot be divided evenly by 2—like 1, 3, 5, and so on. They’re the opposite of even numbers (like 2, 4, 6), and they pop up everywhere in math and programming. In Python, printing odd numbers is a common exercise to learn how to work with loops and conditions. Why Python? It’s simple syntax and powerful built-in functions make it perfect for beginners and pros alike. When we talk about a Python program to print odd numbers from 1 to 100, we’re really talking about using Python’s tools to identify and display every odd number in that range.

Think of this as a foundational skill. Once you master how to print odd numbers in Python, you’ll be ready to tackle more complex tasks like filtering data or generating sequences. In this section, we’ll set the stage by explaining what odd numbers are and why Python is the go-to language for this task. Fun fact: there are exactly 50 odd numbers between 1 and 100—more on that later!

Python Program to Print Odd Numbers From 1 to 100 Using a For Loop

Let’s jump into the most popular way to print odd numbers in Python: using a for loop. A for loop is like a trusty assistant that repeats a task for every item in a sequence. Here, our sequence is the numbers from 1 to 100, and our task is to check each one to see if it’s odd.

Code Example

				
					for num in range(1, 101):  # 101 because range is exclusive of the upper bound
    if num % 2 != 0:       # Check if the number is odd
        print(num, end=" ")
				
			

Output:

				
					1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99
				
			

Explanation

  • range(1, 101): This generates numbers from 1 to 100. The upper bound (101) is exclusive, so it stops at 100.
  • num % 2 != 0: The modulo operator % gives the remainder after division. If a number divided by 2 has a remainder (not 0), it’s odd.
  • print(num, end=” “): Prints each odd number with a space instead of a new line for a clean output.

This method is straightforward and efficient. The for loop odd numbers Python approach is a favorite because it’s readable and quick. Want to make it even shorter? You can use a step value in range():

				
					for num in range(1, 101, 2):  # Start at 1, step by 2
    print(num, end=" ")
				
			
Here, range(1, 101, 2) jumps straight to odd numbers (1, 3, 5…), cutting out the need for a condition. Both methods work, but the second is more concise!

Using a While Loop to Print Odd Numbers From 1 to 100 in Python

Not a fan of for loops? No problem! A while loop can also get the job done. This loop keeps running as long as a condition is true, giving you more control over the flow. Let’s see how to use it in a Python program to print odd numbers from 1 to 100.

Code Example

				
					num = 1
while num <= 100:
    if num % 2 != 0:
        print(num, end=" ")
    num += 1
				
			

Output:

				
					1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99
				
			

Explanation

  • num = 1: Start at 1, the first odd number.
  • while num <= 100: Keep going until we hit 100.
  • if num % 2 != 0: Same odd-number check as before.
  • num += 1: Increment num by 1 each time to avoid an infinite loop.

The while loop is a bit more manual than a for loop, but it’s great for understanding how loops work under the hood. It’s also flexible—if you wanted to tweak the step size or add more conditions, a while loop makes it easy. For example, you could increment by 2 (num += 2) starting from 1 to skip even numbers entirely.

How to Print Odd Numbers in Python: Step-by-Step Explanation

Now that we’ve seen the code, let’s break down how to print odd numbers in Python step by step. This is where the magic happens—understanding the logic is key to mastering programming.

The Logic Breakdown

  • Define the Range: We want numbers from 1 to 100. Python’s range() function or a counter variable helps us here.
  • Identify Odd Numbers: A number is odd if dividing it by 2 leaves a remainder. In code, num % 2 != 0 does this check.
  • Loop Through Numbers: Use a for or while loop to visit each number in the range.
  • Print the Odd Ones: When a number passes the odd test, display it.

Algorithm in Plain English

  • Start at 1.
  • Check if it’s odd (remainder when divided by 2 isn’t 0).
  • If yes, print it. If no, skip it.
  • Move to the next number.
  • Stop at 100.

Why It Works

The modulo operator (%) is the star here. It’s a simple yet powerful tool for number classification. For instance:

  • 5 % 2 = 1 (odd)
  • 6 % 2 = 0 (even)

This logic is universal—you can use it to print odd numbers in Python for any range, not just 1 to 100. It’s a building block for more advanced tasks like filtering lists or finding patterns.

Common Variations: Printing Odd Numbers in Different Ranges

The beauty of a Python program to print odd numbers from 1 to 100 is its adaptability. Let’s explore some variations to suit different needs.

1. Odd Numbers From 1 to 10

				
					for num in range(1, 11, 2):
    print(num, end=" ")
				
			

Output:

				
					1 3 5 7 9
				
			

2. Odd Numbers From 1 to n (User-Defined)

				
					n = int(input("Enter a number: "))
for num in range(1, n + 1, 2):
    print(num, end=" ")
				
			
				
					Example Input: 20
Output: 1 3 5 7 9 11 13 15 17 19
				
			

3. Custom Range (11 to 33)

				
					for num in range(11, 34, 2):
    print(num, end=" ")
				
			

Output:

				
					11 13 15 17 19 21 23 25 27 29 31 33
				
			

These examples show how flexible Python is. The range(start, stop, step) function is your friend—adjust the start and stop to fit any range, and set step to 2 to hit only odd numbers. This adaptability makes for loop odd numbers Python a go-to technique.

Understanding Odd Numbers: How Many Are Between 1 and 100?

Let’s wrap up with some math and facts. How many odd numbers are there from 1 to 100? It’s a common question tied to our topic, and the answer is 50.

Calculation

  • Odd numbers form an arithmetic sequence: 1, 3, 5, …, 99.
  • Formula for the nth term: a_n = a_1 + (n-1)d, where a_1 = 1, d = 2 (difference), and a_n = 99.
  • Solve: 99 = 1 + (n-1) * 2
    • 98 = (n-1) * 2
    • n-1 = 49
    • n = 50

So, there are 50 terms—or 50 odd numbers.

Quick List

PositionOdd Number
11
23
35
4997
5099

Fun Fact

The sum of odd numbers from 1 to 99 is 2500 (50²). This pattern holds for any sequence of consecutive odd numbers starting from 1!

Conclusion

Mastering a Python program to print odd numbers from 1 to 100 is more than just a coding exercise—it’s a gateway to understanding loops, conditions, and number logic. Whether you use a for loop for simplicity or a while loop for control, Python makes it easy and fun. We’ve covered the basics, explored variations, and even calculated that there are 50 odd numbers in this range. Now it’s your turn—try tweaking the code, experimenting with ranges, and building on this foundation. Happy coding!

Related Courses

Join SystechGroup’s course today and upgrade your skills. Enroll now!

TrichyCoimbatore

FAQs

The simplest way to print odd numbers in Python from 1 to 100 is using a for loop with a step value of 2 in the range() function. Here’s the code:

for num in range(1, 101, 2):

    print(num, end=” “)

This starts at 1, skips to every odd number (3, 5, 7…), and stops at 99. It’s quick, readable, and doesn’t need an extra condition to check for oddness.

Yes! A list comprehension is a concise way to create a list of odd numbers, which you can then print. Here’s how:

odd_numbers = [num for num in range(1, 101) if num % 2 != 0]

print(*odd_numbers)

This generates a list of odd numbers from 1 to 100 and uses the * operator to unpack and print them. It’s a fancy alternative to a for loop odd numbers Python approach, though it uses more memory for large ranges.

To print odd numbers from 1 to 100 in reverse (99 down to 1), adjust the range() function to count backwards with a step of -2:

for num in range(99, 0, -2):

    print(num, end=” “)

This starts at 99, steps down by 2 (99, 97, 95…), and stops before 0. It’s a fun twist on the standard Python program to print odd numbers from 1 to 100.

If your while loop gets stuck, you might have forgotten to increment the counter. Here’s a correct example:

num = 1

while num <= 100:

    if num % 2 != 0:

        print(num, end=” “)

    num += 1  # Don’t forget this!

Without num += 1, the loop runs forever because num never changes. This is a common mistake when learning how to print odd numbers in Python with a while loop.

To count odd numbers instead of printing them, use a counter variable in your loop. Here’s the code:

count = 0

for num in range(1, 101):

    if num % 2 != 0:

        count += 1

print(“Number of odd numbers:”, count)