Introducing Python Modules · •A book is generally divided into chapters. Why? •If it is not...

14
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

Transcript of Introducing Python Modules · •A book is generally divided into chapters. Why? •If it is not...

Page 1: Introducing Python Modules · •A book is generally divided into chapters. Why? •If it is not divided into chapters, reading will be boring and problematic. •Similarly, a big

Introducing Python Modules Based on CBSE Curriculum

Class -11

By-Neha TyagiPGT CSKV 5 Jaipur II ShiftJaipur Region

Neha Tyagi, PGT CS II Shift Jaipur

Page 2: Introducing Python Modules · •A book is generally divided into chapters. Why? •If it is not divided into chapters, reading will be boring and problematic. •Similarly, a big

Introduction

Neha Tyagi, PGT CS KV 5 Jaipur II Shift

• 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.

Page 3: Introducing Python Modules · •A book is generally divided into chapters. Why? •If it is not divided into chapters, reading will be boring and problematic. •Similarly, a big

Introduction

Neha Tyagi, PGT CS KV 5 Jaipur II Shift

• 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.

Page 4: Introducing Python Modules · •A book is generally divided into chapters. Why? •If it is not divided into chapters, reading will be boring and problematic. •Similarly, a big

Functions

Neha Tyagi, PGT CS KV 5 Jaipur II Shift

• Imagine a Math’s formula say 3x2

x = 1 will result into 3 X 12 = 3

x = 2 will result into 3 X 22 = 12

x = 3 will result into 3 X 32 = 27

means f(x) = 3x2

• We can say f(x) = 3x2 is a function where function is f, x is its

argument and 3x2 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 ex-

def Calc(x):

r = 3*x**2

return r

Page 5: Introducing Python Modules · •A book is generally divided into chapters. Why? •If it is not divided into chapters, reading will be boring and problematic. •Similarly, a big

Functions

Neha Tyagi, PGT CS KV 5 Jaipur II Shift

• def is the keyword to declare a function.

• calc is name of function.

• 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.

Carefully see the last example-

result

Page 6: Introducing Python Modules · •A book is generally divided into chapters. Why? •If it is not divided into chapters, reading will be boring and problematic. •Similarly, a big

Functions Calling

Neha Tyagi, PGT CS KV 5 Jaipur II Shift

• 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)

Page 7: Introducing Python Modules · •A book is generally divided into chapters. Why? •If it is not divided into chapters, reading will be boring and problematic. •Similarly, a big

Python Function Type

Neha Tyagi, PGT CS KV 5 Jaipur II Shift

• 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.

Page 8: Introducing Python Modules · •A book is generally divided into chapters. Why? •If it is not divided into chapters, reading will be boring and problematic. •Similarly, a big

Structure of Python Module

Neha Tyagi, PGT CS KV 5 Jaipur II Shift

• 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.

Page 9: Introducing Python Modules · •A book is generally divided into chapters. Why? •If it is not divided into chapters, reading will be boring and problematic. •Similarly, a big

Python Module -Structure

Neha Tyagi, PGT CS KV 5 Jaipur II Shift

This module has 2 functions and 2 docstrings.|

Importing module.

Using object of module.

docString starts and ends with triple quotes “”””.

Page 10: Introducing Python Modules · •A book is generally divided into chapters. Why? •If it is not divided into chapters, reading will be boring and problematic. •Similarly, a big

Python Module-Structure

Neha Tyagi, PGT CS KV 5 Jaipur II Shift

help(<ModuleName>) is used to see complete documentation of module.

dir (<ModuleName>) shows name of all objects of a module.

Facts of docString :• First letter of a line should be capital.• Second line empty.• Information in third line.

Page 11: Introducing Python Modules · •A book is generally divided into chapters. Why? •If it is not divided into chapters, reading will be boring and problematic. •Similarly, a big

Importing module in a Python program

Neha Tyagi, PGT CS KV 5 Jaipur II Shift

• Import statement can be used in two forms-

1. Importng complete module-

import 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.

Page 12: Introducing Python Modules · •A book is generally divided into chapters. Why? •If it is not divided into chapters, reading will be boring and problematic. •Similarly, a big

Namespace

Neha Tyagi, PGT CS KV 5 Jaipur II Shift

• 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)

Page 13: Introducing Python Modules · •A book is generally divided into chapters. Why? •If it is not divided into chapters, reading will be boring and problematic. •Similarly, a big

Processing of import <module> command

Neha Tyagi, PGT CS KV 5 Jaipur II Shift

• 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हैं |

Page 14: Introducing Python Modules · •A book is generally divided into chapters. Why? •If it is not divided into chapters, reading will be boring and problematic. •Similarly, a big

Thank you

Please follow us on our blog

Neha Tyagi, KV 5 Jaipur II Shift

www.pythontrends.wordpress.com