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
Run in browser1# Program to print odd numbers up to N23n = int(input("Enter upper limit: "))45for 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.