Scopeof Variables
 SCOPEmeans in which part(s) of the program, a
particular piece of code or data is accessible or
known.
 InPythonthere are broadly 2 kindsof Scopes:
Global Scope
Local Scope
Global Scope
 A name declared in top level segment( main ) of a
program is said to have global scope and can be
usedin entire program.
 Variable defined outside all functions are global
variables.
Local Scope
 A name declare in a function body is said to have
local scope i.e. it can be used only within this
function and the other block inside the function.
 Theformal parameters are also having local scope.
 Letusunderstandwithexample….
Example– Localand Global Scope
Example– Localand Global Scope
„a‟isnot accessible
here becauseit is
declared in function
area(), soscopeis
local to area()
Example– Localand Global
Scope
Variable "ar‟ is accessible in
function showarea() because
it ishaving Global Scope
Thisdeclaration “global count” is
necessaryfor usingglobal
variables in function, other wise an
error “local variable 'count'
referenced before assignment”
will appear becauselocal scope
will create variable “count” and it
will be found unassigned
Lifetime of Variable
 Is the time for which a variable lives in memory. For
Global variables the lifetime isentire program run
i.e. as long as program is executing. For Local
variables lifetime is their function‟s run i.e. as long as
function isexecuting.
NameResolution(ScopeResolution)
 Forevery nameusedwithin program python follows nameresolutionrules
knownasLEGB rule.
 (i) LOCAL : first check whether name is in local environment, if yes
Python uses its value otherwise moves to (ii)
 (ii) ENCLOSING ENVIRONMENT: if not in local, Python checks whether
name is in Enclosing Environment, if yes Python uses its value
otherwise moves to (iii)
 GLOBAL ENVIRONMENT: if not in above scope Python checks it in
Global environment,if yes Python uses itotherwise moves to (iv)
 BUILT-IN ENVIRONMENT: if not in above scope, Python checks it in built-
in environment, if yes, Python uses its value otherwise Python would
reportthe error:
 name<variable> notdefined
Predict the output
Programwith
variable “value” in
both LOCALand
GLOBALSCOPE
Predict the output
Programwith
variable “value” in
both LOCALand
GLOBALSCOPE
Predict the output
UsingGLOBAL
variable “value” in
local scope
Predict the output
UsingGLOBAL
variable “value” in
local scope
Predict the output
Variable “value”
neither in local nor
global scope
Predict the output
Variable “value”
neither in local nor
global scope
Predict the output
Variable in Global
notin Local
(input in variable at
global scope)
Predict the output
Variable in Global
notin Local
(input in variable at
global scope)

Scope of Variables.pptx

  • 1.
    Scopeof Variables  SCOPEmeansin which part(s) of the program, a particular piece of code or data is accessible or known.  InPythonthere are broadly 2 kindsof Scopes: Global Scope Local Scope
  • 2.
    Global Scope  Aname declared in top level segment( main ) of a program is said to have global scope and can be usedin entire program.  Variable defined outside all functions are global variables.
  • 3.
    Local Scope  Aname declare in a function body is said to have local scope i.e. it can be used only within this function and the other block inside the function.  Theformal parameters are also having local scope.  Letusunderstandwithexample….
  • 4.
  • 5.
    Example– Localand GlobalScope „a‟isnot accessible here becauseit is declared in function area(), soscopeis local to area()
  • 6.
    Example– Localand Global Scope Variable"ar‟ is accessible in function showarea() because it ishaving Global Scope
  • 7.
    Thisdeclaration “global count”is necessaryfor usingglobal variables in function, other wise an error “local variable 'count' referenced before assignment” will appear becauselocal scope will create variable “count” and it will be found unassigned
  • 8.
    Lifetime of Variable Is the time for which a variable lives in memory. For Global variables the lifetime isentire program run i.e. as long as program is executing. For Local variables lifetime is their function‟s run i.e. as long as function isexecuting.
  • 9.
    NameResolution(ScopeResolution)  Forevery nameusedwithinprogram python follows nameresolutionrules knownasLEGB rule.  (i) LOCAL : first check whether name is in local environment, if yes Python uses its value otherwise moves to (ii)  (ii) ENCLOSING ENVIRONMENT: if not in local, Python checks whether name is in Enclosing Environment, if yes Python uses its value otherwise moves to (iii)  GLOBAL ENVIRONMENT: if not in above scope Python checks it in Global environment,if yes Python uses itotherwise moves to (iv)  BUILT-IN ENVIRONMENT: if not in above scope, Python checks it in built- in environment, if yes, Python uses its value otherwise Python would reportthe error:  name<variable> notdefined
  • 10.
    Predict the output Programwith variable“value” in both LOCALand GLOBALSCOPE
  • 11.
    Predict the output Programwith variable“value” in both LOCALand GLOBALSCOPE
  • 12.
    Predict the output UsingGLOBAL variable“value” in local scope
  • 13.
    Predict the output UsingGLOBAL variable“value” in local scope
  • 14.
    Predict the output Variable“value” neither in local nor global scope
  • 15.
    Predict the output Variable“value” neither in local nor global scope
  • 16.
    Predict the output Variablein Global notin Local (input in variable at global scope)
  • 17.
    Predict the output Variablein Global notin Local (input in variable at global scope)