Print Odd Numbers in Range in Python

Print all odd numbers between 1 and a given upper limit.

BeginnerLoop ProgramsExample 3 of 25
print-odd-numbers-in-range.py
1# Program to print odd numbers up to N
2
3n = int(input("Enter upper limit: "))
4
5for i in range(1, n + 1, 2):
6 print(i)

Output

Enter upper limit: 10
1
3
5
7
9

What's going on

We start from 1 and step by 2 with range(1, n + 1, 2) to generate all odd numbers <= n.

After this program

If the idea is still fuzzy, read Day 9: Loops and Iteration in the Python course. For a short check, try the Python quiz (300 questions). Stay with all 25 loop programs, or see every topic on Python exercises.