SyntaxError: invalid decimal literal in Python (Solved)
🚫 SyntaxError: invalid decimal literal
The Python error “SyntaxError: invalid decimal literal” occurs if you use an invalid decimal literal in a Python expression, like
amount = 12.5a, or
100_step = 'someValue'.
Here’s what the error looks like:
File /dwd/sandbox/test.py, line 1 100_step = 'someValue' ^ SyntaxError: invalid decimal literal
A SyntaxError is a type of error that occurs when the Python's interpreter encounters a line of code that does not conform to the rules of the Python syntax.
What's a literal? You may ask.
The term "literal" refers to an actual value in the source code. All the following values are literals:
And here's a function call with a decimal literal as an argument:
# Calling a function with a numeric argument addPrice(45.45)
How to fix "SyntaxError: invalid decimal literal"
The error "SyntaxError: invalid decimal literal" happens under the following scenarios:
A decimal value containing letters: In Python, any literal starting with numbers is considered a decimal literal - no matter what side of an operator it is.
Based on Python's syntax, a decimal literal can't contain alphabet letters. For instance,
45KG are invalid decimal literals.
Here are some examples that lead to "SyntaxError: invalid decimal literal":
# 🚫 Invalid decimal literals int(45KG) hour = 9h45 price = 45.99€ score = 34sc
To fix the issue, remove the letters in your numeric values.
An identifier (variable or function name) starting with a number: Identifiers (also referred to as names) are arbitrary names you use for definitions in the source code, such as variable names, function names, class names, etc.
That said, an identifier shouldn't begin with a number because Python's interpreter wouldn't recognize it as such. When a supposed-to-be identifier starts with numbers, Python considers it a decimal literal. That's why it raises a "SyntaxError: invalid decimal literal" once it detects a letter in it:
# 🚫 Raises SyntaxError: invalid decimal literal 100_steps = range(0, 100) # 🚫 Raises SyntaxError: invalid decimal literal def 100_words: # some code here ... pass
In the above example, since the variable name starts with a number, it's considered a decimal literal. But once Python encounters the word
'step', it throws the error. Please note
_ is a valid token used as a delimiter in numbers (Python +3.6)
You can correct your identifiers like so:
# ✅ variable names start with letters & underscores. hundred_steps = range(0, 100) # ✅ function names start with letters & underscores. def hundred_words(): # some code here ... pass
Alright, I think it does it! I hope this short guide helped you fix your problem.
Thanks for reading.
If you read this far, you can tweet to the author to show them you care. Tweet a Thanks
Never miss a guide like this!
Join the DWD mailing list for your weekly dose of knowledge! This mailing list is for doers and thinkers. If you're a curious person, you might find it useful, just as 2,000 other devs do!
Disclaimer: This post might contain affiliate links. I might receive a commission if a purchase is made. However, it doesn’t change the cost you’ll pay.