U2 1 Functions
U2 1 Functions
Lecture 1 Unit 2
FUNCTIONS
🞆 Functions
🞆 Strings
🞆 Lists
🞆 Tuple
🞆 Dictionaries
2
FUNCTIONS
🞆 Defining a function
🞆 Calling a function
🞆 Types of functions
🞆 Function Arguments
🞆 Anonymous functions
🞆 Global and local variables
3
DEFINING A FUNCTION
● 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.
Creating a Function
4
DEFINING A FUNCTION
def function_name(parameters):
"""docstring"""
statement(s) 5
CALLING A FUNCTION
To call a function, use the function
name followed by parenthesis:
Example
def my_function():
print("Hello from a
function")
6
Calling Function
TYPES OF FUNCTIONS
Functions are of two types:
1.Built-in functions - Functions that
are built into Python.
2.User-defined functions -
Functions defined by the users
themselves.
7
FUNCTION ARGUMENTS
In Python, user-defined functions can
take four different types of arguments.
1. Default arguments
2. Required arguments
3. Keyword arguments
4. Arbitrary arguments 8
FUNCTION ARGUMENTS
1. Default arguments
Function definition
def defaultArg( name, msg =
"Hello!"):
Function call
9
defaultArg( name)
FUNCTION ARGUMENTS
1. Default arguments - Example
def defaultArg(name, msg =
"Hello!"):
defaultArg("Alice")
defaultArg("Bob","Good Morning!") 10
Function definition
def requiredArg (str,num):
Function call
requiredArg ("Hello",12)
11
FUNCTION ARGUMENTS
2. Required arguments - Example
def requiredArg(name, msg):
requiredArg("Bob","Good
Morning!")
requiredArg("Alice") 12
Function definition
def keywordArg( name, msg ):
Function call
keywordArg( name = "Alice", msg = "hi")
13
keywordArg( msg = "hello", name = "Bob")
FUNCTION ARGUMENTS
3. Keyword arguments - Example
def keywordArg(name, msg):
Function definition
def varlengthArgs(*vArgs):
Function call
varlengthArgs(10,20,30,40) 15
FUNCTION ARGUMENTS
4. Arbitrary arguments - Example
def varlengthArgs(*vArgs):
for i in vArgs:
print("Hello, My age is ", i)
varlengthArgs(10,20,30,40)
OUTPUT: Hello, My age is 10
16
Hello, My age is 20
ANONYMOUS FUNCTION
● In Python, anonymous function is a
function that is defined without a name.
● While normal functions are defined using
the def keyword, in Python anonymous
functions are defined using the lambda
keyword.
● Hence, anonymous functions are also
called lambda functions. 17
ANONYMOUS FUNCTION
SYNTAX
lambda arguments: expression
EXAMPLE
double = lambda x: x * 2
print(double(5))
OUTPUT: 10 18
lambda with filter()
The filter() function in Python takes in
a function and a list as arguments.
EXAMPLE
# filter out only the Multiples of 5 from a
list: lst = [3, 5, 4, 9, 8, 15, 50, 17]
OUTPUT: local
25
GLOBAL AND LOCAL VARIABLES
Example 6: Global variable and Local
variable with same name
x=5
OUTPUT:
def foo():
local x: 10
x = 10
print("local x:", x) global x: 5
foo()
print("global x:", x) 26
GLOBAL AND LOCAL VARIABLES
Example 7: Create a nonlocal variable
def outer():
x = "local"
def inner(): OUTPUT:
nonlocal x
x = "nonlocal" inner: nonlocal
print("inner:", x) outer: nonlocal
inner()
print("outer:", x) 27
outer()