Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
20 views

Functions in Python

The document discusses different types of functions in Python including built-in functions, user-defined functions, default arguments, keyword arguments, variable length arguments, lambda functions, and properties of functions such as functions being objects that can be passed as arguments or returned from other functions.

Uploaded by

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

Functions in Python

The document discusses different types of functions in Python including built-in functions, user-defined functions, default arguments, keyword arguments, variable length arguments, lambda functions, and properties of functions such as functions being objects that can be passed as arguments or returned from other functions.

Uploaded by

Sirjandeep Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

FUNCTIONS IN

PYTHON

Abhishek Sharma
Functions
 A block of related statements designed to
perform a computational, logical, or
evaluative task.
 Two types:1 Built-in functions

2 User defined functions


Syntax
def function_name(parameters):
"""docstring"""
statement(s)
return expression
Abhishek Sharma
Sample Program
def evenOdd(x):
if (x % 2 == 0):
print("even")
else:
print("odd")

# Driver code to call the function


evenOdd(10)

output: even
Abhishek Sharma
Types of Arguments
 Default arguments
 Keyword arguments
 Variable length arguments

Abhishek Sharma
Default arguments
def myFun(x, y=50):
print("x: ", x)
print("y: ", y)
myFun(10)
Output:
x: 10
y: 50
Note: once a default value is given all arguments
to its right must also have default value.

Abhishek Sharma
Keyword arguments
 No need to remember the order of parameters
def student(fname,lname):
print(fname,lname)
student(lname=‘Thakur’, fname=‘Aksita ’)
Output:
Aksita, Thakur

Abhishek Sharma
Variable length arguments
Example:
def myFun(*args):
for arg in args:
print(arg)
myFun('Hello', 'Welcome', 'to', ‘Python')
Output: Hello
Welcome
to
Python
Abhishek Sharma
Variable length keyword arguments

def myFun(**kwargs):
for key, value in kwargs.items():
print("%s == %s" % (key, value))
myFun(first=‘hello', second=‘all',
third=‘Python')
Output: first == hello
second == all
third == Python

Abhishek Sharma
Is Python Function Pass by Reference or pass by value?

 In Python every variable name is a


reference.
 When we pass a variable to a function, a
new reference to the object is created.
def myFun(x):
x[0] = 20
lst = [10, 11, 12, 13, 14, 15]
myFun(lst)
print(lst)
Output: [20,11,12,13,14,15]
Abhishek Sharma
 When we pass a reference and change the
received reference to something else, the
connection between the passed and
received parameter is broken.
def myFun(x):
x = [20, 30, 40]
lst = [10, 11, 12, 13, 14, 15]
myFun(lst)
print(lst)
Output: [10, 11, 12, 13, 14, 15]

Abhishek Sharma
Example
def myFun(x):
x = 20
x = 10
myFun(x)
print(x)
Output: 10

Abhishek Sharma
Lambda Or Anonymous Functions
 Lambda or anonymous functions are created
using the lambda keyword.
 Lambda functions are throw-away functions, i.e.
they are just needed where they have been
created and can be used anywhere a function is
required.
 The lambda feature was added to Python due to
the demand from LISP programmers.
 Lambda functions contain only a single line.
Abhishek Sharma
 Its syntax can be given as,

Example:

Abhishek Sharma
Pass lambda func as argument
def func(f, n):
print(f(n))
twice = lambda x : x * 2
thrice = lambda x : x * 3
func(twice,4)
func(thrice,3)
Output: 8
9

Abhishek Sharma
Properties of Functions
 Function is an instance of the object type.
 You can store function in a variable.
 You can pass function as a parameter to
another function.
 You can return the function from a function.
 You can store them in data structures such as
hash tables, lists etc.

Abhishek Sharma
Functions are objects
def shout(text):
return text.upper()
print(shout(‘hello’))
y = shout
print(y(‘hello’))
Output:
HELLO
HELLO

Abhishek Sharma
Function passed as argument
def shout(text):
return text.upper()
def whisper(text):
return text.lower()
def greet(func):
greeting = func(‘’’Hi,function passed as an
argument’’’)
print(greeting)
greet(shout)
greet(whisper)
Abhishek Sharma
Function return Function
def create_adder(x):
print("x in create_adder:",x)
def adder(y):
print("x in adder:",x)
print("y in adder:",y)
return x+y
return adder
add_15=create_adder(15)
print(add_15(10))

Abhishek Sharma
Solve Output
def change(p,q=30):
print(“q”,q)
p=p+q
q=p-q
print(“change”,p,”#”,q)
return(p)
r,s=150,100
r= change(r,s)
print(“main”,r,”#”,s)
s= change(s)

Abhishek Sharma
OUTPUT
q= 100
Change: 250 # 150
Main: 250 # 100
q= 30
change= 130 # 100

Abhishek Sharma
Nested Functions can access variables
of parent function
def f1():
s= “variable of f1”
def f2():
print(s)
f2()
f1()

Output: variable of f1

Abhishek Sharma
Global Keyword
c=10
def add():
global c
c=c+2
print(“inside add:”,c)
add()
c=15
Print(“In main :”,c)

Output: inside add:12


In main : 15
Abhishek Sharma

You might also like