Nitin Raturi
Nitin Raturi's Software Engineering Blog

Follow

Nitin Raturi's Software Engineering Blog

Follow
Python Number Data Type: A Comprehensive Guide

Photo by Mika Baumeister on Unsplash

Python Number Data Type: A Comprehensive Guide

Nitin Raturi's photo
Nitin Raturi
ยทMar 25, 2023ยท

8 min read

Table of contents

In Python, numbers represent numerical data types that can be used for mathematical operations. Numbers are immutable data types, which means that their values cannot be changed once created.

Numbers in python refers to the numeric values used in Python programming. It includes integers, floating-point numbers, and complex numbers. Python provides built-in functions to perform basic operations and type conversions on numbers, as well as convert them to different number systems.

Three Types of Numbers in Python

As we mentioned earlier, Python has support for three different types of numbers:

  • Integers

  • Floating-point numbers

  • Complex numbers

In the next sections, we'll discuss each of these types of numbers in detail.

๐Ÿ’ก Complete Python Roadmap for beginners and experts.

Integers in Python

Integers are whole numbers, positive or negative, without a decimal point. In Python, integers have unlimited precision, which means that they can be as large or as small as necessary.

Basic Operations with Integers

Python provides support for several basic arithmetic operations with integers, including addition, subtraction, multiplication, and division. Here's an example:

a = 10
b = 5
print(a + b) # Output: 15
print(a - b) # Output: 5
print(a * b) # Output: 50
print(a / b) # Output: 2.0

Type Conversion for Integers

Python allows you to convert integers to other types of numbers. For example, you can convert an integer to a floating-point number using the float() function. Here's an example:

a = 10
b = float(a)
print(b) # Output: 10.0

Floating-Point Numbers in Python

Floating-point numbers, also known as floats, are numbers with a decimal point. In Python, floating-point numbers are represented using the float data type.

Basic Operations with Floating Point Numbers

Python provides support for several basic arithmetic operations with floating-point numbers, including addition, subtraction, multiplication, and division. Here's an example:

a = 3.14
b = 2.0
print(a + b) # Output: 5.14
print(a - b) # Output: 1.14
print(a * b) # Output: 6.28
print(a / b) # Output: 1.57

Type Conversion for Floating Point Numbers

Python allows you to convert floating-point numbers to other types of numbers. For example, you can convert a floating-point number to an integer using the int() function. Here's an example:

a = 3.14
b = int(a)
print(b) # Output: 3

Complex Numbers in Python

Complex numbers are numbers that have both real and imaginary parts. In Python, complex numbers are represented using the complex data type.

Basic Operations with Complex Numbers

Python provides support for several basic arithmetic operations with complex numbers, including addition, subtraction, multiplication, and division. Here's an example:

a = 2 + 3j
b = 1 + 2j
print(a + b) # Output: (3+5j)
print(a - b) # Output: (1+1j)
print(a * b) # Output: (-4+7j)
print(a / b) # Output: (1.6-0.2j)

Type Conversion for Complex Numbers

Python allows you to convert complex numbers to other types of numbers. For example, you can convert a complex number to a floating-point number using the float() function. Here's an example:

a = 2 + 3j
b = float(a)
print(b) # Output: TypeError: can't convert complex to float

Since complex numbers cannot be converted to a floating-point number directly, you need to convert the real or imaginary part of the complex number first.

Basic Operations with Numbers

Python provides support for several basic arithmetic operations with numbers. Here's a summary of the basic operations:

OperatorDescription
+Addition
-Subtraction
*Multiplication
/Division
%Modulus (returns the remainder of a division)
//Floor division (returns the quotient of a division, rounded down to the nearest integer)
**Exponentiation

Here's an example:

a = 10
b = 3
print(a + b) # Output: 13
print(a - b) # Output: 7
print(a * b) # Output: 30
print(a / b) # Output: 3.3333333333333335
print(a % b) # Output: 1
print(a // b) # Output: 3
print(a ** b) # Output: 1000

Python Number to Binary and Other Conversions

Python provides built-in functions to convert numbers to binary and other number systems like decimal and hexadecimal. Let's discuss these functions in detail:

Binary to Decimal Conversion

To convert a binary number to a decimal number, you can use the int() function. The int() function takes two arguments, the first argument is the binary number, and the second argument is the base of the number system.

Since we want to convert a binary number to a decimal number, the base would be 2.

binary_number = "1101"
decimal_number = int(binary_number, 2)
print(decimal_number) # Output: 13

In the above example, we converted the binary number "1101" to a decimal number 13.

Decimal to Binary Conversion

To convert a decimal number to a binary number, you can use the bin() function. The bin() function takes a decimal number as an argument and returns a binary number in string format.

decimal_number = 13
binary_number = bin(decimal_number)
print(binary_number) # Output: 0b1101

In the above example, we converted the decimal number 13 to a binary number "1101".

Hexadecimal to Decimal Conversion

To convert a hexadecimal number to a decimal number, you can use the int() function. The int() function takes two arguments, the first argument is the hexadecimal number, and the second argument is the base of the number system.

Since we want to convert a hexadecimal number to a decimal number, the base would be 16.

hexadecimal_number = "1A"
decimal_number = int(hexadecimal_number, 16)
print(decimal_number) # Output: 26

In the above example, we converted the hexadecimal number "1A" to a decimal number 26.

Decimal to Hexadecimal Conversion

To convert a decimal number to a hexadecimal number, you can use the hex() function. The hex() function takes a decimal number as an argument and returns a hexadecimal number in string format.

decimal_number = 26
hexadecimal_number = hex(decimal_number)
print(hexadecimal_number) # Output: 0x1a

In the above example, we converted the decimal number 26 to a hexadecimal number "1a".

Decimal to Octal Conversion

To convert a decimal number to an octal number, you can use the oct() function. The oct() function takes a decimal number as an argument and returns an octal number in string format.

decimal_number = 9
octal_number = oct(decimal_number)
print(octal_number) # Output: 0o11

In the above example, we converted the decimal number 9 to an octal number "11".

Octal to Decimal Conversion

To convert an octal number to a decimal number, you can use the int() function. The int() function takes two arguments, the first argument is the octal number, and the second argument is the base of the number system.

Since we want to convert an octal number to a decimal number, the base would be 8.

octal_number = "11"
decimal_number = int(octal_number, 8)
print(decimal_number) # Output: 9

In the above example, we converted the octal number "11" to a decimal number 9.

Decimal to Base 36 Conversion

To convert a decimal number to a base 36 number, you can use the base_repr() function. The base_repr() function takes two arguments, the first argument is the decimal number, and the second argument is the base of the number system.

Since we want to convert a decimal number to a base 36 number, the base would be 36.

decimal_number = 12345
base36_number = base_repr(decimal_number, 36)
print(base36_number) # Output: 9IX

In the above example, we converted the decimal number 12345 to a base 36 number "9IX".

These are some of the other number conversions you can perform using built-in functions in Python. By utilizing these functions, you can easily convert numbers to various number systems and perform different mathematical operations on them.

FAQ

Is number an immutable data type in Python?

Yes, numbers are immutable data types in Python, which means that once a number is created, it cannot be changed.

How do you divide numbers in Python?

To divide two numbers in Python, you can use the / operator. If you want to perform floor division, you can use the // operator.

Converting Numbers to Words

You can convert a number to words using the num2words library. Here's an example:

pip install num2words
from num2words import num2
a = 123
b = num2words(a)
print(b) # Output: one hundred and twenty-three

String to Integer Conversion

Python also provides support for converting strings to integers. Here's an example:

a = "10"
b = int(a)
print(b) # Output: 10

Note that if the string contains non-numeric characters, a ValueError exception is raised.

Declaring Numbers in Python

Declaring numbers in Python is easy. You can assign a number to a variable like this:

x = 10
y = 3.14
z = 2 + 3j

In this example, we assigned the integer 10 to the variable x, the floating-point number 3.14 to the variable y, and the complex number (2+3j) to the variable z. Python automatically assigns the appropriate data type based on the value assigned to the variable.

In summary, with a solid understanding of the Python number data type, you can start building more complex programs that involve numerical computations.

You can also explore these fundamental python topics:

Did you find this article valuable?

Support Nitin Raturi by becoming a sponsor. Any amount is appreciated!

Learn more about Hashnode Sponsors
ย 
Share this