Zed-King Study app · Free
O Level students across India practise here, free
Google login · no Play Store needed
About this PDF
Local and global scope, creating your own modules and importing them.
This is part of the free O Level M3-R5.1 study material in the Zed-King Study Zone. You can read every page above in your browser, or download the file and study offline. Zed-King Group of Institute is a NIELIT Study Centre in Kaithal, Haryana, and all of this material follows the current NIELIT syllabus.
- Course
- O Level — NIELIT Foundation Diploma
- Module
- M3 — M3-R5.1: Programming and Problem Solving through Python
- Section
- Chapterwise Notes
- Format
- PDF — read online or download
- Price
- Free, no sign-up required
Text from this PDF
The opening pages as plain text — open the reader above for tables, diagrams and the full file.
CHAPTER-8 Scope and Modules
Module:- i. Module is a python file that contains python variables, classes, functions, statements or definitions.
Like- akit.py is a module ii. The extension name of Python module file is .py. This means that the file created in python is a module.
iii. Import keyword is used in one module of Python to use the properties of another module.
iv. There are some inbuilt modules in Python such as math, random. Each module has a different identity. Math module contains some functions related to mathematics such as sin(), cos() and sqrt() etc.
v. Modules in Python are a type of program. Modules contain functions and classes.
There are two types of modules in Python
1. In-built Modules
2. User-Defined Modules E.xmnple:- math a=5 pmt(y)
IOO x=5 k=i*j pfiut(k)
import math import mdl a=2 print(r)
mdl.mult With argument:- a-IO mdl rint mdl.a Note:- we can access members by using module name and (.) dot operator Renaming module:- a-IO mdl as m rint m.a Note:- import mdl as m mdl original module name m aliasing name aliasing name (m) use mdl module members access Note:- import mdl as m where mdl is the original module name and m is the aliasing name. By using aliasing name (m) all the members of the mdl module can be accessed.
Import particular member from module:- To import a particular member from a module, use the from-import keyword.
The main advantage of from import is that any member of the module can be accessed without the module name.
from mdl import add mul a=5 Access all members from module:- a-IO Member Aliasing:- Olg save mdl.py from mdl import * a=5 from mdl import add as sm, mul as LEGB The full name of LEGB is local, enclosed, global, built in. This is the possible scope of the scope of a variable in Python.
The role of LEGB is to define the name space that stores variable names, function names, and values. LEGB has an order of precedence for scope resolution. LEGB rules define the sequence in which python names (variable, function, module etc) searches.
For example, if we give reference of the name given to any variable to python, then python will see that name in sequence in the scope of local, enclosing, global, built in.
Like- module name, function name, variable name, class name etc.
Scope:- Scope defines the hierarchical order in which the name space has to be searched to get the mapping of the variable. It defines the accessibility and lifetime of a variable.
a="outer loop" ggfmsg():
a="innser loop" Built-ln Global Enclosed Local def num():
num Local:- Traceback (most recent call last):
File "C:\Users\Akhilesh\Downloads\mem.py", line 5, in <module> priot(a)
NameError: name 'a' is not defined This is the code block or body of the function or lambda expression available in any python program. These are the names in python scope which we define inside the function loop, hence this name can be used only in that function or block code local.
The name space gets deleted after function or code block execution.
Local variable is inside a function or class method. And it can also be accessed in the same scope. Any local variable cannot be accessed outside the function or class.
def outside():
def inside():
inside outside Enclosed:- Q_utput a-IOO def msg():
a=50 enclosing variable def msgl():
print(b)
ms I This is a special scope that exists only for nested functions. This scope contains the names that are defined in the enclosing function.
Note:- Names in the enclosing scope are accessible from the enclosing function to the inner function.
output def outside():
def inside():
inside pOnt(a)
outside()
Global:- The global name space is created for the module when the definition of the module is read. The global name space is the top most scope in a python program or script or module. This scope contains all those names which are defined at the top level of a program or module. Names in the global name space can be accessed everywhere throughout the program.
More chapterwise notes for M3-R5.1
Run these Python programs yourself — free
Reading code is not the same as writing it. Type any program from these notes into our free online Python compiler (real Python 3 with NumPy, input() works), or study one of the explained programs with its output.
Prepare for O Level with expert guidance
Classroom training, practical labs and exam registration at Zed-King Institute, Kaithal.
