Modules in Python

So what is a module? The Python Tutorial defines it as a file containing Python definitions and statements, which can be later imported and used when necessary.

1. If you want to import a module as a whole, you can do it using the import module_name statement. You are allowed to import more than one module at once using a comma-separated list. For example:

although the latter form is not recommended due to stylistic reasons, and it’s better and prettier to express the same intention in more a verbose and explicit form, such as:

 

2. If a module is imported in the above manner and you want to access any of its entities, you need to prefix the entity’s name using dot notation. For example:

The snippet makes use of two entities coming from the my_module module: a function named my_function() and a variable named my_data. Both names must be prefixed by my_module. None of the imported entity names conflicts with the identical names existing in your code’s namespace.

 

3. You are allowed not only to import a module as a whole, but to import only individual entities from it. In this case, the imported entities must not be prefixed when used. For example:

The above way – despite its attractiveness – is not recommended because of the danger of causing conflicts with names derived from importing the code’s namespace.

 

4. The most general form of the above statement allows you to import all entities offered by a module:

Note: this import’s variant is not recommended due to the same reasons as previously (the threat of a naming conflict is even more dangerous here).

 

5. You can change the name of the imported entity “on the fly” by using the as phrase of the import. For example:

 

Exercise 1

You want to invoke the function make_money() contained in the module named mint. Your code begins with the following line:

What is the proper form of the function’s invocation?

 

 

Exercise 2

You want to invoke the function make_money() contained in the module named mint. Your code begins with the following line:

What is the proper form of the function’s invocation?

 

 

Exercise 3

You’ve written a function named make_money on your own. You need to import a function of the same name from the mint module and don’t want to rename any of your previously defined names. Which variant of the import statement may help you with the issue?

 

 

Exercise 4

What form of the make_money function invocation is valid if your code starts with the following line:

?

 

 

Working with standard modules

1. A function named dir() can show you a list of the entities contained inside an imported module. For example:

prints out the list of all the os module’s facilities you can use in your code.

2. The math module couples more than 50 symbols (functions and constants) that perform mathematical operations (like sine(), pow(), factorial()) or providing important values (like π and the Euler symbol e).

3. The random module groups more than 60 entities designed to help you use pseudo-random numbers. Don’t forget the prefix “random“, as there is no such thing as a real random number when it comes to generating them using the computer’s algorithms.

4. The platform module contains about 70 functions which let you dive into the underlaying layers of the OS and hardware. Using them allows you to get to know more about the environment in which your code is executed.

5. Python Module Index (https://docs.python.org/3/py-modindex.html is a community-driven directory of modules available in the Python universe. If you want to find a module fitting your needs, start your search there.

 

Exercise 1

What is the expected value of the result variable after the following code is executed?

 

True

Exercise 2

(Complete the sentence) Setting the generator’s seed with the same value each time your program is run guarantees that…

… the pseudo-random values emitted from the random module will be exactly the same.

 

Exercise 3

Which of the platform module’s functions will you use to determine the name of the CPU running inside your computer?

The processor() function

 

Exercise 4

What is the expected output of the following snippet?

 

3