01-03 Numeric Types

01 - 03 Numeric Types¶

In python there are basically 3 built-in Numeric Data-Types

• int
• float
• complex

01 - 03.01 int¶

Integers are the most basic numeric type in python. Any number that does not contain a decimal point is considered as an integer and is of type int.

An interesting difference between the int type in python and other languages is that they are variable precision. This means that the integer computation on other languages will overflow at either $2^{31}$ or at $2^{63}$ (based on whether you have 32 bit or 64 bit CPU architecture). However in python, you can perform computation on numbers that are larger than these. (Actually the precision of a number can increase till your compute runs out of memory)

Lets test it

In [1]:
# this will crash on C
2 ** 120

Out[1]:
1329227995784915872903807060280344576

01 - 03.02 float¶

Floating point numbers contain the numbers with decimal point.. that is they can hold the result of fractions or exponential numbers.

Any integer can be converted to float by passing it to the float constructor

In [2]:
float(2)

Out[2]:
2.0
In [3]:
2e4

Out[3]:
20000.0

01 - 03.03 Complex¶

Complex numbers are the numbers with real and imaginary parts. Just like int or float, we can construct a complex number by passing the arguments to complex constructor

In [6]:
c_val = complex(2, 3)
print(c_val)

(2+3j)

In [7]:
c_val.real

Out[7]:
2.0
In [8]:
c_val.imag

Out[8]:
3.0
In [10]:
c_val.conjugate() # complex conjugate of c_val

Out[10]:
(2-3j)

Magnitude of a complex number:

i.e. $\sqrt{(c.real^2 + c.imag^2)}$

can be obtained by using abs function

In [11]:
abs(c_val)

Out[11]:
3.605551275463989

We shall take a deeper look at these datatypes and the operations that can be performed on them in the Operators module.