Title: Mathematical Operations
Slug: math_operations
Summary: Mathematical Operations
Date: 2016-05-01 12:00
Category: Python
Tags: Basics
Authors: Chris Albon

Import the math module


In [5]:
import math

Display the value of pi.


In [6]:
math.pi


Out[6]:
3.141592653589793

Display the value of e.


In [7]:
math.e


Out[7]:
2.718281828459045

Sine, cosine, and tangent


In [8]:
math.sin(2 * math.pi / 180)


Out[8]:
0.03489949670250097

Exponent


In [9]:
2 ** 4, pow(2, 4)


Out[9]:
(16, 16)

Absolute value


In [10]:
abs(-20)


Out[10]:
20

Summation


In [11]:
sum((1, 2, 3, 4))


Out[11]:
10

Minimum


In [12]:
min(3, 9, 10, 12)


Out[12]:
3

Maximum


In [13]:
max(3, 5, 10, 15)


Out[13]:
15

Floor


In [14]:
math.floor(2.949)


Out[14]:
2

Truncate (drop decimal digits)


In [15]:
math.trunc(32.09292)


Out[15]:
32

Truncate (integer conversion)


In [16]:
int(3.292838)


Out[16]:
3

Round to an integrer


In [17]:
round(2.943), round(2.499)


Out[17]:
(3, 2)

Round by 2 digits


In [18]:
round(2.569, 2)


Out[18]:
2.57