Learn Python Integer Literals: Decimal, Binary, Octal and Hexadecimal

Understanding Python Integer Literals

Many Python programs need to work with whole number values such as ages, counts, quantities, scores, and measurements. These values can be represented in Python in different ways: they can either be written directly in the code or created dynamically during program execution.

When a whole number value is written directly in Python code, it is represented using an integer literal. For values created or received during program execution, Python uses integer objects and other mechanisms such as user input, calculations, file handling or external data sources.

🚀 Getting Started: New to literals? Start with our complete guide to Python literals to understand how different literal types are used in Python.

💡 Tip: To understand how Python stores and works with integer values internally, explore our guide on the Python int() function.

Introduction: What is an Integer Literal?

Definition: An integer literal is a whole number written directly in Python code without a decimal point. It represents a fixed integer value that can be assigned to variables, used in expressions or passed to functions.

Example: In the statement age = 25, the value 25 is an integer literal because it is a whole number written directly in the code.

Besides regular decimal numbers, Python also allows integer literals to be written in binary, octal, and hexadecimal formats. Although they use different prefixes, Python treats all of them as integer values.

Let’s explore the different forms of integer literals supported by Python.

Forms of Integer Literals in Python

The following are the different forms of integer literals supported in Python:

Let’s understand each form of integer literal with simple examples.

1. Decimal Integer Literals (Base 10)

Decimal integers are the standard whole numbers used in everyday calculations. They are written without any prefix and are the most commonly used integer literals in Python.

Syntax
variable_name = integer_value

Explanation: Here, variable_name is the name of the variable, and integer_value represents any whole number written directly in the code, such as 10, -5, or 100.

Example 1: Decimal Integer Literal
# Decimal integer literal

age = 25

print(age)      


# Output: 25

Explanation: The value 25 is a decimal integer literal. Since it has no prefix or decimal point, Python interprets it as a base-10 (decimal) integer.

Example 2: Negative Decimal Integer Literal
# Negative decimal integer literal

temperature = -5

print(temperature)      


# Output: -5

Explanation: The value -5 is a negative decimal integer literal. The minus sign indicates a negative whole number, while the value is still interpreted as a decimal integer.

⬆ Move to Top

2. Binary Integer Literals (Base 2)

Binary integer literals represent numbers using only 0 and 1. They are written with the prefix 0b or 0B, which tells Python to interpret the value as a binary (base-2) number.

Syntax
variable_name = 0b_binary_value

Explanation: Here, variable_name is the name of the variable, and 0b_binary_value represents a binary integer literal consisting of only 0 and 1, prefixed with 0b or 0B.

Example 1: Binary Integer Literal
# Binary integer literal

binary_number = 0b1011      # Binary 1011 = Decimal 11

print(binary_number)        


# Output: 11

Explanation: The binary literal 0b1011 represents the binary number 1011. Python automatically converts it to its decimal equivalent, 11, when the value is used or printed.

Example 2: Negative Binary Integer Literal
# Negative binary integer literal

negative_binary = -0b1011

print(negative_binary)      


# Output: -11

Explanation: The value -0b1011 is a negative binary integer literal. The minus sign indicates a negative value, while Python converts the binary number 1011 to its decimal equivalent and returns -11.

⬆ Move to Top

3. Octal Integer Literals (Base 8)

Octal integer literals represent numbers using the base-8 numeral system. They are written with the prefix 0o or 0O, which tells Python to interpret the value as an octal (base-8) number.

Syntax
variable_name = 0o_octal_value

Explanation: Here, variable_name is the name of the variable, and 0o_octal_value represents an octal integer literal consisting of digits from 0 to 7, prefixed with 0o or 0O.

Example 1: Octal Integer Literal
# Octal integer literal

octal_number = 0o17      # Octal 17 = Decimal 15

print(octal_number)      


# Output: 15

Explanation: The octal literal 0o17 represents the octal number 17. Python automatically converts it to its decimal equivalent, 15, when the value is used or printed.

Example 2: Negative Octal Integer Literal
# Negative octal integer literal

negative_octal = -0o17

print(negative_octal)      


# Output: -15

Explanation: The value -0o17 is a negative octal integer literal. The minus sign indicates a negative value, while Python converts the octal number 17 to its decimal equivalent and returns -15.

⬆ Move to Top

4. Hexadecimal Integer Literals (Base 16)

Hexadecimal integer literals represent numbers using the base-16 numeral system. They are written with the prefix 0x or 0X, which tells Python to interpret the value as a hexadecimal (base-16) number.

Syntax
variable_name = 0x_hexadecimal_value

Explanation: Here, variable_name is the name of the variable, and 0x_hexadecimal_value represents a hexadecimal integer literal consisting of digits 0 to 9 and letters A to F (or a to f), prefixed with 0x or 0X.

Example 1: Hexadecimal Integer Literal
# Hexadecimal integer literal

hex_number = 0x1A      # Hexadecimal 1A = Decimal 26

print(hex_number)      


# Output: 26

Explanation: The hexadecimal literal 0x1A represents the hexadecimal number 1A. Python automatically converts it to its decimal equivalent, 26, when the value is used or printed.

Example 2: Negative Hexadecimal Integer Literal
# Negative hexadecimal integer literal

negative_hex = -0x1A

print(negative_hex)      


# Output: -26

Explanation: The value -0x1A is a negative hexadecimal integer literal. The minus sign indicates a negative value, while Python converts the hexadecimal number 1A to its decimal equivalent and returns -26.

⬆ Move to Top

Key Examples at a Glance: Integer Literals

The following table summarizes the different types of Python integer literals discussed above:

Type Base Prefix Example Meaning
Decimal Literals 10 None 25, -5 Standard whole numbers
Binary Literals 2 0b / 0B 0b1011, -0b1011 Binary representation (Base 2)
Octal Literals 8 0o / 0O 0o17, -0o17 Base-8 number system
Hexadecimal Literals 16 0x / 0X 0x1A, -0x1A Base-16 representation

Key Takeaways: Integer Literals

Here are the key points to remember about Python integer literals:

  • An integer literal is a whole number written directly in Python code without a decimal point.
  • Python supports different integer literal formats, including decimal, binary, octal, and hexadecimal.
  • Binary, octal, and hexadecimal literals use the prefixes 0b, 0o, and 0x.
  • Integer literals can represent positive and negative whole numbers.
  • Python internally treats all integer literals as integer values during execution.

Leave a Comment

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

Scroll to Top