1. .
Class-XII Computer Science (083)
All the contents used as part of the slides are either self created or from the public domain or textbooks for Class XII.
This presentation is only used for leaning purpose only. Programs used in this presentation are based on Python 3.8.0.
Computational Thinking and Programming - 2
Working with Functions
Scope of Variables
S K Mahto, PGT (Computer Science)
J.N.V East Medinipur WB
2. Working with Functions
Scope of Variable
● The part of the program where a variable can be used is known as
Scope of variable.
● Or we can say, Scope of a variable is the portion of code where it is
available to read and update.
● There are two types of scopes :
Global Scope
Local Scope
Presented by : S K Mahto, PGT Computer Science
3. Working with Functions
Scope of Variable
● Note :
● By default, in python the scope is local to a functions.
● we saw that if we use a name inside a function and that it is different
from using the same name outside the function.
● But actually this happens only when we update the name inside the
function.
Presented by : S K Mahto, PGT Computer Science
4. Working with Functions
Scope of Variable
● Global Scope
With global scope, variable can be used anywhere in the program
Variable defined outside all functions are global variables.
Presented by : S K Mahto, PGT Computer Science
5. Working with Functions
Scope of Variable
● Two Examples – Global Scope
Presented by : S K Mahto, PGT Computer Science
def fun():
y=x
print(y)
x=5
fun()
def fun():
y=x
print(y)
x=10
x=5
fun()
What happened?
Let us see in practical…
6. Working with Functions
Scope of Variable
● Two Examples – Global Scope
Presented by : S K Mahto, PGT Computer Science
def fun():
y=x
print(y)
x=5
fun()
def fun():
y=x
print(y)
x=10
x=5
fun()
• If x is not found in fun(), python looks at enclosing function
for global x
• if x is updated in fun(), it becomes a local name.
Fine ! Error !
7. Working with Functions
Scope of Variable
Presented by : S K Mahto, PGT Computer Science
def fun():
y=x[0]
print(y)
x[0]=20
x=[5]
fun()
• Actually, this applies only to
immutable values.
8. Working with Functions
Scope of Variable
Presented by : S K Mahto, PGT Computer Science
def fun():
y=x[0]
print(y)
x[0]=20
x=[5]
fun()
• Actually, this applies only to
immutable values
• Global names that point to
mutable values can be updated
within a function.
Fine!
9. Working with Functions
Scope of Variable
Global immutable values
● What, if we want a global integer?
● Why would you want a global integer?
● To count the number of times a function is called
● To access global variable inside the function
prefix keyword global with the variable.
Presented by : S K Mahto, PGT Computer Science
def fun():
global x
y=x
print(y)
x=10
x=5
fun()
print(x)
10. Working with Functions
Scope of Variable
● Local Scope
With local scope, variable can be used only within
the function / block that it is created
Variables defined inside any function are local variables.
● Here f and i are local variable, and these can be used only within the function.
Presented by : S K Mahto, PGT Computer Science
11. Working with Functions
Summery
● Python variables are looked up inside-out from within function.
● Updating a variable with immutable values creates a local copy of
that name.
● Can update global variables with mutable values.
● Use global keyword to update immutable values inside the function.
● Local variables can be used within the function.
Presented by : S K Mahto, PGT Computer Science