Understanding Number Datatype in Python: Decimal, Octal and Hexadecimal Numbers

Python DataTypes - Number Datatype in Python
Python DataTypes – Number Datatype in Python

Understanding the Number Datatype in Python: A Comprehensive Overview

Number datatype in Python is a combination of different number format classes like Integer, Float, and Complex. Python supports the Decimal, Octal decimal, and Hexadecimal number systems.

Program – Integer Number datatype

print("integer number representation")
number_int = 10
print(number_int)
print(type(number_int))

number_long = 999849389348943899988
print(number_long)
print(type(number_long))

print("\n---------------------------------------------------\n")

print("Octal decimal number representation")
number_octal1 = 0o12
# octal1 decimal equivalent = 1*8^1 + 1*8^0 = 9
print(number_octal1)
print(type(number_octal1))

number_octal2 = 0o7654321
# 7*8^6 + 6*8^5 + 5*8^4 + 4*8^3 + 3*8^2 + 2*8^1 + 1*8^0 = 2054353
print(number_octal2)
print(type(number_octal2))

print("\n---------------------------------------------------\n")

print("Hexadecimal number representation")
number_hexa1 = 0x12
# hexa1 decimal equivalent = 1*16^1 + 1*16^0 = 17
print(number_hexa1)
print(type(number_hexa1))

number_hexa2 = 0xfedcba987654321
# 16*16^15 + 15*16^14 + 14*16^13 + 13*16^12 + 12*16^11 + 11*16^10 +
# 10*16^9 + 9*16^8 + 8*16^7 + 7*16^6 + 6*16^5 + 5*16^4 +
# 4*16^3 + 3*16^2 + 2*16^1 + 1*16^0 = 1147797409030816545
print(number_hexa2)
print(type(number_hexa2))

Output of the Integer Number datatype program:

integer number representation
10
<class 'int'>
999849389348943899988
<class 'int'>

---------------------------------------------------

Octal decimal number representation
10
<class 'int'>
2054353
<class 'int'>

---------------------------------------------------

Hexadecimal number representation
18
<class 'int'>
1147797409030816545
<class 'int'>

Note from the output:

  • Datatype class for Decimal , Octal decimal and Hexadecimal number is <class ‘int’>

Exploring Float Number Datatype in Python: Understanding Float Datatype

Numbers in Python can also be of class type float. For example, 2.38 and 89.32 are numbers with decimal point values. Float also supports exponent representation, such as 39.2e57.

Program – Float Number Datatype in Python

print("Float number representation - float type")
number_float = 10.53
print(number_float)
print(type(number_float))

Output of the Float Number Datatype program:

Float number representation - float type
10.53
<class 'float'>

Note from the output:

  • Datatype class for float numbers is <class ‘float’>

Understanding the Complex Datatype in Python

  • Numbers in Python can also be of class type complex are Complex Numbers.
  • Complex numbers have real and imaginary parts with representation of x + yj.
    • Number value with j is an imaginary part and the other is the real part of the complex number.
    • Real part and imaginary part can also be zero.
  • For example, 10 – 36j – this is a complex number, 10 is real part and -36 is an imaginary part of this complex number

Program – Complex Datatype in Python

print("Complex number representation - complex type")
number_complex = 10 + 12j
print(number_complex)
print(type(number_complex))
print(number_complex.real)
print(number_complex.imag)

Output of the Complex Datatype program:

Complex number representation - complex type
(10+12j)
<class 'complex'>
10.0
12.0

Note from the output:

  • Datatype class for complex numbers is <class ‘complex’>

Integer Numbers in Various Number Systems

Integer numbers can be classified as signed (negative) or unsigned (positive) values. These integer numbers can be represented across multiple number systems, including Decimal, Octal, and Hexadecimal. Ultimately, all these representations are converted to decimal numbers for uniformity.

  • The Decimal number system operates with a base of 10.
  • The Octal number system utilizes a base of 8.
  • The Hexadecimal number system functions with a base of 16.

Example:

  • 98 and -98 are integer numbers in the Decimal number system.
  • 0xfe represents an integer number in the Hexadecimal number system.
  • 0o76 denotes an integer number in the Octal number system.

Conversion Between Number Systems: Understanding Decimal, Octal and Hexadecimal Numbers

Using the base values of the number system, we can convert a number from one number system to another number system.

  • Decimal Conversions: We can convert a Decimal number to an Octal decimal number or a Hexadecimal number, and vice-versa.
  • Octal to Hexadecimal Conversions: We can convert an Octal decimal number to a Hexadecimal number, and vice-versa.

Example: Converting Octal Decimal Number to Decimal Number

  • Octal numbers are of base 8. Converting octal numbers to decimal by multiplying number’s each digit with 8 to the power index of the digit.
  • Octal (base 8) decimal equivalent: 0o12 = 1*81 + 2*80 = 10

Octal number 0o12, 1 multiplied by 8 to power 1, because 1 is at index 1 plus 2 multiplied with 8 to power 0 because 2 is at index 0.

Example: Converting Hexadecimal Number to Decimal Number

  • Hexadecimal numbers are of base 16. Converting hexadecimal numbers to decimal by multiplying numbers each digit with 16 to the power index of the digit.
  • Hexadecimal (base 16) equivalent: 0x12 = 1*161 + 2*160 = 18

Hexadecimal number 0x12, 1 multiplied by 16 to power 1, because 1 is at index 1 plus 2 multiplied with 16 to power 0 because 2 is at index 0.

Boolean Datatype in Python

The Bool class is another important datatype in Python, primarily utilized for comparisons and conditional statements. The two possible values for a boolean datatype variable are True and False.

Program – Boolean Datatype

boolean_true = True
print(boolean_true)
print(type(boolean_true))

boolean_false = False
print(boolean_false)
print(type(boolean_false))

Output of the Boolean Datatype program:

True
<class 'bool'>
False
<class 'bool'>

Summary

This article discusses Python’s number systems, such as Decimal, Octal, and Hexadecimal, along with their conversion methods. It also explores various datatypes, including Integer, Float, Complex, and Boolean, emphasizing their representations and characteristics in Python.

For an in-depth understanding of Variables and Data Types in Python, check out our article, “Variables in Python & Variable Data Types.

Python Code – Github Repository

Code snippets and programs related to Number Datatype in Python, can be accessed from GitHub Repository. This GitHub repository all contains programs related to other topics in Python tutorial.

Interview Questions & Answers

How can you convert between different numeric data types in Python?

We can convert the numeric datatype using the class of another numeric datatype.

a = 54.7         # <class 'float'>
b = int(a)       # 54; converted to <class 'int'>
c = float(b)   # 54.0; converted to <class 'float'>
d = complex(b)  # 54 + 0j;converted to <class 'complex'> 

How do you handle very large integers in Python?

Python’s has no explicitly defined limit size for int datatype. Python automatically allocates memory as much is required. Python int datatype automatically handles large integers.

For example – large_int = 987654321801234567890128974557901234567890

How Python handles floating-point precision.

Python represents floating point numbers using double-precision (64-bit) as per the IEEE 754 standard. But floating-point numbers have limited precision. That is why, float number operations may result in rounding errors.

For example – result = 3.1 + 1.2 # Expected 4.3, but can get 4.30000000000000004

How can you convert other data types to Boolean in Python?

bool() function can be used to convert other data types to a Boolean. Boolean can have two possible value True or False. During conversion to boolean value:

  • Non-zero numbers, non-empty strings, and non-empty collection datatypes (like lists, tuples) are True.
  • Zero numbers, empty strings, and empty containers are False.
bool(0)     # False (zero number)
bool(1)     # True  (non-zero number)
bool(''")     # False (empty string)
bool('Python')  # True  (non empty string)

Related Topics

  • What Are Comments and Docstring in Python (With Programs)?
    Before we start learning about Comments and Docstring in Python and its concepts, we should setup our Python Integrated Development Environment (IDE) on our local machine. Read through the following articles to learn more about What is an IDE, Popular IDEs for Python and Python installation on your machine. Introduction to Comments in Python In…
  • Introduction to Variables in Python & Variable Data Types
    What are Variables in Python In Python, variables act as references to reserved memory locations where actual values are stored. Each variable name points to a specific memory address that holds the assigned value. In this case: How Variable Memory Works When we define variables, like height = 175, Python allocates a specific memory location…
  • Understanding Number Datatype in Python: Decimal, Octal and Hexadecimal Numbers
    Understanding the Number Datatype in Python: A Comprehensive Overview Number datatype in Python is a combination of different number format classes like Integer, Float, and Complex. Python supports the Decimal, Octal decimal, and Hexadecimal number systems. Program – Integer Number datatype Output of the Integer Number datatype program: Note from the output: Exploring Float Number…
  • Understanding Scope in Python Programming (with Example Programs)
    In previous articles, we learned about following topics: Understanding Scope in Programming Generally, in the scope of a program, a variable can be declared only once with the same name. But, it’s possible to declare variables with the same name in different scopes within the same program. Changing the value of a variable within a…
  • Understanding Lists in Python: A Comprehensive Guide
    Lists in Python List is a core datatype in Python. Lists are sequences used to store elements (collection of data). Lists are similar to Arrays but are of dynamic size. It means the size of the list can be increased or decreased as we add or remove elements from the list. Understanding Python Lists: A…

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

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