Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




PYTHON - MODULE


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.


Example :


add.py


def addition(x, y):
    z = x + y
    return z

main.py


import add

a = 10
b = 11
c = add.addition(a, b)
print("The added result is ", c)


Output :



  The added result is 21

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.


import add

The next thing we do is, create two variables 'a' and 'b', and initialise them with '10' and '11'.


a = 10
b = 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'.


c = add.addition(a, b)

And the addition function gets called from the 'add.py' module.


java_Collections

And we get the added result returned and printed.


So, to summarise, creating and invoking a 'Module' is a three step process.


  1. Create a Function and save it in a file named say 'add.py' (Module name can be anything but the extension must be '.py'). And the 'Module is created'.

    add.py


    def addition(x, y):
        z = x + y
        return z
    
  2. Next, in the calling program, import the 'Module' using the 'import' keyword, followed by the 'Module' name.
    import add
  3. Then call the Function (i.e. 'addition(...)') using the module name (i.e. 'add').
    c = add.addition(a, b)

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.


Variables in a Module


Say for example, we need a 'Module' that will have a List with three names,


names = ["John", "Sean", "Prakash"]

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.


Example :


newaddfunction.py


names = ["John", "Sean", "Prakash"]

def addition(x, y):
    z = x + y
    return z

main.py


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)


Output :



  The added result is 21
  The second name in the List is Sean

So, in the above Example, we have created a module named, 'newaddfunction.py'.


newaddfunction.py


names = ["John", "Sean", "Prakash"]

def addition(x, y):
    z = x + y
    return z

And created a List containing three names,


names = ["John", "Sean", "Prakash"]

java_Collections

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'.


a = 10
b = 11

Then as usual, we have called the 'addition(...)' Function using the 'newaddfunction' module.


c = newaddfunction.addition(a, b)

Next, we have the 'Module' name 'newaddfunction' to access the second name of the List 'names'.


name = newaddfunction.names[1]

And as we can see, the second element in the List is, 'Sean'. And we got it printed.


The second name in the List is Sean

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.


Renaming the Module name


Example :


newaddfunction.py


names = ["John", "Sean", "Prakash"]

def addition(x, y):
    z = x + y
    return z

main.py


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)


Output :



  The added result is 21
  The second name in the List is Sean

And what we have done is, renamed the 'newaddfunction' Module name in the 'import' statement using 'as' keyword.


import newaddfunction as af

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'.


c = af.addition(a, b)

Similarly, accessed the List using the new 'Module' name 'af'.


name = af.names[1]

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.


The 'from' keyword with import


Example :


newaddfunction.py


names = ["John", "Sean", "Prakash"]

def addition(x, y):
    z = x + y
    return z

main.py


from newaddfunction import names

name = names[1]
print("The second name in the List is ", name)


Output :



  The second name in the List is Sean

And all we have done is, used 'from' keyword along with 'import'.


from newaddfunction import names

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.


name = names[1]

Python also have a lot of Modules that are predefined. 'Math' module is one of the common 'Module' used.


Predefined Modules


Example :


import math
print(math.factorial(5))


Output :



  120

So, in the above code, we have used the 'import' statement to import the 'math' package that has all the predefined Mathematics needed.


import math

Then we have used 'math' to call the 'factorial(5)' function to calculate the factorial of '5'.


print(math.factorial(5))

If you want to list out all the methods provided by any 'Module'. You can use the 'dir( )' Function.


The dir( ) Function


Example :


import math
print(dir(math))


Output :



  ['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'pi', 'pow', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']