All Topics

Modules



What is a Module?
Consider a module to be the same as a code library.
A file containing a set of functions you want to include in your application.

Create a Module
To create a module just save the code you want in a file with the file extension .py

def greeting(name):
print("Hello, " + name)

Save this code in a file named mymodule.py

Use a Module
Now we can use the module we just created, by using the import statement

import mymodule
mymodule.greeting("Jonathan")
When using a function from a module, use the syntax: module_name.function_name.

Save this file as mymodule.py



Renaming a Module:-
Create an alias for mymodule called mx using 'as' keyword.


Import from module:-
mymodule.py:-


Driver code:-


When importing using the from keyword, do not use the module name when referring to elements in the module.
Example: person1["age"], not mymodule.person1["age"]



Hello