Along with Strings,Python can also do numerical calculation. But first, let's know how numbers are manipulated by Python.
Numbers are of two types:
Just as we do 1+2=3 in math, we can do the same in Python. How?
Using operators
Operators are constructs that behave like functions but which differ syntactically and semantically from real math functions.
In simpler words,
Operators are Python's math and logic symbols to do calculations.
There are two kinds of operators in Python: Arithmetic and Logical.
Arithmetic operators are as follows:
Lets look at some examples:
#Lets ask Python what is 2+2print(2+2)It returns:
4You could change the numbers or the above examples to get different results.
Now, let's try something more fun:
#PEMDAS with Pythonprint(2*3+(4+5)//7)returns,
7Now follow through the logic:
The logic is instead PEDMAS instead of PEMDAS. Python went about solving the problem like this,
2*3+(4+5)//72*3+9//72*3+16+17That should help in learning about operators.
The Math module is used to do complicated stuff like pi, e, and square roots. But, the Math module must be imported when you want to use it. For instance,
import mathp=math.piprint(p*2**2) #we are basically finding the value of 4piThis returns,
12.566370614359172To take a look at all the functions in the math module, go to this link.
a. 2**2+(4+5)*8
b. Find the area of a circle whose radius is 5(Hint: Import math module)
c.Use floor division or the modulo operator to find whether 246078 is a multiple of 7 and 3.