4  Numbers

Python has a couple of different options for data types when it comes to working with numbers. Numbers without and with demical points are refered to as int (integers) and float (floating point). So, depending upon how we initialize a variable, its data type would be assigned accordingly. Both these data types can have negative values. The int and float functions can be used to explicity assign the corresponding data types. Note, when assigning and int data type to a number with decimal value, only the interger part is retained. For working with complex numbers there is an additional data type complex.

x = 5 
y = 5.0
print(type(x))
print(type(y))
a = float(x)
b = int(y)
print(a)
print(b)
<class 'int'>
<class 'float'>
5.0
5

A numeric variable can also be initiatlized using scientific notation i.e. e notation. The e can be both lower or upper case. Variables declared with a scientific notation are always float.

z = 5e3
print(z)
5000.0

The data type for the output of an arithmetic operator would depend on the data type of the operands. If both the operands are int then the output would also be int. An exception to this is / (division) where the data type for the output is always float irrespective of the data type of the operands. Also, if at least one of the operand is float then the data type for the output would be float.

print(type(4*2))
print(type(4/2))
<class 'int'>
<class 'float'>

4.1 Formatted print

To print a floating point number with a desired number of decimal places we can use the formatted print option. The %f format specifier can be used to indicate the required number of decimal places for the output. Here, we also have an option to include the sign (+) in case of positive number. In addition to this, the round function can also be used which takes two arguments – the floating point number and the required number of decimal places.

n = 1234.5678

Formatted print           Output
————————————————————————————————
print("%.2f" % n)         1234.57
print("%+.2f" % n)        +1234.57
print("%.2e" % n)         1.23e+03
print("%.d" % n)          1234

4.2 f-strings

From python 3.6 onwards, formatting of variable, calculations of mathematical expression etc. can be easily done within the print statement. These string starts with f. The variables inside the f-strings are enclosed within curly braces {}. The formatting is similar to as we described above, the code is relatively cleaner and easy to follow.

a = -4
b = 5.0
print(f'The product of {a} and {b} is {a*b}')
print(f'The product of {a} and {b} is {a*b:0.3f}')
The product of -4 and 5.0 is -20.0
The product of -4 and 5.0 is -20.000

4.3 Complex numbers

Complex numbers can be initialized using the complex function which takes either one or two arguments, depending upon the nature of argument: - a complex number is specified directly - the real and imaginary parts are specified as separate arguments.

c = complex(3+6j)
z = complex(5,2)
print(c, z)
print(f"The real component of z is {z.real}")
print(f"The imaginary component of z is {z.imag}")
print(f"The conjugate of z is {z.conjugate()}")
(3+6j) (5+2j)
The real component of z is 5.0
The imaginary component of z is 2.0
The conjugate of z is (5-2j)