Tuesday, February 13, 2018

python

Python Moudule

Modules are used to categorize code in Python into smaller part. A module is simply a file, where classes, functions and variables are defined. Grouping similar code into a single file makes it easy to access.
Have a look over example:
If the content of a book is not indexed or categorized into individual chapters, then the book might have turned boring and hectic. Hence, dividing book into chapters made it easy to understand.
In the same sense python modules are the files which have similar code. Thus module is simplify a python code where classes, variables and functions are defined.
Advantage:
Python provides the following advantages for using module:
1) Reusability: Module can be used in some other python code. Hence it provides the facility of code reusability.
2) Categorization: Similar type of attributes can be placed in one module.

Importing a Module:

There are different ways by which you we can import a module. These are as follows:

1) Using import statement:

"import" statement can be used to import a module.
Syntax:
  1. import <file_name1, file_name2,...file_name(n)="">  
  2. </file_name1,>  
Have a look over an example:
eg:
  1. def add(a,b):  
  2.     c=a+b  
  3.     print c  
  4.     return  
Save the file by the name addition.py. To import this file "import" statement is used.
  1. import addition  
  2. addition.add(10,20)  
  3. addition.add(30,40)