SlideShare a Scribd company logo
Python Functions
• A collection of related assertions that carry out a mathematical,
analytical, or evaluative operation is known as a function.
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.
Syntax
# An example Python Function
def function_name( parameters ):
# code block
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_ )
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" ) )
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
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.
# 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)
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.
# 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)
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.
• # 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" )
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.
• # 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)
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
# 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) )
a = lambda x, y : (x * y)
print(a(4, 5))
a = lambda x, y, z : (x + y + z)
print(a(4, 5, 5))
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) )
Chapter 2               Python Functions

More Related Content

PPT
functions _
PPT
python slides introduction interrupt.ppt
PPT
Powerpoint presentation for Python Functions
PPTX
Python for Data Science function third module ppt.pptx
PPT
Python programming variables and comment
PPTX
_Python_ Functions _and_ Libraries_.pptx
PPTX
Functions in Python Programming Language
PPT
functions modules and exceptions handlings.ppt
functions _
python slides introduction interrupt.ppt
Powerpoint presentation for Python Functions
Python for Data Science function third module ppt.pptx
Python programming variables and comment
_Python_ Functions _and_ Libraries_.pptx
Functions in Python Programming Language
functions modules and exceptions handlings.ppt

Similar to Chapter 2 Python Functions (20)

PPTX
JNTUK python programming python unit 3.pptx
PPTX
Functions in python, types of functions in python
PPTX
UNIT 3 python.pptx
PPT
Py-Slides-3 difficultpythoncoursefforbeginners.ppt
PPTX
Lecture 08.pptx
PPTX
Python Functions.pptx
PPTX
Functions in python slide share
PPTX
Functions in Python with all type of arguments
PPTX
Python programming - Functions and list and tuples
PPTX
2_3 Functions 5d.pptx2_3 Functions 5d.pptx
PPTX
Function in Python.pptx by Faculty at gla university in mathura uttar pradesh
PPTX
functions in python By Eng. Osama Ghandour الدوال فى البايثون مع مهندس اسامه ...
PDF
Dive into Python Functions Fundamental Concepts.pdf
PPTX
Learn more about the concepts Functions of Python
PDF
Python functions
PDF
Python Function.pdf
PPTX
cbse class 12 Python Functions2 for class 12 .pptx
PPTX
CHAPTER 01 FUNCTION in python class 12th.pptx
PPTX
Functions and Modules.pptx
JNTUK python programming python unit 3.pptx
Functions in python, types of functions in python
UNIT 3 python.pptx
Py-Slides-3 difficultpythoncoursefforbeginners.ppt
Lecture 08.pptx
Python Functions.pptx
Functions in python slide share
Functions in Python with all type of arguments
Python programming - Functions and list and tuples
2_3 Functions 5d.pptx2_3 Functions 5d.pptx
Function in Python.pptx by Faculty at gla university in mathura uttar pradesh
functions in python By Eng. Osama Ghandour الدوال فى البايثون مع مهندس اسامه ...
Dive into Python Functions Fundamental Concepts.pdf
Learn more about the concepts Functions of Python
Python functions
Python Function.pdf
cbse class 12 Python Functions2 for class 12 .pptx
CHAPTER 01 FUNCTION in python class 12th.pptx
Functions and Modules.pptx
Ad

Recently uploaded (20)

PPTX
Lesson notes of climatology university.
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
master seminar digital applications in india
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
RMMM.pdf make it easy to upload and study
PDF
Sports Quiz easy sports quiz sports quiz
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Classroom Observation Tools for Teachers
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
Institutional Correction lecture only . . .
PDF
01-Introduction-to-Information-Management.pdf
Lesson notes of climatology university.
Microbial disease of the cardiovascular and lymphatic systems
master seminar digital applications in india
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
RMMM.pdf make it easy to upload and study
Sports Quiz easy sports quiz sports quiz
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Classroom Observation Tools for Teachers
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
TR - Agricultural Crops Production NC III.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
STATICS OF THE RIGID BODIES Hibbelers.pdf
Renaissance Architecture: A Journey from Faith to Humanism
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
102 student loan defaulters named and shamed – Is someone you know on the list?
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Institutional Correction lecture only . . .
01-Introduction-to-Information-Management.pdf
Ad

Chapter 2 Python Functions

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