Python UNIT III-Part-1
Python UNIT III-Part-1
Python UNIT III-Part-1
Python Programming
Unit – 3
Dr. S. P. Ponnusamy
Assistant Professor and Head
Syllabus
• UNIT III
Functions – Built in functions – function definition and calling –
return statement – void function – scope and lifetime of
variables – args and kwargs – command line arguments - Tuples
– creation – basic tuple operations – tuple() function – indexing
– slicing – built-in functions used on tuples – tuple methods –
packing – unpacking – traversing of tuples – populating tuples –
zip() function - Sets – Traversing of sets – set methods –
frozenset.
–.
Government Arts and Science College
Tittagudi – 606 106
Department of Computer Science
Python Programming
Functions
Functions - Introduction
A function is a named part of a program with a smaller size that is used
to perform a specific task.
The functions may be used by other parts of the program to perform
complicated tasks.
Any repeated component in a program can be replaced by a function,
which avoids more lines of code.
The function gets into execution only when it is called from any place in
the program. It can be called any number of times in the program
Functions - Introduction
Main Module
:
Fun1()
:
Code of Fun1()
Fun1()
:
Fun1()
:
Fun1()
:
Functions - Introduction
Main Module 2
:
Fun1()
1 :
:
Fun1() #function call
3
:
:
:
:
4 :
:
:
:
Program 5.1
Functions - Arguments
To achieve the task that is defined inside the function, some information is
needed from the calling place.
These pieces of information can be passed into functions by a caller as
arguments.
The arguments are specified after the function name, inside the parentheses.
The arguments are used as input to the function to accomplish the task.
The arguments are classified into two categories depending on the position
of the arguments : actual and formal arguments.
Functions - Arguments
The arguments which are used in the function call are referred to as
actual arguments
The arguments which are used in the function definition are referred to
as formal arguments
Formal arguments
def func1 (a,b,c) :
--------------
--------------
--------------
func1(x,y,z)
Actual arguments
Functions – Return
A function may contain zero or more return statements.
The return statement is optional and can be used only when it is necessary to
return value(s) to the caller (the programme actually calls the function).
The return statements can be situated anywhere in the function body after
the function header.
A return statement ends the execution of the function call and "returns" the
result, which is specified within the parenthesis to the caller.
The return() takes zero or more values.
Any statement after the return statement is not executed by the compiler.
Functions – Return
If the return statement is without a value, the special value "None" is
returned.
If there is no return statement in the function code, the function ends,
when the control flow reaches the end of the function body and the
value None will be returned. This kind of function is called as void
functions.
Program 5.6
Scope of Variable
A program can contain multiple identifiers to refer to different types of
objects.
A single object can be referred to by many identifiers.
A single identifier name can be used to refer to different objects.
To maintain the mapping between objects and identifiers, Python uses
namespaces.
The location and lifetime of the identifier/variable is determined by
variable scope.
Program 5.7
Government Arts and Science College
Tittagudi – 606 106
Department of Computer Science
Python Programming
Program 5.8
Government Arts and Science College
Tittagudi – 606 106
Department of Computer Science
Python Programming
Program 5.9
UnboundLocalError: local variable 'a' referenced before assignment
Program 5.12
Python allows the modification of the global variable in a function by using the
global keyword.
It informs the interpreter that the variable defined with the global keyword is
referred to as a global variable in the main body of the program.
The content of the global variable is allowed for modification in the function. It is
not a local variable.
Program 5.14
args parameters
Python allows us to handle the variable-length, non-keyworded
arguments.
In the function definition, we use an asterisk (*) before the argument name
to indicate variable length arguments in the order.
The variable length argument holds all the non-keyword variable
arguments as a tuple.
This tuple remains empty if no additional arguments are specified during
the function call.
Program 5.16
Government Arts and Science College
Tittagudi – 606 106
Department of Computer Science
Python Programming
args parameters
kwargs parameters
Python allows us to handle variable-length, keyworded arguments.
In the function definition, we use a double asterisk (**) before the
argument name to indicate keyworded variable length arguments in the
order.
The variable length argument holds all the keyworded arguments as a
dictionary.
This dictionary remains empty if no arguments are specified for the kwargs
during the function call.
Program 5.17
Government Arts and Science College
Tittagudi – 606 106
Department of Computer Science
Python Programming
End