Work of numbers and basic Math functions - Python

Here we will learn about numbers and mathematical expressions in Python.
Math is too easy with Python.
So lets start it now
For addition of two numbers simply use those numbers as we take them in math books, for example to add 1 into 1 in python shell type 1+1 and press enter, it gives output 2, 2+2 will give 4, 1+1+1 gives output 3 try more by your own for practice.

>>> 1+1
2
>>> 2+2+2
6
>>> 2+2+2+2
8

Same easy method for subtraction

>>> 9-3
6
>>> 9-1-1
7

Division

>>> 9/3
3

Here is one notable thing in division, what about those number who give answers in decimals like 2.9843345 etc. Because by default python does not give output in such situations in decimals, python will exclude decimal figures from such outputs.
For such divisions where we want answer in decimal points we have to include decimal point with first decimal zero with any number who is going to divide.

For example normally the output of 18/7 will be 2 and we know that output should not be only two, if we want full answer in decimals we have to include a decimal point with any of the number with either 18 or with 7, as 18.0/7 or 18/7.0 or also can be 18.0/17.0 or it can also work as 18./7 or 18./7. or 18/7.

>>> 18/7
2
>>> 18.0/7.0
2.5714285714285716 <-- Answer in decimals is called a float
>>> 18./7.
2.5714285714285716 <-- Answer in decimals is called a float

Modulus (Modulus is the remainder of any division, for Example a % b gives the remainder after dividing a by b. For example, 11 % 4 is 3, since 11 = 4 * 2 + 3.)

>>> 9%4
1 <-- Division remainder
>>> 6%2
0 <-- Division remainder

Can get Modulus of decimals, decimals in the language of computers are called float (2.323, 4.5656, 1.01 etc are floats)

>>> 4.55%2.30
2.25 <-- Output is a float

Square/exponent
In python 2 exponent 3 is 2x2x2=8 and is written as 2*3 in programming

>>> 2*3
8
>>> 6*7
42
>>> -2*3
-6

We can also do 2*2*2 which will give output 8 or 2*2*2 can also be written as 2**2 output will be same 8
>>> 2*2*2
8

Comments

Post a Comment

Share post using share buttons or leave a comment