• 6 heures
  • Facile

Ce cours est visible gratuitement en ligne.

course.header.alt.is_video

course.header.alt.is_certifying

J'ai tout compris !

Mis à jour le 21/01/2021

More functions with modules and packages

Connectez-vous ou inscrivez-vous gratuitement pour bénéficier de toutes les fonctionnalités de ce cours !

Let's assume that you need to calculate the square root of a number for one of your programs. There is no existing square root function in Python. You could write one, but there have been a lot of people before you in the exact same position. Guess what? One of them already created a function and stored it in a module!

What are modules in Python?

A module is like a code library: a file containing a set of functions, classes, and variables you want to include in your application.

For instance, if you're working on a geometry application, you may need some:

  • Classes:

    • A square with its side's length as attribute.

    • A triangle with the length of its three sides as attributes.

    • A circle defined by its radius.

    • Etc.

  •  Variables:

    • PI: constant, useful to calculate circle area, etc.

    • Phi: constant, represents the golden ratio.

  • Functions:

    • Area with calculations dependent upon an object (square, triangle, etc.).

    • Angles which calculate angles of a triangle based on the length of its sides.

    • Etc.

You could write/define all these in your notebook, or you could store them in a Python file, and then import them in your notebook, which is called a module!

Our first module
Our first module

 Use the import keyword to import the geometry module:

import geometry

Now you can use your functions, classes, etc. in your notebook:

sq = geometry.square(4)
tri = geometry.triangle(3, 6, 5)
print(geometry.pi) # -> 3.14159265359
geometry.area(sq) # -> 16

All of the functions, variables, classes, etc. included in the geometry module can be used with  moduleName.function()  or  moduleName.class(). If you don't want to write geometry every time you want to use a geometry function, you have two options: 

from geometry import * # -> we can use area() or access pi directly
# OR :
import geometry as geo # we can now use geo.are() or geo.pi

You can also import a specific function from a module and use it like any other Python native function (print, len, etc.):

from geometry import pi
print(pi) # -> 3.14159265359

When one module isn't enough: the packages

A package is a collection of Python modules. While a module is a single Python file, a package is a directory containing an additional __init__.py file. This distinguishes it from a directory that contains a number of scripts.

For instance, you could have stored your geometry in three different files:

  • One for the classes: classes.py

  • One for the variable: variables.py

  • One for the functions: function.py

In this case, you would have the following directory:

Our geometry package
Geometry package

You have to use the . operator to access a module after importing the whole package:

import geometry # import all the geometry package
print(geometry.variables.pi) # -> 3.1415...
sq = geometry.classes.square(4)
geometry.function.area(sq) # -> 16

Or you can import a module from a package:

import geometry.variables as var # import only what is available in variables.py
print(var.pi) # -> 3.1415...

NumPy: the first glimpse of scientific computing

Let's try this with numpy, a well-known package containing a lot of scientific tools! To import the NumPy package, you could write import numpy, but it's easier to write:

import numpy as np

Now that you've imported the NumPy module, what about the square root function? It's the sqrt  function of NumPy:

np.sqrt(16) # -> 4.0

But NumPy is providing a particular new object: the array. An array is similar to a list, or a mathematical matrix, and includes a lot of useful methods! Let's see an example of what is possible with arrays:

some numpy array's methods
Some NumPy array methods

To go deeper with Python packages

These are just a few examples of what NumPy does! If you want to go deeper into packages, below are some other frequently used ones:

  • math : contains a lot of mathematical functions/variables. A lot of these are also included in NumPy.

  • matplotlib and seaborn: used for data visualization.

  • pandas: to import and process your data into Python.

  • sklearn: simple and efficient tools for data mining and data analysis.

  • scipy: used for scientific computing.

Summary

In this chapter, you learned the basics of modules that provide useful functions:

  • A module is a file consisting of Python code which can define functions, classes, and variables.

  • You can use any module in Python through the import key.

  • To use a module's function, classes, etc., use the . notation:  module.function()

  • A package is a collection of Python modules.

  • A Python array is a NumPy's object, similar to a list, but with far more available methods. 

Exemple de certificat de réussite
Exemple de certificat de réussite