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
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
Basic operations : The reference are been linked to wikipedia
a+b | ![]() | addition |
a-b | ![]() | subtraction |
a*b | ![]() | multiplication |
a/b | ![]() | division |
a//b | ![]() | floor division {only in Python 2.2 and higher versions} |
a%b | ![]() | modulo |
-a | ![]() | negation |
abs(a) | ![]() | absolute value |
a**b | ![]() | exponent |
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

Recent blog posts
- watir-webdriver web inspector
- gem list to gemfile
- Packing ruby2.0 on debian.
- Made it into The Guinness Book!
- to_h in ruby 2.0
- Filter elements by pattern jQuery.
- Better HTML password fields for mobile ?
- Grayscale image when user offline
- nth-child CSS pseudo-class Christmas colors
- EventEmitter in nodejs