Python Numbers


Python Numbers

In Python, there are three common data types used to store numbers:

  • Integer (int)
  • Float (float)
  • Complex (complex)
Python Numbers

Integer

An integer is a number without decimal places, or we can say that integers are whole numbers. Integers can be positive, as well as negative. They can be of any length, the only limitation for length is the memory of the system.

We can use underscores(_) anywhere in between the long numbers to make them easier to read. The number will still be treated as an integer.

Type function will return class ‘int’.

Positive integer.

In [1]:
temperature = 65
print(temperature)
print(type(temperature))
65
<class 'int'>

Integer with underscore(s).

In [2]:
price = 786_123_496
print(price)
print(type(price))
786123496
<class 'int'>

Negative integer.

In [3]:
temperature = -65
print(temperature)
print(type(temperature))
-65
<class 'int'>

Long integer.

In [4]:
long_number = 123456789123456789123456789123456789123456789123456789
print(long_number)
print(type(long_number))
123456789123456789123456789123456789123456789123456789
<class 'int'>

Float

A floating-point number (float) is a number with a decimal place. They can also be either positive or negative and can be of any length. Floating-point numbers can also contain “e”, “e” is a scientific notation that means “10 to the power of”. For example, 1E17 means 1 * 1017.

To make them easier to read, we can also use underscores (_) anywhere in between the long numbers, even after the decimals. The number will still be treated as a float.

Type function will return class ‘float’.

Positive float.

In [1]:
temperature = 65.5
print(temperature)
print(type(temperature))
65.5
<class 'float'>

Float with underscore(s).

In [2]:
price = 786_123_496.23_14
print(price)
print(type(price))
786123496.2314
<class 'float'>

Negative float.

In [3]:
temperature = -65.5
print(temperature)
print(type(temperature))
-65.5
<class 'float'>

Long float.

In [4]:
long_number = 123456789123456789123456789123456789123456789123456789.3659874
print(long_number)
print(type(long_number))
1.2345678912345678e+53
<class 'float'>

Float with “e”

In [5]:
length = 12E3
print(length)
print(type(length))
12000.0
<class 'float'>

Complex

Complex numbers are numbers with a real part and an imaginary part. They are not used in our day-to-day calculations, mostly used by expert mathematicians.

In a complex number, j is appended with the imaginary part. Example 4+7j, here, in this number, 4 is a real number and 7 is an imaginary number.

Type function will return class ‘complex’.

Positive complex number.

In [1]:
x = 4+5j
print(x)
print(type(x))
(4+5j)
<class 'complex'>

[variable].real is used to fetch the real part.

In [2]:
print(x.real)
4.0

[variable].imag is used to fetch the imaginary part.

In [3]:
print(x.imag)
5.0

complex() function is used to create complex number. It has two arguments, first is real number and second is imaginary number.

In [4]:
y = complex(4,5)
print(y)
print(type(y))
(4+5j)
<class 'complex'>

Number Type Conversion

We can convert data type of number from one type to other type usign these methods:

  1. int() – To convert to integer
  2. float() – To convert tot floating-point number.
  3. complex() – To convert a number to complex number

int() – Converting float to int.

In [1]:
x = 5.5
a = int(x)

print(a)
print(type(a))
5
<class 'int'>

float() – Converting int to float.

In [2]:
x = 5
a = float(x)

print(a)
print(type(a))
5.0
<class 'float'>

complex() – Converting int to complex.

In [3]:
x = 5
a = complex(x)

print(a)
print(type(a))
(5+0j)
<class 'complex'>

Number Functions

Here are a few built-in functions, that can be used with the numbers.

FunctionDescriptionExampleOutput
min(a,b,…)Returns the minimum of the sequence.min(5,10,6)5
max(a,b,….)Returns the maximum of the sequence.max(5,10,6)10
abs(a)Returns the (positive) distance between a and 0.abs(-5.6)5.6
pow(a,b)Returns a ** b.pow(2,3)8
round(a[,n])The value of a is rounded to n digits.round(5.236,2)5.24

Math Module

Python has math module for the different mathematical operations on the numbers like trigonometry, logarithms, etc. Here are the few of the functions from the maths module:

FunctionDescriptionExampleOutput
math.sqrt(a)Returns the square root of a.math.sqrt(4)2.0
math.floor(a)The floor value of a, i.e., the greatest integer that is less than a.math.floor(5.6)5
math.ceil(a)The ceiling value of a, i.e., the smallest integer that is not less than a.math.ceil(5.6)6
math.exp(a)The exponent of a.math.exp(2)7.38905609893065
math.fabs(a)The absolute value of a.math.fabs(-5.28)5.28
math.log(a)The natural log value of a.math.log(20)2.995732273553991
math.log10(a)The base 10 log of a is returned.math.log10(20)1.3010299956639813
math.modf(a)A tuple is returned containing the fractional and integer parts of a floating-point number. The integer part is also returned as a float.math.modf(2.56)(0.56, 2.0)

Random Module

The random module is used to generate the random numbers, select random elements, etc. Though random module is too vast, but here are a few methods of random module:

To generate a random number between 0 and 1.

In [1]:
import random
print(random.random())

To select a randome item from the string, list, etc

In [2]:
import random
 
name = 'Logical Python'
print(random.choice(name))

Decimal Numbers in Python

Internally in the code, the decimal numbers are not represented as we think. When we try to perform some arithmetic operations on the decimals, we see unexpected results.

Why we get 3.3000000000000003 ?

In [1]:
x = 1.1
y = 2.2

z = x + y
print(z)
3.3000000000000003

This does not happens with all numbers.

In [2]:
x = 2.2
y = 3.3

z = x + y
print(z)
5.5

Floating-Point Representation Limitations:

This discrepancy is due to the way floating-point numbers are stored in the computer’s memory. The limitations of representing certain decimal fractions in the binary floating-point format.

Python uses the IEEE 754 standard for floating-point arithmetic, which represents numbers in binary. However, not all decimal fractions can be precisely represented in binary, leading to rounding errors.

Binary Representation of 1.1 and 2.2:

1. The decimal number 1.1 cannot be represented precisely in binary, just like the fraction 1/3 cannot be represented precisely as a decimal (0.333… recurring). So, when 1.1 is converted to binary, it becomes an infinitely repeating binary fraction.

1.0001100110011001100110011001100110011001100110011...

2. Similarly, the decimal number 2.2 cannot be represented exactly in binary and has a repeating binary fraction.

10.001100110011001100110011001100110011001100110011...

3. When you add these two binary representations (1.1 and 2.2), you get a binary number that is very close to, but not exactly equal to, the binary representation of 3.3.

11.0100110011001100110011001100110011001100110011001...

4. Due to the limitations of the binary floating-point representation, the result is a number that is not precisely 3.3.

Decimal Module

If you need precise decimal arithmetic, you can use the decimal module in Python, which provides a Decimal data type that avoids these rounding errors:

In [1]:
import decimal
 
x = decimal.Decimal('1.1')
y = decimal.Decimal('2.2')
 
z = x+y
print(z)
3.3

References

  1. Built-in Types
  2. Python Numbers
  3. Basics about Numbers