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

U2 1 Functions

Uploaded by

Satish Kumar
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)
14 views

U2 1 Functions

Uploaded by

Satish Kumar
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/ 27

PYTHON PROGRAMMING

Lecture 1 Unit 2
FUNCTIONS

Name: Mr. Dheeraj Sundaragiri


Assistant Professor
Department of Computer Science and
Engineering
UNIT 2 - CONTENTS

🞆 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

In Python a function is defined using the


def keyword.
Syntax:

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!"):

print("Hello",name + ', ' + msg)

defaultArg("Alice")

defaultArg("Bob","Good Morning!") 10

OUTPUT: Hello Alice, Hello!


FUNCTION ARGUMENTS
2. Required arguments

Function definition
def requiredArg (str,num):
Function call
requiredArg ("Hello",12)
11
FUNCTION ARGUMENTS
2. Required arguments - Example
def requiredArg(name, msg):

print("Hello",name + ', ' + msg)

requiredArg("Bob","Good
Morning!")

requiredArg("Alice") 12

OUTPUT: Hello Bob, Good Morning!


FUNCTION ARGUMENTS
3. Keyword arguments

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):

print("Hello",name + ', ' + msg)


keywordArg( name = "Alice", msg =
"hi")

keywordArg( msg = "hello", name =


14
"Bob")
FUNCTION ARGUMENTS
4. Arbitrary arguments

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]

new_lst = list(filter(lambda x: (x%5 !=


0),lst)) # Output: [3, 4, 9, 8, 17] 19
lambda with map()
The map() function in Python takes in
a function and a list.
EXAMPLE

# square all the items in a list:


lst = [3, 5, 4, 9, 8, 15, 50, 17]

new_lst = list(map(lambda x: x**2,lst))


# Output: [9, 25, 16, 81, 64, 225, 2500, 289] 20
GLOBAL AND LOCAL VARIABLES
Example 1: Create a Global Variable
x = "global"
def foo():
print("x inside :", x)
foo()
print("x outside:", x)

OUTPUT: x inside : global 21


x outside: global
GLOBAL AND LOCAL VARIABLES
Example 2: UnboundLocalError
x = "global"
def foo():
x=x*2
print(x)
foo()

OUTPUT: UnboundLocalError: local 22


variable 'x' referenced before assignment
GLOBAL AND LOCAL VARIABLES
Example 3: global keyword
x = "global"
def foo():
global x
x=x*2
print(x)
foo()
23
OUTPUT: globalglobal
GLOBAL AND LOCAL VARIABLES
Example 4: Accessing local variable
outside the scope.
def foo():
y = "local"
foo()
print(y)
OUTPUT: NameError: name 'y' is not 24
defined
GLOBAL AND LOCAL VARIABLES
Example 5: Create a Local Variable
def foo():
y = "local"
print(y)
foo()

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()

You might also like