---
title: 3.5 Number Literals
description: "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,…"
url: "https://edcret.in/docs/python-programming/numeric-data-types-python/3-5-number-literals/"
updated: "2026-07-29"
category: Python Programming / Numeric Data Types
---

# 3.5 Number Literals

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

- `125` is 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 `0b` or `0B`.
- Binary numbers use only the digits `0` and `1`.
- `0b1010` represents the decimal value `10`.
- 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 `0o` or `0O`.
- Octal numbers use digits from `0` to `7`.
- `0o25` represents the decimal value `21`.
- 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 `0x` or `0X`.
- They use digits `0–9` and letters `A–F`.
- `0x2A` represents the decimal value `42`.
- 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 `float` data type.

---

## Example 6: Complex Number Literal

**main.py**

```
number = 4 + 7j

print(number)
print(type(number))
```

### Output

```
(4+7j)
<class 'complex'>
```

### Explanation

- The suffix `j` indicates 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, `.75` is treated as `0.75`.
- Writing `0.75` is 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 `e` represents scientific notation.
- `1.2e5` means `1.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.5` instead 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 `0b` prefix.
- Octal literals use the `0o` prefix.
- Hexadecimal literals use the `0x` prefix.
- Floating-point literals contain a decimal point or scientific notation.
- Complex literals use the suffix `j` for the imaginary part.
- Python automatically assigns the correct numeric data type based on the literal format.
