- Remainder
9 % 2 = 1
Check Odd Number in Python
What you’ll learn
- What odd means when dividing by 2.
- A clean
is_oddhelper usingn % 2 != 0. - How to print odd numbers in a fixed range.
Overview
Odd numbers are integers not divisible by 2. In Python, you can classify parity in one line with modulo.
Two programs
Single value check and range printing.
Live preview
Try your own integer and instantly see the result.
Important fact
Zero is even, so it is not odd.
Prerequisites
Modulo operator %, if conditions, and for loops.
- Basic Python syntax with
defandprint. - Understanding that modulo gives remainder after division.
What is an odd number?
An odd number is an integer that cannot be divided by 2 evenly. It always leaves a remainder.
In Python, parity checks are easy and readable using n % 2.
Remainder mod 2
Odd integers are those that are not divisible by 2. In modular arithmetic language, odd numbers have nonzero remainder when divided by 2.
1515 % 2 = 1, so 15 is odd.
Quick examples
- Remainder
8 % 2 = 0
Takeaway: odd and even are opposite parity classes.
Live preview
Uses JavaScript safe integers but follows the same odd-number rule as Python examples.
Algorithm
Goal: decide if a number is odd and print odd values in a range.
Compute modulo
Calculate n % 2.
Check nonzero remainder
If remainder is not 0, number is odd.
Loop through range
Use the same check for each integer from start to end.
📜 Pseudocode
function is_odd(n):
return (n mod 2) != 0
function print_odds(start, end):
for i from start to end:
if is_odd(i):
output i Check one number
Simple helper using modulo to classify a single value.
def is_odd(number: int) -> bool:
return number % 2 != 0
number = 15
if is_odd(number):
print(f"{number} is an odd number.")
else:
print(f"{number} is not an odd number.") Explanation
If remainder by 2 is nonzero, the function returns True.
Odds in [1, 10]
Loop through the range and print values that pass odd check.
def is_odd(number: int) -> bool:
return number % 2 != 0
def print_odds_from_1_to_10() -> None:
print("Odd numbers in the range 1 to 10:")
for i in range(1, 11):
if is_odd(i):
print(i, end=" ")
print_odds_from_1_to_10() Explanation
Python range end is exclusive, so use range(1, 11) to include 10.
Notes
Step by two. For long ranges, start at first odd and increment by 2.
Bit trick. (n & 1) == 1 also detects odd integers.
Interview tip: explain modulo first, then mention bitwise as optional.
❓ FAQ
🔄 Input / output examples
Use this quick table to check your understanding.
| n | Odd? |
|---|---|
0 | No (even) |
15 | Yes |
22 | No |
-5 | Yes |
Edge cases and pitfalls
Odd/even classification works for positive, zero, and negative integers.
n = 0
Zero is even, so odd check returns false.
Still valid
Example: -5 % 2 != 0, so -5 is odd.
Inclusive ranges
Remember Python range excludes end, so use end + 1.
⏱️ Time and space complexity
| Operation | Time | Extra space |
|---|---|---|
is_odd(n) | O(1) | O(1) |
Range [a, b] scan | O(b - a + 1) | O(1) |
Summary
- Test:
n % 2 != 0. - Zero is even, so it is not odd.
- Ranges: reuse same helper in loops.
Every whole number is either even or odd—never both. Zero is even, so the test n % 2 != 0 correctly says zero is not odd.
8 people found this page helpful
