What are Magic Numbers?
Have you ever seen code like this?
def calculate_discount(price):
return price * 0.15What does 0.15 mean? Is it 15%? A discount? A tax?
Magic numbers are numeric values that appear in code without any explanation. They make code hard to read, maintain, and debug.
Why are Magic Numbers a Problem?
1. Hard to Read
Other developers (or future you) can’t understand what the numbers mean.
2. Hard to Maintain
If you need to change a value, you have to search through the entire codebase.
3. Easy to Make Mistakes
The same number might mean different things in different places.
The Solution: Named Constants
PEP 8
recommends using UPPER_SNAKE_CASE for constants. Additionally, clean code practices recommend replacing magic numbers with named constants for better readability.
Practical Example
❌ Avoid this:
def calculate_discount(price):
return price * 0.15✅ Do this instead:
DISCOUNT_RATE = 0.15
def calculate_discount(price):
return price * DISCOUNT_RATEAnother example:
❌ Avoid this:
# What's 86400? How long is that?
time_seconds = 86400✅ Do this instead:
SECONDS_IN_DAY = 86400
time_seconds = SECONDS_IN_DAYThe Zen of Python
PEP 20 (The Zen of Python) teaches us:
“Explicit is better than implicit.”
“Readability counts.”
Named constants make code explicit and readable!
When You DON’T Need Constants
Not every number is a “magic number”. These are acceptable:
0,1,-1- often used as initial values, boundaries, or return signals- Loop counters - like
for index in range(10) - Array indices - like
items[0]oritems[-1] - Simple math - like
x * 2ory + 1where the operation is clear - Boolean comparisons - like
if count > 0
Example where constants are NOT needed:
# These are fine - the meaning is obvious
for index in range(10):
print(index)
if len(items) > 0:
return items[0]
result = value * 2 # Doubling is clearConclusion
Replacing magic numbers with named constants is a simple habit that greatly improves your code quality. Your team (and your future self) will thank you!
Remember: Code is read much more often than it’s written. Make it clear.

