A package is a pre-built set of functions and data types that can be loaded into a Python session to extend the language's functionality.
We can import a package using the command import package_name
.
When we import a package, we create a new namespace for the contents of that package. A namespace is effectively a naming system that helps us to avoid naming conflicts. We don't always know the names of every object in a package that we are importing, and there might be situations where an object in a package has a name that conflicts with an object we have previously defined. By default, the name of a namespace for a package is the same as the name of the package, and we access its contents using the following syntax:
package_name.object_name
math
Package¶Base Python includes many mathematical functions and operations, but there are several common mathematical functions that are missing. Several of these are included in the math
package.
import math
The math
packages contains functions the following functions (along with many others):
sqrt()
- Returns the square root of the argument. factorial()
- Returns the factorial of the argument.exp()
- Returns the value of e
raised to the argument.log()
- Returns the natural logarithm of the argument. An optional base
parameter can be specified to change the base. print('Square root of 20: ', math.sqrt(20))
print('10 factorial: ', math.factorial(10))
print('e raised to 5: ', math.exp(5))
print('Natural log of 100:', math.log(100))
print('Common log of 100: ', math.log(100, 10))
The math
package also contains variables. For example, it contains an object named pi
which stores the value of the constant pi
. This package also includes trig functions such as sin
, cos
, and tan
.
print(math.pi)
print(math.sin(math.pi/3))
print(math.cos(math.pi/3))
print(math.tan(math.pi/3))
If we are making frequent use of a certain package, it can b tedius to type its name every time you want to use an object from the package, especially if the name of the package is long. Python allows us to set an alias for a package when we import it. Setting an alias allows us to choose the name of the namespace associate with the package. The syntax for this is:
import package_name as alias
import math as mt
print(mt.pi)
print(mt.sin(mt.pi/3))
print(mt.cos(mt.pi/3))
print(mt.tan(mt.pi/3))
If we have an object that we know we will be using frequently, we can import it directly into the main namespace as follows:
from package_name import object_name
Note that this only imports the specified object. Nothing else from the package is imported.
from math import pi
print(pi)
print(sin(pi))
print(cos(pi))
We can import several objects with a single import statement.
from math import pi, sin
print(pi)
print(sin(pi))
print(cos(pi))
If we want to import all objects from a package into the primary namespace, we can do so with the following command:
from package_name import *
Be careful with this command. If there are any naming conflicts, objects that had been created previously might be over-written.
from math import *
print(pi)
print(sin(pi))
print(cos(pi))