Easy Math with Python

Simple math with python , is as easy as counting your fingers ;0)

Basic operations : The reference are been linked to wikipedia

a+b a + b\, addition
a-b a - b\, subtraction
a*b a \times b\, multiplication
a/b a \div b\, division
a//b a \div b\, floor division {only in Python 2.2 and higher versions}
a%b a \bmod b\, modulo
-a -a\, negation
abs(a) |a|\, absolute value
a**b a^b\, exponent
sqrt(a) \sqrt{a}\, square root


To use sqrt( ) , we need to explicitly import the function , to do so we follow this :

from
math import sqrt

You can also use the round () as , round( the number , number of decimal places to be rounded)


Examples :

>>> from math import sqrt

>>> sqrt(9)
3.0

>>> 3+5
8

>>> 5-6
-1

>>> -0
0

>>> 6*8
48

>>> 1**2
1

>>> 2**1
2

>>> 2**2
4

>>> 2/3
0

>>> 3/2
1

>>> 0/0
Traceback (most recent call last):
File "", line 1, in ?
ZeroDivisionError: integer division or modulo by zero

>>> 3/0
Traceback (most recent call last):
File "", line 1, in ?
ZeroDivisionError: integer division or modulo by zero

>>> 6/8
0

>>> 6//8
0

>>> 7/3
2

>>> 7//3
2

>>> .6/.2
2.9999999999999996

>>> .6//.2
2.0

>>> round(2.9,10)
2.8999999999999999

>>> 22/7
3

>>> 22 // 7
3

>>> 22/7
3

>>> 3.45 * 5
17.25

>>> 4-5
-1

>>> abs(-9)
9

>>> 9%7
2 Share this