Python Module

python-modules-ipcisco-1

Python Modules are the libraries that includes a set of functions, variables etc. that are defined earlier. In other words, they are the files that includes ready to use functions, variables etc. There are well known Python modules while you can also create your own module. With Python module architecture, you do not need to write every function times and times. You can create one time and then you can use the module that this function resides.

 

There are widely used modules in python. We can say these modules well-known modules. There are a lot of modules like this . Some of these well-known modules are given below:

 

  • math
  • collections
  • csv
  • datatime
  • re
  • unittest
  • string

 

The extension of module files is “.py”. Both the well-known modules and newly created modules have this extension.

 

So, how can we use an existing well known python module? How can we create a new module in Python? How can we use just created module in python? In this lesson, we will focus on these questions.

 


 

How to Use A Module in Python?

 

To use the functions or variables in a well-known python module or a newly created module, firstly we should import the module that the function resides in. We use “import” keyword with the name of the module to add a module into the code.

 

For example, if we would like to the functions in math module, we use the below line:

 

import math

 


 

How to Use Part of A Module?

 

If we need only specific parts of a module, we can define it in the code. When we define this, the other parts of the module is not included. We can do this with “from” keyword.

 

from math import sqrt

print(sqrt(25))

 

python-modules-ipcisco-2

 

Here, we get only sqrt function from the math module of python.

 


 

How to Create A Module in Python?

 

To create a module, the only thing we need to do is defining functions and then save it with “.py” extension. After that our module is ready to use. To use this module, we need to put this module in the same directory with the code file in which we will use this module. Or we an put the created module to the lib directory of Python (This is “C:\Users\asus\anaconda3\Lib” in my PC).

 

Let’s show this with an example. In the below example, firstly we will create a python module that includes two functions. After that we will use them in another coding example. To do this, we will import this module

 

def square(x):
  print(x*x)

 def hello(x):
  print("Hello ",x)

 

We will record this file as “ourmodule.py”. Then, we will use this name in our other code file.

 

import ourmodule

ourmodule.square(5)
ourmodule.hello("Gokhan")

 

The output of this code will be:

 

25
Hello Gokhan

 

python-modules-ipcisco-1

 

We can also define different variables in a Python Module. And after that when we import this file to other file, we can use these variables easily. Modules is a very efficient mechanism also for such usages.

 

In the below example, we will define a list in a module and then use it with import keyword.

 

def elves():
   elves=["Elrond", "Legolas", "Arwen"]

 

We will record this file as characters.py. And when we need to use them, we will add this moduel with import keyword.

 

import characters

print(elves[1])

 

The output will be:

 

Legolas

As it is also defined in this new code file.

 


 

Module Alias

 

We can define aliases to the modules. To do this we use “as” keyword after import keyword and the name of the python module.

 

import ourmodule as our

 


 

Listing Module Functions

 

We can use Python dir() function to list the functions in a Python module. Let’s use this with math module and list all the functions listed in this module.

 

import math

print(dir(math))

 

The output will be a list of the defined funtions in math module.

 

['__doc__', '__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']
Back to: Python Programming Course > Python Basics

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact

  • info@ipcisco.com
IPCisco is the Winner! “Best Certification Study Journey of 2019!”

Cisco-ITBlogAwards-2019-Winner-IPCisco-k