Modules are ready made bundles of code, which can be used by developers. Modules contain code which was developed by some programmer(probably in some basement 🤷🏻) and have specific functionality. For example – Python’s Math Module contain many functions which can be used for doing Mathematical stuff in Python Code, similarly turtle module can be helpful for making graphical programs like Games.
How to import modules in Python?
- Modules can be imported in Python using from module_name import * statement(* being some function inside module). Here in order to initialise module, you need to do some_variable = *. Then refer to function using some_variable across whole program.
- Modules can also be imported using import module_name statement. Here your importing whole of module as compared to only some specific function in #1. In this case for initialising some function from module, you can do some_variable = module_name.function_name() and use some_variable throughout Python program for referencing to function.
Let’s try to understand this by some code examples.
# 1 from math import ceil m = ceil # Use variable m for referencing to math module function ceil() # 2 import math c = math.ceil() # Use variable m for referencing to math module ceil() function f = math.floor() # Use variable f for referencing to math module floor() function
If your not aware of Python’s Math Module then read Math Module of Python article.