0% found this document useful (0 votes)
2 views34 pages

Python_Lecture

The document covers user-defined functions in Python, focusing on variable scope, including local, global, and built-in scopes. It explains how variables can be accessed and modified within functions, and the differences between call by value and call by reference, ultimately describing Python's unique call-by-object-reference model. Various examples illustrate these concepts, including how mutable and immutable types behave when passed to functions.

Uploaded by

music93939646
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views34 pages

Python_Lecture

The document covers user-defined functions in Python, focusing on variable scope, including local, global, and built-in scopes. It explains how variables can be accessed and modified within functions, and the differences between call by value and call by reference, ultimately describing Python's unique call-by-object-reference model. Various examples illustrate these concepts, including how mutable and immutable types behave when passed to functions.

Uploaded by

music93939646
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

PYTHON

LECTURE 21
Today’s Agenda

• User Defined Functions-III


• Variable Scope

• Local Scope

• Global Scope

• Argument Passing
Variable Scopes

 The scope of a variable refers to the places from


where we can see or access a variable.

 In Python , there are 4 types of scopes:


 Local : Inside a function body
 Enclosing: Inside an outer function’s body . We will
discuss it later
 Global: At the module level
 Built In: At the interpreter level

 In short we pronounce it as LEGB


Global Variable

 GLOBAL VARIABLE

 A variable which is defined in the main body of a file is called


a global variable.

 It will be visible throughout the file


Local Variable

 LOCAL VARIABLE
 A variable which is defined inside a function is local to that
function.

 It is accessible from the point at which it is defined until the


end of the function.

 It exists for as long as the function is executing.

 Even the parameter in the function definition behave like local


variables

 When we use the assignment operator (=) inside a


function, it’s default behaviour is to create a new local
variable – unless a variable with the same name is already
defined in the local scope.
Example

s = "I love Python"


Since the variable
def f(): s is global , we
print(s) can access it from
anywhere in our
f() code

Output:
I love Python
Example

s = "I love Python" Since we have not


called the
def f(): function f( ) , so
print(s) the statement
print(s) will never
get a chance to
Output: run
Example

def f():
print(s)
s = "I love Python"
f() Even though the
variable s has been
declared after the
Output: function f( ) , still it
I love Python is considered to be
global and can be
accessed from
anywhere in our
code
Example

def f():
print(s)
f()
s="I love Python"

Since we have called


Output: the function f( ) ,
NameError ! before declaring
variable s , so we get
NameError!
Example

def f():
s="I love Python"
print(s)
f()
The variable s now
becomes a local
Output: variable and a
I love Python function can easily
access all the local
variables inside it’s
definition
Example

def f():
s="I love Python"
print(s)
f()
print(s)
The variable s is
Output: local and cannot be
I love Python accessed from
outside it’s
NameError! function’s definition
Example

s="I love Python"


def f():
s="I love C"
print(s)
If a variable with
f() same name is
defined inside the
print(s) scope of function as
well then Python
creates a new
Output: variable in local
I love C scope of the
function and uses it
I love Python
Example

What if we want to use the same global variable


inside the function also ?
s="I love Python"
def f():
To do this , we need
global s a special keyword in
s="I love C" Python called global.
print(s) This keyword tells
Python , not to
f() create any new
print(s) variable , rather use
the variable from
global scope
Output:
I love C
I love C
Guess The Output ?

s="I love Python"


def f(): Now , this is a special
print(s) case! . In Python any
variable which is
s="I love C" changed or created
inside of a function is
print(s) local, if it hasn't been
f() declared as a global
variable. To tell Python,
print(s) that we want to use the
global variable, we have
to explicitly state this by
using the keyword
Output: "global"
UnboundLocalError!:
Local variable s referenced before assignment
Guess The Output ?

s="I love Python"


def f():
global s
print(s)
s="I love C"
print(s)
f()
print(s)

Output:
I love Python
I love C
I love C
Guess The Output ?

a=1
Output:
def f():
global : 1
print ('Inside f() : ', a)
inside f( ):1
def g():
global: 1
a=2
inside g( ): 2
print ('Inside g() : ',a)
global : 1
def h():
inside h( ): 3
global a
global : 3
a=3
print ('Inside h() : ',a)

print ('global : ',a)


f()
print ('global : ',a)
g()
print ('global : ',a)
h()
print ('global : ',a)
Guess The Output ?

a=0 Output:
7
if a == 0: 3
b=1 0
1
def my_function(c): NameError!
d=3
print(c)
print(d)
my_function(7)
print(a)
print(b)
print(c)
print(d)
Guess The Output ?

def foo(x, y): Output:


42 17 4 17
global a 42 15 3 4
a = 42
x,y = y,x
b = 33
b = 17
c = 100
print(a,b,x,y)

a, b, x, y = 1, 15, 3,4
foo(17, 4)
print(a, b, x, y)
Argument Passing

 There are two ways to pass arguments/parameters


to function calls in C programming:

 Call by value

 Call by reference.
Call By Value

 In Call by value, original value is not modified.

 In Call by value, the value being passed to the


function is locally stored by the function parameter
as formal argument

 So , if we change the value of formal argument, it


is changed for the current function only.

 These changes are not reflected in the actual


argument’s value
Call By Reference

 In Call by reference , the location (address) of


actual argument is passed to formal arguments,
hence any change made to formal arguments will
also reflect in actual arguments.

 In Call by reference, original value is


modified because we pass reference (address).
What About Python ?

 When asked whether Python function calling


model is "call-by-value" or "call-by-reference",
the correct answer is: neither.

 What Python uses , is actually called "call-by-


object-reference"
A Quick Recap Of Variables

 We know that everything in Python is an object.

 All numbers , strings , lists , tuples etc in


Python are objects.

 Now , recall , what happens when we write the


following statement in Python:
x=10

 An object is created in heap , storing the value 10


and x becomes the reference to that object.
A Quick Recap Of Variables

 Also we must recall that in Python we have 2 types


of data : mutable and immutable.

 Immutable types are those which do not allow


modification in object’s data and examples are int ,
float , string ,tuple etc

 Mutable types are those which allow us to modify


object’s data and examples are list and dictionary
What Is
Call By Object Reference ?

 Now , when we pass immutable arguments like


integers, strings or tuples to a function, the
passing acts like call-by-value.

 The object reference is passed to the function


parameters.

 They can't be changed within the function, because


they can't be changed at all, i.e. they are
immutable.
What Is
Call By Object Reference ?

 It's different, if we pass mutable arguments.

 They are also passed by object reference, but


they can be changed in place in the function.

 If we pass a list to a function, elements of that list


can be changed in place, i.e. the list will be
changed even in the caller's scope.
Guess The Output ?

def show(a):
print("Inside show , a is",a," It's id is",id(a))
Since Python uses Pass
by object reference , so
when we passed a ,
a=10 Python passed the
address of the object
print("Outside show, a is",a," It's id is",id(a))
pointed by a and this
show(a) address was received by
the formal variable a in
the function’s argument
list. So both the
Output: references are pointing
to the same object
Guess The Output ?

def increment(a):
a=a+1
When we pass a to
a=10 increment(a), the function
has the local variable a
increment(a) referring to the same
object. Since integer is
print(a) immutable, so Python is
not able to modify the
object’s value to 11 in place
and thus it created a new
object. But the original
Output: variable a is still referring
to the same object with the
10 value 10
Guess The Output ?

def show(mynumbers):
print("Inside show , mynumbers is",mynumbers)
mynumbers.append(40)
print("Inside show , mynumbers is",mynumbers)
Since list is a mutable
type , so any change
mynumbers=[10,20,30] made in the formal
print("Before calling show, mynumbers
referenceis",mynumbers)
mynumbers
does not create a new
show(mynumbers) object in memory .
print("After calling show, mynumbers
Rather is",mynumbers)
it changes the
data stored in original
list
Output:
Guess The Output ?

def show(mynumbers):
mynumbers=[50,60,70]
print("Inside show , mynumbers is",mynumbers)
If we create a new
mynumbers=[10,20,30] object inside the
function , then
print("Before calling show, mynumbers is",mynumbers)
Python will make the
show(mynumbers) formal reference
mynumbers refer to
print("After calling show, mynumbers is",mynumbers))
that new object but
the actual argument
mynumbers , will still
Output: be refering to the
actual object
Guess The Output ?

def foo(x):
x.append (3)
x = [8]
return x
x=[1, 5]
y= foo(x)
print(x)
print(y)

Output:
[1,5,3]
[8]
Guess The Output ?

def swap(a,b):
a,b=b,a
a=10
b=20
swap(a,b)
print(a)
print(b)

Output:
10
20
Guess The Output ?

def changetoupper(s):
s=s.upper()
s="bhopal"
changetoupper(s)
print(s)

Output:
bhopal
Guess The Output ?

def changetoupper(s):
s=s.upper()
return s
s="bhopal"
s=changetoupper(s)
print(s)

Output:
BHOPAL

You might also like