Modules in python
Let's discuss what a module is and its uses
Modules refer to a file containing python statement and definition.
We use modules to break down large programs into small manageable and organised files. Furthermore modules provide reusability of code.
We can define our most used function in a module and import it. Instead of copying their definition into different programs.
How to create a module and import them
To create a module write a python code in a file and save it with .py file extension. Let try some examples
def max_of_two( x, y ):
of x > y:
return x
return y
def max_of_three( x, y, z ):
return max_of_two( x, max_of_two( y, z ) )
print(max_of_three(3, 6, -5))
Importing module
To use this module, we need to import it let's create a new file main.py
And import it .
Import example
Once we import the example module we can call the max_of _three() function
Code
Import example
Output = example.max_of_three(3,21)
To call a function defined in the module we use the dot operator .
Thank you very much am Justin odi you can follow me on all social media spaces I offer python class too
Comments
Post a Comment
DROP YOUR COMMENT HERE