Content:
- Introduction:
- math module:
- random module:
- statistics module:
Introduction:
Modules refer to a file containing Python statements and definitions. A file containing Python code, for example: hello.py , is called a module, and its module name would be hello .
math module:
math module is a inbuilt module in python and has a set of methods and constants. Some of the methods and constant are listed below.
In [3]:
# pi
import math
print(math.pi)
In [4]:
# e
import math
print(math.e)
In [26]:
# sqrt
import math
print(math.sqrt(78))
In [6]:
# celi
import math
print(math.ceil(3.14))
In [7]:
# floor
import math
print(math.floor(3.98))
In [8]:
# pow
import math
print(math.pow(2,3))
In [27]:
# fabs - this method returns the absolute value of a number, as a float
import math
print(math.fabs(-66.43))
In [11]:
# sin
import math
print(math.sin(45))
In [12]:
# cos
import math
print(math.cos(45))
In [13]:
#tan
import math
print(math.tan(45))
random module:
random moduleis a built-in module that you can use to make random numbers.
In [28]:
# random - Returns a random float number between 0 and 1
#syntax - random.random()
import random
print(random.random())
In [18]:
# randint - Returns a random number between the given range, both included
#syntax - random.randint(start, stop)
import random
print(random.randint(10,15))
In [19]:
# randrange - Returns a random number between the given range
# Syntax - random.randrange(start, stop, step)
import random
print(random.randrange(10,20,3))
statistics module:
it is a built-in module that can be used to calculate mathematical statistics of numeric data.
In [20]:
# mean - this method calculates the mean (average) of the given data set.
# syntax - statistics.mean(data)
import statistics
print(statistics.mean([-11, 5.5, -3.4, 7.1, -9, 22]))
In [23]:
# median - this method calculates the median (middle value) of the given data set. This method also sorts the data in ascending order before calculating the median.
#syntax - statistics.median(data)
#Note: If the number of data values is odd, it returns the exact middle value. If the number of data values is even, it returns the average of the two middle values.
# Import statistics Library
import statistics
# Calculate middle values
print(statistics.median([-11, 5.5, -3.4, 7.1, -9, 22]))+
In [25]:
# mode - This method calculates the mode (central tendency) of the given numeric or nominal data set.
# Syntax - statistics.mode(data)
# Import statistics Library
import statistics
# Calculate the mode
print(statistics.mode([1, 3, 3, 3, 5, 7, 7.9, 11]))
print(statistics.mode(['red', 'green', 'blue', 'red']))
Refrenences: https://www.w3schools.com/