0% found this document useful (0 votes)
4 views22 pages

Functions Day4

The document explains variable scope in Python, detailing local and global scopes. Local variables are accessible only within their function, while global variables can be accessed throughout the entire program. It also discusses the behavior of mutable and immutable arguments when passed to functions, providing examples and outputs for clarity.

Uploaded by

opradium
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)
4 views22 pages

Functions Day4

The document explains variable scope in Python, detailing local and global scopes. Local variables are accessible only within their function, while global variables can be accessed throughout the entire program. It also discusses the behavior of mutable and immutable arguments when passed to functions, providing examples and outputs for clarity.

Uploaded by

opradium
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/ 22

FUNCTION-

SCOPE OF
VARIABLES
What is Variable Scope?
Parts of Program within which
a variable is accessible, is
known Variable Scope.
TYPES OF VARIABLE SCOPE
There are broadly two kinds
of scopes in python-
1.LOCAL SCOPE
2.GLOBAL SCOPE
LOCAL SCOPE
• Variables declared in a function body is said to have Local Scope i.e it can
be used only within this function.

• It cannot be accessed outside the function.

• Parameters (Formal arguments) have local Scope.

• In this scope, the lifetime of variables is as long as the function executes.

They are destroyed once we return from the function. Hence, a function

does not remember the value of a variable from its previous calls.
LOCAL SCOPE
Example:

OUTPUT:

In the above code, x is local to my_func() ,hence accessible inside the function
only.if we try to print its value outside function “NameError” will come.
GLOBAL SCOPE
• A Variable declared in top level segment(_main_)of a program is said to have a
Global Scope.
• A Global Variable is accessible inside the whole program and all
blocks(functions, other block of codes) contained in the program.
• Lifetime of a Global Variable is the period throughout which the variable exists
in the memory.
Example:
def my_func():
x = 10
OUTPUT:
print("Value inside function:",x) Value inside function: 10
x = 20 Value outside function: 20
my_func()
print("Value outside function:",x)
Here,
we can see that the value of x is 20 initially.
Even though the function my_func()changed the value of x to
10,
it did not affect the value outside the function. This is
because the variable x inside the function is different (local
to the function) from the one outside. Although they have
same names, they are two different variables with different
scope.
Use of “global” keyword

Example:
def my_func():
global x
print("Value inside function:",x)
x = 20
my_func()
print("Value outside function:",x)
Output:
Value inside function: 20
Value outside function: 20
IMMUTABLE ARGUMENTS
(passing immutable type value to a function)
Example:
def my_func(n):
n=n+2
print("Value inside function:",n)
x = 20
my_func(x)
print("Value outside function:",x)
Output:
Value inside function: 22
Value outside function: 20
So in the above example
the function my_func() received the passed value in parameter n and then changed the value of n to 22 but after returning from my_func(), the
originally passed variable remains unchanged as it is immutable. Lets understand how,

20

Local envir.
x n
Global Environment
Inside the function, my_func() , n is now refer to 22 but the value of
x is still referring to 20 .Hence Value of x remain unchanged.

20 22

Local envir.
x n
Global Environment
MUTABLE ARGUMENTS
(Passing Mutable type value to
function)
Example:
def increment(n):
n.append([4])

L = [1, 2, 3]
increment(L)
print(L)

Output
L = [1, 2, 3, 4]
The code L = [1, 2, 3] has the variable L refer to the list object
containing references to three immutable objects: integer 1, 2, and 3

When we pass L to
increment() , local
variable n refer to
same object.
The .append() method change the value in place as List is
mutable.

Since no new object is created and the change occurred in


place of the object, when we print L, we get the modified
list.
Give the output of the
following code:
num=1
def f():
global num
num=10
return num
print(num)
print(f())
print(num)
Output
1
10
10
Give the output of the
following code:
A=10
Y=5
def call():
A=Y
A=2
print("Y=",Y,"A=",A)
print("A+Y=",A+Y)
return A+Y
print("Y=",Y,"A=",A)
print(call())
print("Y=",Y,"A=",A)
Output
Y= 5 A= 10
Y= 5 A= 2
A+Y= 7
7
Y= 5 A= 10
Find and write the output of the following snippet:

def check(n1=1,n2=2):
n1=n1+n2
n2+=1
print(n1,n2)
check()
check(2,1)
check(3)
OUTPUT

33
32
53
Find and write the output of the following snippet:

def interest(prnc,time=2,rate=0.10):
return(prnc*time*rate)

print(interest(6100,1))
print(interest(5000,rate=0.05))
print(interest(5000,3,0.12))
print(interest(time=4,prnc=5000))
OUTPUT
610.0
500.0
1800.0
2000.0

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy