Say, you want to create a Function to add two numbers and want that add Function to be used by every other programmer, who wants to use it.
And this is where a 'Module' into picture.
Creating a 'Module' is super simple. All you need to do is, create a file with '.py' extension and the file name would be your module name.
Let us explain it with the below example.
def addition(x, y): z = x + y return z
import add a = 10 b = 11 c = add.addition(a, b) print("The added result is ", c)
So, in the above code, we have created a Function called 'def addition(x, y):', that takes two values as 'Arguments'(i.e. 'x' and 'y'). And returns the added result 'z'.
def addition(x, y): z = x + y return z
Then we have saved it in a file and named it 'add.py'.
And the file named 'add.py' is a 'Module'.
And that's it. We are done creating a 'Module'.
Our next task would be to use the 'Module', 'add.py'.
For that we come to our actual application and say, named it as 'main.py'.
And the first thing we do is, import the 'add' module using the 'import' keyword. So that we can use it.
The next thing we do is, create two variables 'a' and 'b', and initialise them with '10' and '11'.
Then we try adding them using the 'addition(x, y)' Function, that is defined in the 'add.py' module.
So, to invoke the 'addition(x, y)' Function, we have used the dot '.' operator with the module name 'add'.
And the addition function gets called from the 'add.py' module.
And we get the added result returned and printed.
So, to summarise, creating and invoking a 'Module' is a three step process.
def addition(x, y): z = x + y return z
So far, we have seen that we can have Functions in a 'Module'. But we can also have variables in a 'Module'. It can be a Integer or a String, a List, Dictionary or any Collection.
Say for example, we need a 'Module' that will have a List with three names,
And an 'add' Function that will add two numbers and return the result.
So, we can create a 'Module' in the same way and declare a List along with a Function.
Let us see in the below example.
names = ["John", "Sean", "Prakash"] def addition(x, y): z = x + y return z
import newaddfunction a = 10 b = 11 c = newaddfunction.addition(a, b) print("The added result is ", c) name = newaddfunction.names[1] print("The second name in the List is ", name)
So, in the above Example, we have created a module named, 'newaddfunction.py'.
names = ["John", "Sean", "Prakash"] def addition(x, y): z = x + y return z
And created a List containing three names,
And also the module has a Function 'def addition(x, y):'.
def addition(x, y): z = x + y return z
Now, in our main application, we have created two variables 'a' and 'b' and assigned them with '10' and '11'.
Then as usual, we have called the 'addition(...)' Function using the 'newaddfunction' module.
Next, we have the 'Module' name 'newaddfunction' to access the second name of the List 'names'.
And as we can see, the second element in the List is, 'Sean'. And we got it printed.
Now, everthing else is fine. But don't you think, the 'Module' name is a little big.
i.e. newaddfunction.addition(a, b)
'newaddfunction' seems like a long name.
Let us see the next example to shorten or rename the 'Module' name in the 'import' statement.
names = ["John", "Sean", "Prakash"] def addition(x, y): z = x + y return z
import newaddfunction as af a = 10 b = 11 c = af.addition(a, b) print("The added result is ", c) name = af.names[1] print("The second name in the List is ", name)
And what we have done is, renamed the 'newaddfunction' Module name in the 'import' statement using 'as' keyword.
Now, we do not need a long Module name i.e. 'newaddfunction'. We can use the short name 'af'.
And eventually, we have made a call to the 'addition(...)' using the new 'Module' name 'af'.
Similarly, accessed the List using the new 'Module' name 'af'.
So, in the above example, we have a List and a Function in the above 'Module'. But let's say, in our main application, we just want to use the List and restrict the use of Function.
In simple words, we don't want the Function to be even included.
And Python has a solution for that as well using the 'from' keyword.
names = ["John", "Sean", "Prakash"] def addition(x, y): z = x + y return z
from newaddfunction import names name = names[1] print("The second name in the List is ", name)
And all we have done is, used 'from' keyword along with 'import'.
And the List 'names' got included in our main application.
And the most important thing to note is, you don't even need the module name to access the List.
You can access the List directly.
Python also have a lot of Modules that are predefined. 'Math' module is one of the common 'Module' used.
import math print(math.factorial(5))
So, in the above code, we have used the 'import' statement to import the 'math' package that has all the predefined Mathematics needed.
Then we have used 'math' to call the 'factorial(5)' function to calculate the factorial of '5'.
If you want to list out all the methods provided by any 'Module'. You can use the 'dir( )' Function.
import math print(dir(math))