Introducing Python Modules Based on CBSE Curriculum Class -11 By- Neha Tyagi PGT CS KV 5 Jaipur II Shift Jaipur Region Neha Tyagi, PGT CS II Shift Jaipur
Introduction A book is generally divided into chapters. Why? If it is not divided into chapters, reading will be boring and problematic. Similarly, a big program is generally divided into small units, which are known as modules. The process of dividing a big program into small modules is known as modularity. A module is itself an individual unit.
Introduction Modularity decreases complexity of a program. Modularity also enhance the capability of Reusability. In this chapter we will discuss modularity in python and about python modules.
Functions Imagine a Math s formula say 3x 2 x = 1 will result into 3 X 1 2 = 3 x = 2 will result into 3 X 2 2 = 12 x = 3 will result into 3 X 3 2 = 27 means f(x) = 3x 2 We can say f(x) = 3x 2 is a function where function is f, x is its argument and 3x 2 is its functionality. Function completes its working and afterwards returns a result. Programming languages also supports functions. A function- Can have arguments. Can perform Functionality. Can return result. For exdef Calc(x): r = 3*x**2 return r
Functions def is the keyword to declare a function. calc is name of function. Carefully see the last exampleresult Varibale inside ( ) is argument or parameter. colon : specifies end of line and starting of function block. The block contains whole functionality of the function. Here r = 3 * x ** 2 is the functionality of calc function. The part which is not inside indentation is not the part of function. return statement returns the computed result.
Functions Calling Function calling statement or syntax <FunctionName>(<ValueOfArgumentToBePassed>) def Calc(x): r = 3*x**2 return r To call the function calc you need to write following statement- Calc(4) or A=5 Calc(A)
Python Function Type In Python, functions are of 3 types- Built-in functions : These are ready to use pre defined functions. For ex- len( ), type( ), int( ), input( ) etc. 1. Functions in Modules : These functions existes in modules. To use such functions, you need to first import the concerned module. like sin( ) in math module. 2. User defined functions : These are developed by programmer.
Structure of Python Module A Python Module contains several things other than functions. A Python file (.py file) can contain following objects- Docstring : Triple Quoted comments which are important for documentation. Variables and constants : labels for data. Classes : Blue print for objects. Objects : Instances of classes. Statements : Instructions Functions :Group of instructions Therefore we can say that if there is a module named ABC then there should be a file named ABC.py. Module is an individual unit of data and code. It has the feature of reusability. It can depend on other modules also.
Python Module -Structure docstring starts and ends with triple quotes. This module has 2 functions and 2 docstrings. Importing module. Using object of module.
Python Module-Structure help(<modulename>) is used to see complete documentation of module. Facts of docstring : First letter of a line should be capital. Second line empty. Information in third line. dir (<ModuleName>) shows name of all objects of a module.
Importing module in a Python program Import statement can be used in two forms- 1. Importng complete moduleimport module1, module 2,.... For ex: >>>import math >>>import math,time etc To use it s object then- <modulename>.<functionname> 2. Importing selected objects from a module.- from <module> import<objectname1>,<>... Here, first pi value showed error. But when it is used after importing math module then all errors gets resolved.
Namespace A namespace is a place to keep all names. It can understand with following example- Suppose students from different KVs are participating in regional sports meet. If a name say Rahul is called- There may be many students with the name Rahul which will create confusion. Correct way of calling here should be Rahul from KV 5 Jaipur. Hence, KV 5 Jaipur is a namespace for Rahul here. This way of namespace resolves confusion. Similarly in python, namespace is an environment to keep logical group of related objects. Python creates a namespace with the name of module itself. When two modules comes together then it gives a manner to write two similar objects, which is as under- <ModuleName>.<ObjectName> Example : >>>math.sqrt(4)
Processing of import <module> command When you run import <module> command, following tasks takes place- Code of imported module executed after interpretation. All programs and variables of the imported modules comes in the present program. A namespace is set up with the name of imported module itself. Processing of from <module> import <object>command When you run from<module> import <object> command, following tasks takes place- Code of imported module executed after interpretation. Only selected (asked for) programs and variables of the imported modules comes in the present program. No new namespace is created. Imported objects appends in the present namespaceह
Thank you Please follow us on our blog www.pythontrends.wordpress.com Neha Tyagi, KV 5 Jaipur II Shift