Modules

A module is a file containing python definitions( functions and variables) and statements.The file name is the module name with the suffix  .py appended.

We need modules to reuse many functions,variables and statements.

To import a module in a program, we use the import keyword along with the name of the module. Ex: import operation. The first time a module is loaded into a running Python script, it is initialized by executing the code in the module once.

Let us write a small piece of code to multiply 2 numbers and also divide them.screenshot-112.png

 

This is a program called as operation.py. It contains 2 functions multiply and divide which take in 2 parameters a and b and returns their product and quotient respectively.

Let us write another program.

screenshot-131.png

This program is called myprogram.py . It imports the file called operation.py and prints the product of 2 numbers 4 and 6 using the multiply function of operation.py.

The output is as follows.

screenshot-121.png

 

Python has a standard list of built in modules. You can check them out here . Python modules

For example, we have math module. To use the math module we use import math statement. 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

Powered by WordPress.com.

Up ↑

%d bloggers like this: