- Example 1: Decimal Integer Literal
- Example 2: Binary Literal
- Example 3: Octal Literal
- Example 4: Hexadecimal Literal
- Example 5: Floating-Point Literal
- Example 6: Complex Number Literal
- Example 7: Floating-Point Literal Without Leading Zero
- Example 8: Exponential Floating-Point Literal
- Example 9: Negative Number Literals
- Example 10: Different Numeric Literals Together
- Common Mistakes
- Best Practices
- Key Points to Remember
A number literal is a numeric value written directly in a Python program.
Whenever you write a number such as 25, 3.14, or 5+2j in your code, you are using a number literal.
Python supports several types of numeric literals, including:
- Decimal integers
- Binary integers
- Octal integers
- Hexadecimal integers
- Floating-point literals
- Complex number literals
Different literal formats allow programmers to represent numbers conveniently for different applications.
Example 1: Decimal Integer Literal #
main.py
number = 125
print(number)
print(type(number))
Output #
125
<class 'int'>
Explanation #
125is a decimal integer literal.- Decimal numbers use base 10.
- Python stores the value as an integer (
int).
Example 2: Binary Literal #
main.py
number = 0b1010
print(number)
print(type(number))
Output #
10
<class 'int'>
Explanation #
- Binary literals begin with the prefix
0bor0B. - Binary numbers use only the digits
0and1. 0b1010represents the decimal value10.- Python stores the value as an integer.
Example 3: Octal Literal #
main.py
number = 0o25
print(number)
print(type(number))
Output #
21
<class 'int'>
Explanation #
- Octal literals begin with the prefix
0oor0O. - Octal numbers use digits from
0to7. 0o25represents the decimal value21.- The stored data type is
int.
Example 4: Hexadecimal Literal #
main.py
number = 0x2A
print(number)
print(type(number))
Output #
42
<class 'int'>
Explanation #
- Hexadecimal literals begin with the prefix
0xor0X. - They use digits
0โ9and lettersAโF. 0x2Arepresents the decimal value42.- Python stores the value as an integer.
Example 5: Floating-Point Literal #
main.py
pi = 3.14159
print(pi)
print(type(pi))
Output #
3.14159
<class 'float'>
Explanation #
- A floating-point literal contains a decimal point.
- Python stores such values using the
floatdata type.
Example 6: Complex Number Literal #
main.py
number = 4 + 7j
print(number)
print(type(number))
Output #
(4+7j)
<class 'complex'>
Explanation #
- The suffix
jindicates the imaginary part. - Python stores the value as a complex number.
Example 7: Floating-Point Literal Without Leading Zero #
main.py
value = .75
print(value)
Output #
0.75
Explanation #
- Python allows decimal numbers without a leading zero.
- Internally,
.75is treated as0.75. - Writing
0.75is generally preferred for readability.
Example 8: Exponential Floating-Point Literal #
main.py
distance = 1.2e5
print(distance)
print(type(distance))
Output #
120000.0
<class 'float'>
Explanation #
- The letter
erepresents scientific notation. 1.2e5means1.2 ร 10โต.- The value is stored as a floating-point number.
Example 9: Negative Number Literals #
main.py
integer = -100
decimal = -12.5
print(integer)
print(decimal)
Output #
-100
-12.5
Explanation #
- Numeric literals can be positive or negative.
- The minus sign (
-) indicates a negative value. - Both integers and floating-point literals can be negative.
Example 10: Different Numeric Literals Together #
main.py
a = 100
b = 25.5
c = 3 + 4j
print(type(a))
print(type(b))
print(type(c))
Output #
<class 'int'>
<class 'float'>
<class 'complex'>
Explanation #
- Python supports different numeric literal types.
- Each literal is automatically assigned the appropriate data type.
Common Mistakes #
1. Using an Invalid Binary Literal #
Incorrect
number = 0b1021
Output #
SyntaxError: invalid digit '2' in binary literal
Reason
Binary literals can contain only 0 and 1.
2. Using Invalid Digits in an Octal Literal #
Incorrect
number = 0o89
Output #
SyntaxError: invalid digit '8' in octal literal
Reason
Octal literals use digits from 0 to 7.
3. Forgetting the 0x Prefix #
Incorrect
number = FF
Output #
NameError: name 'FF' is not defined
Reason
Hexadecimal literals must begin with 0x or 0X.
Correct
number = 0xFF
4. Using i Instead of j #
Incorrect
number = 5 + 3i
Output #
SyntaxError: invalid decimal literal
Reason
Python uses j to represent the imaginary part of a complex number.
Best Practices #
- Use decimal literals for general-purpose programming.
- Use binary, octal, and hexadecimal literals only when they improve clarity, such as in embedded systems or low-level programming.
- Write floating-point literals with a leading zero (for example,
0.5instead of.5). - Use scientific notation for very large or very small floating-point values.
- Choose the literal format that best represents the data being stored.
Key Points to Remember #
- A numeric literal is a number written directly in the source code.
- Python supports decimal, binary, octal, hexadecimal, floating-point, and complex number literals.
- Binary literals use the
0bprefix. - Octal literals use the
0oprefix. - Hexadecimal literals use the
0xprefix. - Floating-point literals contain a decimal point or scientific notation.
- Complex literals use the suffix
jfor the imaginary part. - Python automatically assigns the correct numeric data type based on the literal format.