Python Numbers, Type Conversion and Mathematics#

Learn about the different numbers used in Python, how to convert from one data type to the other, and the mathematical operations supported in Python.

Number Data Type in Python#

Integers, floating point numbers and complex numbers fall under Python numbers category. They are defined as int, float and complex classes in Python.

Integers and floating points are separated by the presence or absence of a decimal point. For instance, 5 is an integer whereas 5.0 is a floating-point number.

Complex numbers are written in the form, x + yj, where x is the real part and y is the imaginary part.

We can use the type() function to know which class a variable or a value belongs to. Similarly, the isinstance() function is used to check if an object belongs to a particular class.

Let’s look at an example:

a = 6

print(type(a)) # type integer

print(type(5.0))  # type float

c = 6 + 3j
print(c + 3)

print(isinstance(c, complex))
<class 'int'>
<class 'float'>
(9+3j)
True

While integers can be of any length, it is only limited by the memory available.

The numbers we deal with every day are of the decimal (base 10) number system. But computer programmers (generally embedded programmers) need to work with binary (base 2), hexadecimal (base 16) and octal (base 8) number systems.

In Python, we can represent these numbers by appropriately placing a prefix before that number. The following table lists these prefixes.

Number System

Prefix

Binary

‘0b’ or ‘0B’

Octal

‘0o’ or ‘0O’

Hexadecimal

‘0x’ or ‘0X’

# Example 1:

# Output: 1
print(0b0001)  # 0001 in binary is 1 in decimal and i have use prefix '0b'

# Output: 10  
print(0o12)    # 12 in octadecimal is 10 in decimal and i have use prefix '0o'

# Output: 10
print(0x000A)  # 000A in hexadecimal is 10 in decimal and i have use prefix '0x'
1
10
10
# Example 2:

# Output: 107
print(0b1101011)

# Output: 253 (251 + 2)
print(0xFB + 0b10) # adding Hex numbers with binary numbers.

# Output: 13
print(0o15)
107
253
13

Type Conversion#

We can convert one type of number into another. This is also known as coercion.

Operations like addition, subtraction coerce integer to float implicitly (automatically), if one of the operands is float.

1 + 3.0  # adding integer number '1' and a float number '3.0'. The answer will be flaot number '4.0'.
4.0

We can see above that 1 (integer) is coerced into 1.0 (float) for addition and the result is also a floating point number.

We can also use built-in functions like int, float and complex to convert between types explicitly. These functions can even convert from strings.

int(6.3)
6
int(-2.7)
-2
float(9)
9.0
a = 1234567890123456789
print (a)

b = 0.1234567890123456789 # total of only 17 numbers after decimal can be printed.
print (b)

c = 1+2j
print (c)
1234567890123456789
0.12345678901234568
(1+2j)

When converting from float to integer, the number gets truncated (decimal parts are removed).

complex('3+6j')
(3+6j)

Python Decimal#

Python built-in class float performs some calculations that might amaze us. We all know that the sum of 1.1 and 2.2 is 3.3, but Python seems to disagree.

(1.1 + 2.2) == 3.3  # The answer should be True but...
False

What is going on?

It turns out that floating-point numbers are implemented in computer hardware as binary fractions as the computer only understands binary (0 and 1). Due to this reason, most of the decimal fractions we know, cannot be accurately stored in our computer.

Let’s take an example. We cannot represent the fraction 1/3 as a decimal number. This will give 0.33333333… which is infinitely long, and we can only approximate it.

It turns out that the decimal fraction 0.1 will result in an infinitely long binary fraction of 0.000110011001100110011… and our computer only stores a finite number of it.

This will only approximate 0.1 but never be equal. Hence, it is the limitation of our computer hardware and not an error in Python.

1.1 + 2.2
3.3000000000000003

To overcome this issue, we can use the decimal module that comes with Python. While floating-point numbers have precision up to 15 decimal places, the decimal module has user-settable precision.

Let’s see the difference:

import decimal

print(0.1)

print(decimal.Decimal(0.1))
0.1
0.1000000000000000055511151231257827021181583404541015625

This module is used when we want to carry out decimal calculations as we learned in school.

It also preserves significance. We know 25.50 kg is more accurate than 25.5 kg as it has two significant decimal places compared to one.

from decimal import Decimal as D

print(D('1.1') + D('2.2'))

print(D('1.2') * D('2.50'))
3.3
3.000

Notice the trailing zeroes in the above example.

We might ask, why not implement Decimal every time, instead of float? The main reason is efficiency. Floating point operations are carried out must faster than Decimal operations.

When to use Decimal instead of float?#

We generally use Decimal in the following cases.

  1. When we are making financial applications that need exact decimal representation.

  2. When we want to control the level of precision required.

  3. When we want to implement the notion of significant decimal places.

Python Fractions#

Python provides operations involving fractional numbers through its fractions module.

A fraction has a numerator and a denominator, both of which are integers. This module has support for rational number arithmetic.

We can create Fraction objects in various ways. Let’s have a look at them.

import fractions

print(fractions.Fraction(1.5))

print(fractions.Fraction(9))

print(fractions.Fraction(1,6))
3/2
9
1/6

While creating Fraction from float, we might get some unusual results. This is due to the imperfect binary floating point number representation as discussed in the previous section.

Fortunately, Fraction allows us to instantiate with string as well. This is the preferred option when using decimal numbers.

import fractions

# As float
# Output: 2476979795053773/2251799813685248
print(fractions.Fraction(1.1))  # 1.1 is a number

# As string
# Output: 11/10
print(fractions.Fraction('1.1'))  #'1.1' is a string and not a number
2476979795053773/2251799813685248
11/10

This data type supports all basic operations. Here are a few examples.

from fractions import Fraction as F  # I changed my Fraction module name to "F"

print(F(1.3) + F(1.3))

print(F(1, 3) + F(1, 3))

print(1 / F(5, 6))

print(F(-3, 10) > 0)

print(F(-3, 10) < 0)
5854679515581645/2251799813685248
2/3
6/5
False
True

Python Mathematics#

Python offers modules like math and random to carry out different mathematics like trigonometry, logarithms, probability and statistics, etc.

import math

print(math.pi)

print(math.cos(math.pi)) # cos(pi) = -1

print(math.exp(10))

print(math.log10(1000)) # log10(1000) = 3

print(math.sinh(1))

print(math.factorial(6))
3.141592653589793
-1.0
22026.465794806718
3.0
1.1752011936438014
720
import random

print(random.randrange(10, 20))

x = ['a', 'b', 'c', 'd', 'e'] # x is a list class of variable and it has 5 elements.

# Get random choice
print(random.choice(x))

# Shuffle x
random.shuffle(x)

# Print the shuffled x
print(x)

# Print random element
print(random.random())
15
b
['d', 'c', 'e', 'a', 'b']
0.8036360430009386

When we run the above program we get the output as follows. (Values may be different due to the random behavior)