• Home
  • 3.7 Numeric Underscores

3.7 Numeric Underscores

View Categories

3.7 Numeric Underscores

2 min read

When working with large numeric values, it can be difficult to count the digits correctly.

Python allows underscores (_) to be placed inside numeric literals to improve readability.

Numeric underscores do not change the value of the number. They are simply ignored by Python when the program runs.

This feature is especially useful when working with large numbers such as population, memory sizes, account numbers, hexadecimal values, or binary numbers.


Syntax #

variable_name = number_with_underscores

Components #

Component Description
variable_name Name of the variable.
= Assignment operator.
number_with_underscores Numeric literal containing one or more underscores for readability.

Example 1: Integer with Numeric Underscores #

main.py

population = 1_450_000_000

print(population)

Output #

1450000000

Explanation #

  • Underscores separate groups of digits.
  • Python ignores the underscores.
  • The stored value is exactly the same as 1450000000.

Example 2: Float with Numeric Underscores #

main.py

distance = 123_456.789_012

print(distance)

Output #

123456.789012

Explanation #

  • Underscores can also be used with floating-point numbers.
  • They improve readability without changing the value.

Example 3: Binary Literal with Underscores #

main.py

number = 0b1010_1100

print(number)

Output #

172

Explanation #

  • Underscores are commonly used to group binary digits.
  • 0b1010_1100 is easier to read than 0b10101100.
  • Python ignores the underscore during execution.

Example 4: Hexadecimal Literal with Underscores #

main.py

color = 0xFF_AA_00

print(color)

Output #

16755200

Explanation #

  • Underscores improve the readability of hexadecimal values.
  • This is common in embedded systems and graphics programming.
  • The stored value is still an integer.

Example 5: Scientific Notation with Underscores #

main.py

number = 1.234_567e6

print(number)

Output #

1234567.0

Explanation #

  • Underscores may be used in the coefficient of scientific notation.
  • They make long decimal values easier to read.
  • Python ignores the underscore when evaluating the number.

Example 6: Comparing Values #

main.py

a = 1_000_000
b = 1000000

print(a == b)

Output #

True

Explanation #

  • Both variables store exactly the same numeric value.
  • The underscores are ignored during execution.
  • Therefore, the comparison returns True.

Example 7: Arithmetic with Numeric Underscores #

main.py

salary = 1_200_000
bonus = 150_000

print(salary + bonus)

Output #

1350000

Explanation #

  • Numbers containing underscores can be used in arithmetic expressions.
  • Python performs the calculations normally.
  • The underscores do not affect the result.

Example 8: Type of Numbers with Underscores #

main.py

number = 12_345

print(type(number))

Output #

<class 'int'>

Explanation #

  • Adding underscores does not change the data type.
  • The number is still stored as an integer.

Example 9: Multiple Numeric Types #

main.py

integer = 10_000
decimal = 12_345.67_89

print(integer)
print(decimal)

Output #

10000
12345.6789

Explanation #

  • Numeric underscores can be used in both integers and floating-point numbers.
  • They improve readability without affecting the stored values.

Example 10: Memory Size Example #

main.py

memory = 16_777_216

print(memory)

Output #

16777216

Explanation #

  • Large numeric values are much easier to read when grouped using underscores.
  • This technique is frequently used for memory addresses, file sizes, and hardware-related constants.

Common Mistakes #

1. Placing an Underscore at the Beginning #

Incorrect

number = _1000

Output #

NameError: name '_1000' is not defined

Reason

Python interprets _1000 as an identifier, not as a numeric literal.

Correct

number = 1_000

2. Placing an Underscore at the End #

Incorrect

number = 1000_

Output #

SyntaxError: invalid decimal literal

Reason

Numeric literals cannot end with an underscore.


3. Using Consecutive Underscores #

Incorrect

number = 1__000

Output #

SyntaxError: invalid decimal literal

Reason

Only one underscore may appear between digits.


4. Placing an Underscore Next to the Decimal Point #

Incorrect

number = 123_.45

Output #

SyntaxError: invalid decimal literal

Reason

Underscores cannot appear immediately before or after the decimal point.

Correct

number = 123.45_67

or

number = 123_456.78

Best Practices #

  • Use numeric underscores only to improve readability.
  • Group digits consistently throughout your program.
  • Use underscores for large decimal, binary, octal, and hexadecimal values.
  • Avoid unnecessary underscores in small numbers.
  • Follow a grouping style that makes the number easier to understand.

Key Points to Remember #

  • Numeric underscores improve the readability of numeric literals.
  • Python ignores underscores when evaluating numbers.
  • Underscores can be used with integers, floating-point numbers, binary, octal, hexadecimal, and scientific notation.
  • Numeric underscores do not change the value or data type of a number.
  • Underscores must appear only between digits.
  • Underscores cannot appear at the beginning or end of a numeric literal.
  • Consecutive underscores are not allowed in numeric literals.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *