0% found this document useful (0 votes)
10 views20 pages

Python Chapter 4 Functions - User Defined Fun

This document covers Chapter 4 of a Programming with Python course, focusing on functions, modules, and packages. It details the use of built-in and user-defined functions, including function definitions, calling, parameters, and variable scope. Additionally, it introduces anonymous functions using the lambda keyword and explains the differences between global and local variables.

Uploaded by

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

Python Chapter 4 Functions - User Defined Fun

This document covers Chapter 4 of a Programming with Python course, focusing on functions, modules, and packages. It details the use of built-in and user-defined functions, including function definitions, calling, parameters, and variable scope. Additionally, it introduces anonymous functions using the lambda keyword and explains the differences between global and local variables.

Uploaded by

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

Subject: Programming with Python (PWP-22616)

Class: TYCM-I

CHAPTER 4: Python Functions,


Modules and Packages
4.1 Use of Python built-in functions
4.2 User defined functions
4.3 Modules
4.4 Python Packages

Mrs. M. P. Bhosale 1
Subject: Programming with Python (PWP-22616)
Class: TYCM-I

CHAPTER 4: Python Functions,


Modules and Packages

Develop Functions, Modules and Packages


for given problems.
Use the Python standard functions for the given
4a
problem.
CO602.4 Develop relevant user defined functions for the
4b
given problem using Python code.
4c Write Python module for the given problem

4d Write Python package for the given problem.

Mrs. M. P. Bhosale 2
4.2 User defined functions:

4.2.1 Function definition,


Function calling,
4.2.2 function arguments and
parameter passing,
Return statement,
4.2.3 Scope of Variables:
Global variable and
Local Variable.
Mrs. M. P. Bhosale 3
4.2 User defined functions:
Function definition:
 In Python, function is a group of related statements that
perform a specific task.
 Functions help break our program into smaller and modular
chunks. As our program grows larger and larger, functions
make it more organized and manageable.
 Furthermore, it avoids repetition and makes code reusable.
 A function is a block of code which only runs when it is
called.
 You can pass data, known as parameters, into a function.
 A function can return data as a result.
 In Python a function is defined using the def keyword
Mrs. M. P. Bhosale 4
4.2 User defined functions:

Syntax of Function:
def function_name(parameters):
‘‘‘docstring’’’
statement(s)
return [Expression]
 Optional documentation string (docstring) to
describe what the function does.
 Return statement is also optional and a return
statement with no arguments is the same as return
None.
Mrs. M. P. Bhosale 5
4.2 User defined functions:
Function Calling:
 Once we have defined a function, we can call it from another
function, program or even the Python prompt.
 To call a function we simply type the function name with
appropriate parameters.
 E.g. Sample Function:
#Function Definition
def show():
print("Hello All")

#Function Calling
show()
Mrs. M. P. Bhosale 6
4.2 User defined functions:

Parameterized Function:
 Information can be passed to functions as
parameter.
 Parameters are specified after the function name,
inside the parentheses.
 You can add as many parameters as you want, just
separate them with a comma.

Mrs. M. P. Bhosale 7
4.2 User defined functions:

Parameterized Function: Example


n=int(input("Enter no:"))
def cube(X):
return X*X*X
print(cube(n))
O/P:
Enter no:5
125

Mrs. M. P. Bhosale 8
Parameterized functions:

Function Arguments
You can call a function by using the following types of
formal arguments −
 Required arguments
 Keyword arguments
 Default arguments
 Variable-length arguments

Mrs. M. P. Bhosale 9
Parameterized functions:
Required arguments
 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.
def add(n1,n2):
return n1+n2
add(23)
O/P:
add(23)
TypeError: add() missing 1 required positional
argument: 'n2'
Mrs. M. P. Bhosale 10
Parameterized functions:
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.
 This allows you to skip arguments or place them out of order
because the Python interpreter is able to use the keywords
provided to match the values with parameters.
def printinfo( name, age ):
print(name, ":", age)
printinfo(age=36,name="KKWP").
O/P:
KKWP : 36
Mrs. M. P. Bhosale 11
Parameterized functions:
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.
def add(n1,n2=23): def add(n1=23,n2):
print(n1+n2) print(n1+n2)
add(85) add(85)
O/P:
108 O/P:
def add(n1=23,n2):
SyntaxError: non-default argument
follows default argument
Mrs. M. P. Bhosale 12
Parameterized functions:
Variable-length arguments
 You may need to process a function for more arguments than you
specified while defining the function.
 These arguments are called variable-length arguments and are not
named in the function definition, unlike required and default
arguments
def functionname([formal_args,] *var_args_tuple ):
"function_docstring"
function_suite
return [expression]
An asterisk (*) is placed before the variable name that holds the values of
all nonkeyword variable arguments. This tuple remains empty if no
additional arguments are specified during the function call
Mrs. M. P. Bhosale 13
Parameterized functions:
Variable-length arguments: Example
def printlist(*arg2):
print(arg2)
printlist()
printlist(47)
printlist(10,20,30,40)
O/P:
()
(47,)
(10, 20, 30, 40)
Mrs. M. P. Bhosale 14
Return Statement:
 The statement return [expression] exits a
function, optionally passing back an
expression to the caller. A return statement
with no arguments is the same as return
None.
def add(n1,n2):
return n1+n2
sum=add(23,45)
print("Addition is:",sum)
O/P:
Addition is: 68
Mrs. M. P. Bhosale 15
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
identifier.
 There are two basic scope of variables in Python −
 Global variables
 Local variables

Mrs. M. P. Bhosale 16
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.
 When you call a function, the variables declared
inside it are brought into scope.

Mrs. M. P. Bhosale 17
Global vs. Local variables:
sum=0
def add(n1,n2):
sum=n1+n2
print("Inside",sum)

add(23,45)
print("Addition is:",sum)
O/P:
Inside 68
Addition is: 0
Mrs. M. P. Bhosale 18
Anonymous Functions:
 These functions are called anonymous because they are not
declared in the standard manner by using the def keyword.
 You can use the lambda keyword to create small anonymous
functions.
 Lambda forms can take any number of arguments but return
just one value in the form of an expression.
 They cannot contain commands or multiple expressions.
 An anonymous function cannot be a direct call to print
because lambda requires an expression
 Lambda functions have their own local namespace and
cannot access variables other than those in their parameter
list and those in the global namespace.

Mrs. M. P. Bhosale 19
Anonymous Functions:
 Syntax
lambda [arg1 [,arg2,.....,argn] ] :expression

 Example:
cube=lambda no: no**3
print("cube of is:",cube(5))

O/P:
cube of is: 125

Mrs. M. P. Bhosale 20

You might also like