Packages are a neat way to store collections of python scripts.

- They are essetially are "Directory of Python scripts".

- "Each script" is called a "Module".

- These "modules", specify functions, methods, types.

Thousands of packages available, these include several for data science:

`Numpy`

:*package for doing "Numerical Computation", such as "efficiently" working with "arrays".*

`Matplotlib`

:*a "data visualization" package for python.*

`Scikit-learn`

:*a "machine learning" package for python.*

Not all packages come packaged with python distribution, some of them have to be downloaded and installed manually. To do this following step should be followed:

- First, identify what package to use, this would be "package name", for instance (say)
`Numpy`

.

- Second, install
`pip`

, a python package management system.

Third, use the follwoing template to install desired packages:

`pip_version_number install < package name >`

for example,

`pip3 install numpy`

- Lastly, before you use the installed packages, you have to explicitly tell python about it, this is done by
*importing the package.*

This can be done in two ways:

- To import all the contents of a package:
`import < package name >`

- To import a specific method or module from the package:

`from < package name > import < function, method name or type name >`

Now to use a package, simply use this template:

`< package name > . < func, method or type name > ( object )`

This could be cumbersome sometimes, hence we can shorten the former statement by using an 'alias'. Using the templet mentioned below:

`import < package name > as < desired alias >`

e.g. `import numpy as np`

( here `np`

is an 'alias' ) then using the package like this,

`< alias name > . < func, method or type name >`

**Note: ***The pythonic way of using packages is to import using method 1 discussed formerly*, i.e. `import < package name >`

**RQ1: ***Which of the following is a package for installation and maintenance system for Python?*

**Ans: **`pip`

.

**RQ2: ***Which statement is the most common way to invoke the import machinery?*

**Ans: **`import`

.

**RQ3: **You import `Numpy`

as `foo`

as follows:

`import numpy as foo`

*Which Python command that used the array() function from Numpy is valid if Numpy is imported as foo?*

**Ans: **`foo.array( object )`

**RQ4: **You want to use Numpy's array() function. You need to decide whether to import this function as follows:

`from numpy import array`

or by imporitng the entire `numpy`

package:

`import numpy`

Select the two correct statements about these different import methods.

**Ans: **

1) The `from numpy import array`

version will make it less clear in the code that you're using Numpy's `array()`

function.

2) Using `import numpy`

will require you to use numpy.array(), making it clear that you're using a Numpy function.

**Objective:**

- Write Python to import different packages.

- Practise different ways to import packages

**Exercises:**

1) import package -- 100xp, status: earned.

2) Selective import -- 100xp, status: earned.

3) Different ways of importing -- 50xp, status: earned.

**1. Import Package:**

As a data scientist, some notions of geometry never hurt. Let's refresh some of the basics.

For a fancy clustering algorithm, you want to find the `circumference`

$C$ and `area`

$A$ of a circle. When the `radius`

of the circle is `r`

, you can calculate $C$ and $A$ as:

$C = 2πr$ and $A = πr^2$

**Note: **To use the constant `pi`

, you'll need the `math`

package.

```
In [3]:
```"""
Instructions:
+ Import the "math" package.
Now you can access the constant "pi" with "math.pi".
+ Calculate the circumference of the circle and store it in C.
+ Calculate the area of the circle and store it in A.
"""
# Definition of radius
r = 0.43
# Import the math package
import math
# Calculate C
C = 2 * math.pi * r
# Calculate A
A = math.pi * ( r ** 2 )
# Build printout
print("Circumference: " + str(C))
print("\nArea: " + str(A))

```
```

**2) Selective import:**

- General package import statement,
`import < package name >`

Selective pacakge import statement,

`from < package name > import < function, method, type or constant >`

```
In [8]:
```"""
Instructions :
+ Perform a selective import from the "math" package where,
you only import the "radians" function.
+ Cal:
- Distance travelled by the Moon over 12 degrees of its orbit.
- Assign the result to "dist".
- calculate this as r ∗ ϕ,
where r is the radius and ϕ is the angle in radians.
- To convert an angle in degrees to an angle in radians,
use the radians() function, which you just imported.
+ Print out "dist".
"""
# Definition of radius
r = 192500
# Import radians function of math package
from math import radians
# Travel distance of Moon if 12 degrees. Store in dist.
dist = r * radians( 12 )
# Print out dist
print("\nThe distance travelled by the moon over 12 degrees of its orbit is: " + str( dist ) + " Km" )

```
```

**3) Different ways of importing:**

There are several ways to import packages and modules into Python. Depending on the import call, you'll have to use different Python code.

- What if we want to be more specific about the package we want to use?

Suppose we want to use a "function" (say) `x`

but with an "alias" of your own, which is in the `y`

"subpackage" of the `z`

package. We will use the following template:

`from < package_name > . < sub-package_name > import < func, method, type or constant > as < desired alias >`

Here's and example:

`from scipy.linalg import inv as my_inv`