Course Content

If you are facing any issue in playing video please use Firefox browser.

Course Content

Description

What is a Module in Python 1

 

In this lecture we will see that, What is module ?, What is use of it ? and How to work with user defined modules ?

 

Module

 

A module is a file consisting of python code. A module can define functions, classes and variables. A module can also include runnable code.

 

For example, math module in which all the functions are provided for mathematical usage.

 

The python code for a module named aname normally resides in a file named 'aname.py'.

 

import statement in Module

 

You can use any Python source file as a module by executing an import statement in some other Python source file. The import has the following syntax :

import module1, module2,...moduleN

 

from...import statement in Module

 

It is also possible to import all names from a module into the current namespace by using the following import statement.

Syntax :

from modname import*

 

Locating Module

 

When you import a module the Python interpreter searches for the module in the following sequences -

  • The current directory,
  • if the module is an found Python then searches each directory in the self variable PYTHONPATH,
  • If all else fails, Python check the default path.

 

Example :

We'll create a module named 'Calculatoroperations' having mathematical functions -

def addition(a,b):

return a+b

def subtraction(a,b):

return a-b

def division(a,b):

return a/b

def multiplication(a,b):

return a*b

Now in next module if we want to use that particular module, we'll import it,

import Calculatoroperations

c=Calculatoroperations.addition(10,20)

print(c)

O/P – 30

 

Also :

from Calculatoroperations import *

c=addition(50,20)

print(c)

c=subtraction(25,20)

print(c)

O/P -

70

5

FAQs

Share With Friend

Have a friend to whom you would want to share this course?

Download The Apps

Code Scan or Download the app
Google Play Store
297K+ Downloads
4.5 Rating
10K+ Reviews
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere