1. Python Functions
• A collection of related assertions that carry out a mathematical,
analytical, or evaluative operation is known as a function.
2. Advantages of Python Functions
• Pause We can stop a program from repeatedly using the same code
block by including functions.
• Once defined, Python functions can be called multiple times and from
any location in a program.
• Our Python program can be broken up into numerous, easy-to-follow
functions if it is significant.
• The ability to return as many outputs as we want using a variety of
arguments is one of Python's most significant achievements.
• However, Python programs have always incurred overhead when calling
functions.
3. Syntax
# An example Python Function
def function_name( parameters ):
# code block
4. Illustration of a User-Defined Function
# Example Python Code for User-Defined function
def square( num ):
"""
This function computes the square of the number.
"""
return num**2
object_ = square(6)
print( "The square of the given number is: ", object_ )
5. Calling a Function
• Calling a Function To define a function, use the def keyword to give it a name, specify the
arguments it must receive, and organize the code block.
# Example Python Code for calling a function
# Defining a function
def a_function( string ):
"This prints the value of length of string"
return len(string)
# Calling the function we defined
print( "Length of the string Functions is: ", a_function( "Functions" ) )
print( "Length of the string Python is: ", a_function( "Python" ) )
6. Function Arguments
• The following are the types of arguments that we can use to call a
function:
1. Default arguments
2. Keyword arguments
3. Required arguments
4. Variable-length arguments
7. 1) Default Arguments
• A default contention is a boundary that takes as information a default
esteem, assuming that no worth is provided for the contention when
the capability is called. The following example demonstrates default
arguments.
8. # Python code to demonstrate the use of default arguments
# defining a function
def function( n1, n2 = 20 ):
print("number 1 is: ", n1)
print("number 2 is: ", n2)
# Calling the function and passing only one argument
print( "Passing only one argument" )
function(30)
# Now giving two arguments to the function
print( "Passing two arguments" )
function(50,30)
9. 2) Keyword Arguments
• Keyword arguments are linked to the arguments of a called function.
While summoning a capability with watchword contentions, the client
might tell whose boundary esteem it is by looking at the boundary
name.
10. # Python code to demonstrate the use of keyword arguments
# Defining a function
def function( n1, n2 ):
print("number 1 is: ", n1)
print("number 2 is: ", n2)
# Calling function and passing arguments without using keyword
print( "Without using keyword" )
function( 50, 30)
# Calling function and passing arguments using keyword
print( "With using keyword" )
function( n2 = 50, n1 = 30)
11. 3) Required Arguments
• Required arguments are those supplied to a function during its call in
a predetermined positional sequence. The number of arguments
required in the method call must be the same as those provided in
the function's definition.
12. • # Python code to demonstrate the use of default arguments
• # Defining a function
• def function( n1, n2 ):
• print("number 1 is: ", n1)
• print("number 2 is: ", n2)
•
• # Calling function and passing two arguments out of order, we need num1 to be 20 and num2 to be 30
• print( "Passing out of order arguments" )
• function( 30, 20 )
•
• # Calling function and passing only one argument
• print( "Passing only one argument" )
• try:
• function( 30 )
• except:
• print( "Function needs two positional arguments" )
13. 4) Variable-Length Arguments
• We can involve unique characters in Python capabilities to pass many
contentions. However, we need a capability. This can be accomplished
with one of two types of characters:
• "args" and "kwargs" refer to arguments not based on keywords.
14. • # Python code to demonstrate the use of variable-length arguments
• # Defining a function
• def function( *args_list ):
• ans = []
• for l in args_list:
• ans.append( l.upper() )
• return ans
• # Passing args arguments
• object = function('Python', 'Functions', 'tutorial')
• print( object )
•
• # defining a function
• def function( **kargs_list ):
• ans = []
• for key, value in kargs_list.items():
• ans.append([key, value])
• return ans
• # Paasing kwargs arguments
• object = function(First = "Python", Second = "Functions", Third = "Tutorial")
• print(object)
15. Python Lambda Functions
• Lambda Functions in Python are anonymous functions, implying they
don't have a name. The def keyword is needed to create a typical
function in Python, as we already know. We can also use the lambda
keyword in Python to define an unnamed function.
• Syntax
• lambda arguments: expression
16. # Code to demonstrate how we can use a lambda function for adding 4 n
umbers
add = lambda num: num + 4
print( add(6) )
def add( num ):
return num + 4
print( add(6) )
17. a = lambda x, y : (x * y)
print(a(4, 5))
a = lambda x, y, z : (x + y + z)
print(a(4, 5, 5))
18. What's the Distinction Between Lambda and
Def Functions?
# Python code to show the reciprocal of the given number to highlight the difference between def()
and lambda().
def reciprocal( num ):
return 1 / num
lambda_reciprocal = lambda num: 1 / num
# using the function defined by def keyword
print( "Def keyword: ", reciprocal(6) )
# using the function defined by lambda keyword
print( "Lambda keyword: ", lambda_reciprocal(6) )