0% found this document useful (0 votes)
2 views

Scope and argument types

The document explains the scope of variables in Python, distinguishing between global and local variables. It also covers different types of function arguments, including required, keyword, default, and variable-length arguments, with examples for each type. The document emphasizes how variable scope affects accessibility and how arguments can be passed to functions in various ways.

Uploaded by

itsrinitoshi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Scope and argument types

The document explains the scope of variables in Python, distinguishing between global and local variables. It also covers different types of function arguments, including required, keyword, default, and variable-length arguments, with examples for each type. The document emphasizes how variable scope affects accessibility and how arguments can be passed to functions in various ways.

Uploaded by

itsrinitoshi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

FUNCTION- SESSION2

Scope of Variables
All variables in a program may not be accessible at all locations in that program. This
depends on where you have declared a variable.
The scope of a variable determines the portion of the program where you can access a
particular variable/identifier. There are two basic types of scopes of variables in Python
:

 Global variables
 Local variables

Global vs. Local variables


Variables that are defined inside a function body have a local scope, and those defined
outside have a global scope.
This means that local variables can be accessed only inside the function in which they
are declared, whereas global variables can be accessed throughout the program body
by all functions i.e inside or outside of the function. When you call a function, the
variables declared inside it are called into scope. See the examples –
Ex.1
a = 10 # This is global variable.
def out( a):
a=20 # Here a is local variable.
return
# Now you can call a function
out(a )
print (" a is now: ", a )

When the above code is executed, it gives the following output −


Answer:__________
Ex.2

a = 10 # This is global variable.

def out( ):
a=20 # Here sum is local variable.
print ("Inside the function local a : ",a)
return

# Now you can call sum function


out( )
print ("Outside the function global a : ", a )

When the above code is executed, it gives the following output −


Answer : ____________________
______________________

Ex.3

Live Demo
sum = 0 # This is global variable.

Def out( a, b ):
sum = a+b # Here sum is local variable.
Print (“Inside the function local sum : “, sum)
return sum

# Now you can call sum function


out( 10, 20 )
print (“Outside the function global sum : “, sum )

When the above code is executed, it gives the following output –


Answer : ____________________
______________________
Function Arguments

You can call a function by using the following types of formal arguments –

 Required arguments
 Keyword arguments
 Default arguments
 Variable-length arguments

Required Arguments
Required arguments are the arguments passed to a function in correct positional order.
Here, the number of arguments in the function call should match exactly with the
function definition
.
Ex.1. To call the function fun(), you need to pass one argument, otherwise it gives a
syntax error like−

def fun( a ):
print (a)
fun()

Live
When the above code is executed, it gives the error as follows –

Traceback (most recent call last):


File “test.py”, line 11, in <module>
fun();
TypeError: fun() takes exactly 1 argument (0 given)
Keyword Arguments
Keyword arguments are related to the function calls. When you use keyword
arguments in a function call, the caller identifies the arguments by the parameter name.
Ex.4.Live Demo

def out(st):
print(st)
return
out(st=”My school”)

When the above code is executed, it gives the following output –

Answer:__________

The following example gives you more clearer picture. Note that the order of
parameters does not matter.
Ex.5Live Demo
def out( name, roll ):
print (“Name: “, name)
print (“Roll: “, roll)
return
out(roll= 50, name = “rajni” )

When the above code is executed, it gives the following output –


Answer:__________
Default Arguments

A default argument is an argument that assumes a default value if a value is not


provided in the function call for that argument. The following example gives you the
concept of default arguments-

Ex.6Live Demo

Def out( name,roll = 60 ):


print (“Name: “, name)
print (“Roll “, roll)
return
out( roll = 50, name = “raj” ) or out(name = “raj”,roll=50 )
out( name = “kapoor” )

When the above code is executed, it gives the following output –


Answer:__________
Variable-length Arguments
When you need to process a function for more arguments than you specified while
defining the function, These arguments are known as variable-length arguments and
are not named in the function definition, unlike required and default arguments.
An asterisk (*) is placed before the variable name.
Following is a example –
Ex.7
Ex.7EEEEEEeeeeLive Demo

def out( a, *var ):


print ("Output is: ")
print (a)

for i in var:
print (i)
return

out( 100 )
out( 200, 300, 400 )

When the above code is executed, it gives the following output −


Answer:__________

*****

BY. R K TIWARI SIR

You might also like