Open In App

Python Numbers

In Python, "Numbers" is a category that encompasses different types of numeric data. Python supports various types of numbers, including integers, floating-point numbers, and complex numbers. Here's a brief overview of each:

Python Integer

Python int is the whole number, including negative numbers but not fractions. In Python, there is no limit to how long an integer value can be.

Example 1: Creating int and checking type

num = -8

# print the data type 
print(type(num))

Output:

<class 'int'>

Example 2: Performing arithmetic Operations on int type

a = 5
b = 6

# Addition
c = a + b
print("Addition:",c)

d = 9
e = 6

# Subtraction
f = d - e
print("Subtraction:",f)

g = 8
h = 2

# Division
i = g // h
print("Division:",i)

j = 3
k = 5

# Multiplication
l = j * k
print("Multiplication:",l)

m = 25
n = 5

# Modulus
o = m % n

print("Modulus:",o)

p = 6
q = 2

# Exponent
r = p ** q
print("Exponent:",r)

Output:

Addition: 11
Subtraction: 3
Division: 4
Multiplication: 15
Modulus: 0
Exponent: 36

Python Float

This is a real number with a floating-point representation. It is specified by a decimal point. Optionally, the character e or E followed by a positive or negative integer may be appended to specify scientific notation. . Some examples of numbers that are represented as floats are 0.5 and -7.823457.

They can be created directly by entering a number with a decimal point, or by using operations such as division on integers. Extra zeros present at the number's end are ignored automatically.

Example 1: Creating float and checking type

num = 3/4

# print the data type
print(type(num))

Output:

<class 'float'>

As we have seen, dividing any two integers produces a float. A float is also produced by running an operation on two floats, or a float and an integer.

num = 6 * 7.0

print(type(num))

Output:

<class 'float'>

Example 2: Performing arithmetic Operations on the float type

a = 5.5
b = 3.2

# Addition
c = a + b
print("Addition:", c)

# Subtraction
c = a-b
print("Subtraction:", c)

# Division
c = a/b
print("Division:", c)

# Multiplication
c = a*b
print("Multiplication:", c)

Output
Addition: 8.7
Subtraction: 2.3
Division: 1.71875
Multiplication: 17.6

Note: The accuracy of a floating-point number is only up to 15 decimal places, the 16th place can be inaccurate.

Python Complex

A complex number is a number that consists of real and imaginary parts. For example, 2 + 3j is a complex number where 2 is the real component, and 3 multiplied by j is an imaginary part.

Example 1: Creating Complex and checking type

num = 6 + 9j

print(type(num))

Output:

<class 'complex'>

Example 2: Performing arithmetic operations on complex type

a = 1 + 5j
b = 2 + 3j

# Addition
c = a + b
print("Addition:",c)

d = 1 + 5j
e = 2 - 3j

# Subtraction
f = d - e
print("Subtraction:",f)


g = 1 + 5j
h = 2 + 3j

# Division
i = g / h
print("Division:",i)


j = 1 + 5j
k = 2 + 3j

# Multiplication
l = j * k
print("Multiplication:",l)

Output:

Addition: (3+8j)
Subtraction: (-1+8j)
Division: (1.307692307692308+0.5384615384615384j)
Multiplication: (-13+13j)

Type Conversion in Python

We can convert one number into the other form by two methods:

Using Arithmetic Operations: 

We can use operations like addition, and subtraction to change the type of number implicitly(automatically), if one of the operands is float. This method is not working for complex numbers.

Example: Type conversion using arithmetic operations

a = 1.6
b = 5

c = a + b

print(c)

Output:

6.6

Using built-in functions

We can also use built-in functions like int(), float() and complex() to convert into different types explicitly.

Example: Type conversion using built-in functions

a = 2
print(float(a))

b = 5.6
print(int(b))

c = '3'
print(type(int(c)))

d = '5.6'
print(type(float(c)))

e = 5
print(complex(e))

f = 6.5
print(complex(f))

Output:

2.0
5
<class 'int'>
<class 'float'>
(5+0j)
(6.5+0j)

When we convert float to int, the decimal part is truncated. 

Note: 

  1. We can't convert a complex data type number into int data type and float data type numbers.
  2. We can't apply complex built-in functions on strings.

Decimal Numbers in Python

Arithmetic operations on the floating number can give some unexpected results. 

Example 1: Let's consider a case where we want to add 1.1 to 2.2. You all must be wondering why the result of this operation should be 3.3 but let's see the output given by Python.

a = 1.1
b = 2.2
c = a+b

print(c)

Output:

3.3000000000000003

Example 2: You can the result is unexpected. Let's consider another case where we will subtract 1.2 and 1.0. Again we will expect the result as 0.2, but let's see the output given by Python.

a = 1.2
b = 1.0
c = a-b

print(c)

Output:

0.19999999999999996

You all must be thinking that something is wrong with Python, but it is not. This has little to do with Python, and much more to do with how the underlying platform handles floating-point numbers. It’s a normal case encountered when handling floating-point numbers internally in a system. It’s a problem caused when the internal representation of floating-point numbers, which uses a fixed number of binary digits to represent a decimal number. It is difficult to represent some decimal numbers in binary, so in many cases, it leads to small roundoff errors. 

In this case, taking 1.2 as an example, the representation of 0.2 in binary is 0.00110011001100110011001100...... and so on. It is difficult to store this infinite decimal number internally. Normally a float object’s value is stored in binary floating-point with a fixed precision (typically 53 bits). So we represent 1.2 internally as,

1.0011001100110011001100110011001100110011001100110011  

Which is exactly equal to :

1.1999999999999999555910790149937383830547332763671875

For such cases, Python's decimal module comes to the rescue. As stated earlier the floating-point number precision is only up to 15 places but in the decimal number, the precision is user-defined. It performs the operations on the floating-point numbers in the same manner as we learned in school.

Related Article - floor() and ceil() function Python

Let's see the above two examples and try to solve them using the decimal number.

Example:

import decimal

a = decimal.Decimal('1.1')
b = decimal.Decimal('2.2')

c = a+b
print(c)

Output

3.3

We can use a decimal module for the cases - 

Note: For more information about decimal numbers in Python and the functions provided by this module, refer to Decimal Functions in Python

Article Tags :