SlideShare a Scribd company logo
Module 3
Functions: Introduction to Functions, inbuilt functions,
user defined functions, passing parameters, return
values, recursion, Lambda functions
Functions
• A function is a block of code which only runs when it is
called.
• You can pass data, known as parameters, into a function.
Types:
• User defined functions
• Built-in-functions
Python built-in-function
• The Python built-in functions are defined as the functions
whose functionality is pre-defined in Python.
• abs() -Returns the absolute value of a number
• all() -Returns True if all items in an iterable object are
true
• any() -Returns True if any item in an iterable object is
true
• ascii() -Returns a readable version of an object. Replaces
none-ascii characters with escape character
User defined functions
• All the functions that are written by any us comes under
the category of user defined functions
• In Python, def keyword is used to declare user defined
functions.
• An indented block of statements follows the function name
and arguments which contains the body of the function.
Advantages for user defined function
• User-defined functions help to decompose a large
program into small segments which makes program easy
to understand, maintain and debug.
• If repeated code occurs in a program. Function can be
used to include those codes and execute when needed by
calling that function.
• Programmars working on large project can divide the
workload by making different functions.
Defining function
• In Python a function is defined using the def keyword:
def my_function():
print("Hello from a function")
Calling a Function
• To call a function, use the function name followed by
parenthesis:
def my_function():
print("Hello from a function")
my_function()
Function arguments
• Information can be passed into functions as arguments.
• Arguments are specified after the function name, inside
the parentheses. You can add as many arguments as you
want, just separate them with a comma.
• Required arguments
• Keyboard arguments
• Default arguments
• Variable-length arguments
Required arguments
• Arguments passed to a function in correct positional order.
• Here, the number of arguments in the function call should
match exactly with the function definition.
def printme( str ):
print str
return;
printme()
Keyboard arguments
• You can also send arguments with the key = value syntax.
• This way the order of the arguments does not matter.
def my_function(child3, child2, child1):
print("The youngest child is " + child3)
my_function(child1 = "Emil", child2 = "Tobias", child3
= "Linus")
Default arguments
• Default values indicate that the function argument will take that
value if no argument value is passed during the function call
• The default value is assigned by using the assignment(=)
operator of the form keyword name=value.passed during the
function call.
def student(firstname, lastname ='Mark', standard ='Fifth'):
print(firstname, lastname, 'studies in', standard,
'Standard')
Variable-length arguments
To process a function for more arguments than you
specified while defining the function. These arguments are
called variable-length arguments
def function name([formal_args,] *var_args_tuple ):
"function_docstring"
function_suite
return [expression]
Function parameters
• The terms parameter and argument can be used for the
same thing: information that are passed into a function.
• Name given in the function definition are called
parameters.
• Values supply in the function call are called arguments.
Anonymous function -LAMBDA
• A lambda function is a small anonymous function.
• A lambda function can take any number of arguments, but
can only have one expression.
• lambda arguments : expression
• x = lambda a : a + 10
print(x(5))
Use of Lambda Functions
• lambda is better shown when you use them as an
anonymous function inside another function.
def myfunc(n):
return lambda a : a * n
mydoubler = myfunc(2)
print(mydoubler(11))
filter() in python
• The filter() method filters the given sequence with the help of
a function that tests each element in the sequence to be true
or not.
filter(function, sequence)
Parameters:
• function: function that tests if each element of a sequence
true or not.
• sequence: sequence which needs to be filtered, it can be
sets, lists, tuples, or containers of any iterators.
• Returns: returns an iterator that is already filtered.
Map() function
• map() function returns a map object(which is an iterator)
of the results after applying the given function to each
item of a given iterable (list, tuple etc.)
map(fun, iter)
Parameters :
• fun : It is a function to which map passes each element of
given iterable.
iter : It is a iterable which is to be mapped.
Scope of variable
• A variable is only available from inside the region it is created.
This is called scope.
Local Scope
• A variable created inside a function belongs to the local scope
of that function, and can only be used inside that function.
Global Scope
• A variable created in the main body of the Python code is a
global variable and belongs to the global scope.
• Global variables are available from within any scope, global and
local.
Local variable:
def myfunc():
x = 300
print(x)
myfunc()
Global variable:
x = 300
def myfunc():
print(x)
myfunc()
print(x)
Function prototype
• Function without arguments and without return type
• Function with arguments and without return type
• Function without arguments and with return type
• Function with arguments and with return type
Function without arguments and without return type
• In this type no argument is
passed through the
function call and no output
is return to main function
• The sub function will read
the input values perform
the operation and print the
result in the same block
def add():
a=int(input("enter a"))
b=int(input("enter b"))
c=a+b
print(c)
add()
Function with arguments and without return type
• Arguments are passed
through the function call
but output is not return to
the main function
def add(a,b):
c=a+b
print(c)
a=int(input("enter a"))
b=int(input("enter b"))
add(a,b)
Function without arguments and with return
type
• In this type no argument is
passed through the
function call but output is
return to the main function.
def add(a,b):
c=a+b
return c
a=int(input("enter a"))
b=int(input("enter b"))
c=add(a,b)
print(c)
Function with arguments and with return type
• In this type arguments are
passed through the
function call and output is
return to the main function
def add(a,b):
c=a+b
return c
a=int(input("enter a"))
b=int(input("enter b"))
c=add(a,b)
print(c)
Call by value
• It is a way of passing arguments to a function in which the
arguments get copied to the formal parameters of a
function and are stored in different memory locations.
• Any changes made within the function are not reflected in
the actual parameters of the function when called.
Call by Reference
• It is a way of passing arguments to a function call in which
both the actual argument and formal parameters refer to
the same memory locations
• Any changes made within the function are reflected in the
actual parameters of the function when called.
Recursion
• Python also accepts function recursion, which means a defined
function can call itself.
def tri_recursion(k):
if(k>0):
result = k+tri_recursion(k-1)
print(result)
else:
result = 0
return result
print("nnRecursion Example Results")
tri_recursion(6)

More Related Content

PPTX
Function in Python.pptx by Faculty at gla university in mathura uttar pradesh
PDF
ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3
PPTX
_Python_ Functions _and_ Libraries_.pptx
PPTX
UNIT-02-pythonfunctions python function using detehdjsjehhdjejdhdjdjdjddjdhdhhd
PPT
python slides introduction interrupt.ppt
PPT
Powerpoint presentation for Python Functions
PPT
functions _
Function in Python.pptx by Faculty at gla university in mathura uttar pradesh
ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3
_Python_ Functions _and_ Libraries_.pptx
UNIT-02-pythonfunctions python function using detehdjsjehhdjejdhdjdjdjddjdhdhhd
python slides introduction interrupt.ppt
Powerpoint presentation for Python Functions
functions _

Similar to Python for Data Science function third module ppt.pptx (20)

PPT
Python programming variables and comment
PPTX
JNTUK python programming python unit 3.pptx
PPTX
Functions in Python Programming Language
PPT
Py-Slides-3 difficultpythoncoursefforbeginners.ppt
PPTX
Functions in python, types of functions in python
PPTX
CHAPTER 01 FUNCTION in python class 12th.pptx
PPTX
Chapter 2 Python Functions
PPTX
2_3 Functions 5d.pptx2_3 Functions 5d.pptx
PPTX
Functions in Python with all type of arguments
PPT
functions modules and exceptions handlings.ppt
PPTX
functions.pptx
PDF
Functions2.pdf
PPTX
Python programming - Functions and list and tuples
PDF
functions- best.pdf
PPTX
cbse class 12 Python Functions2 for class 12 .pptx
PDF
Functions-.pdf
PPTX
UNIT 3 python.pptx
PPTX
Function in Python function in python.pptx
PDF
Chapter Functions for grade 12 computer Science
PDF
Python functions
Python programming variables and comment
JNTUK python programming python unit 3.pptx
Functions in Python Programming Language
Py-Slides-3 difficultpythoncoursefforbeginners.ppt
Functions in python, types of functions in python
CHAPTER 01 FUNCTION in python class 12th.pptx
Chapter 2 Python Functions
2_3 Functions 5d.pptx2_3 Functions 5d.pptx
Functions in Python with all type of arguments
functions modules and exceptions handlings.ppt
functions.pptx
Functions2.pdf
Python programming - Functions and list and tuples
functions- best.pdf
cbse class 12 Python Functions2 for class 12 .pptx
Functions-.pdf
UNIT 3 python.pptx
Function in Python function in python.pptx
Chapter Functions for grade 12 computer Science
Python functions
Ad

More from bmit1 (20)

PPTX
AI-PoweJHJKJGJUJUUYKUKUKYUKYUKYUred Cybersecurity.pptx
PPTX
Creative innovation award certificate.pptx
PPT
PPT presentationhjvfitudtyicfuys7erstodxfciuggguys Phd.ppt
PPT
PPT pretdrtserstraswteateustrsentations Phd.ppt
PPTX
ICEATsssefggfdrghhkoolknvdesfjonbeeffd0.pptx
PPTX
ICEAT25043457uiifghjnnbdddfghhjnkkkkj0.pptx
PPT
BechYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYstein.ppt
PPT
shoemakerRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR.ppt
PPTX
INDUSTRY 4.0000000000000000000000000000000000000.pptx
PPTX
INTERNET OF THINGSSSSSSSSSSSSSSSSSSSSSSSSS.pptx
PPTX
SUBEDUCATIONNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN-PP...
PPTX
KIOT_DIV_Full Stack Vertical_Presentation_v0.1.pptx
PPTX
basiccomputerorganization-160726065251.pptx
PPTX
TLPpppppppppppppppppppppppppppresentation.pptx
PPTX
UNIT 2 - PYTHON FOR DATASCIENCE UNDER PROCEESING DATA .pptx
PPTX
Evaluation Reforms Osmania University.pptx
PPTX
Unit 2.7 PN junction diode & breakdown.pptx
PPTX
Unit 2.8 Metal-Semiconductor contact (1).pptx
PPTX
computer networksssssssssssssssssssssssssssss.pptx
PPTX
how to write a paper -2011151911324429.pptx
AI-PoweJHJKJGJUJUUYKUKUKYUKYUKYUred Cybersecurity.pptx
Creative innovation award certificate.pptx
PPT presentationhjvfitudtyicfuys7erstodxfciuggguys Phd.ppt
PPT pretdrtserstraswteateustrsentations Phd.ppt
ICEATsssefggfdrghhkoolknvdesfjonbeeffd0.pptx
ICEAT25043457uiifghjnnbdddfghhjnkkkkj0.pptx
BechYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYstein.ppt
shoemakerRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR.ppt
INDUSTRY 4.0000000000000000000000000000000000000.pptx
INTERNET OF THINGSSSSSSSSSSSSSSSSSSSSSSSSS.pptx
SUBEDUCATIONNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN-PP...
KIOT_DIV_Full Stack Vertical_Presentation_v0.1.pptx
basiccomputerorganization-160726065251.pptx
TLPpppppppppppppppppppppppppppresentation.pptx
UNIT 2 - PYTHON FOR DATASCIENCE UNDER PROCEESING DATA .pptx
Evaluation Reforms Osmania University.pptx
Unit 2.7 PN junction diode & breakdown.pptx
Unit 2.8 Metal-Semiconductor contact (1).pptx
computer networksssssssssssssssssssssssssssss.pptx
how to write a paper -2011151911324429.pptx
Ad

Recently uploaded (20)

PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
Construction Project Organization Group 2.pptx
PDF
PPT on Performance Review to get promotions
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
Welding lecture in detail for understanding
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
Well-logging-methods_new................
PPTX
UNIT 4 Total Quality Management .pptx
DOCX
573137875-Attendance-Management-System-original
PPTX
additive manufacturing of ss316l using mig welding
Embodied AI: Ushering in the Next Era of Intelligent Systems
Model Code of Practice - Construction Work - 21102022 .pdf
Construction Project Organization Group 2.pptx
PPT on Performance Review to get promotions
CYBER-CRIMES AND SECURITY A guide to understanding
Operating System & Kernel Study Guide-1 - converted.pdf
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Automation-in-Manufacturing-Chapter-Introduction.pdf
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Welding lecture in detail for understanding
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Well-logging-methods_new................
UNIT 4 Total Quality Management .pptx
573137875-Attendance-Management-System-original
additive manufacturing of ss316l using mig welding

Python for Data Science function third module ppt.pptx

  • 1. Module 3 Functions: Introduction to Functions, inbuilt functions, user defined functions, passing parameters, return values, recursion, Lambda functions
  • 2. Functions • A function is a block of code which only runs when it is called. • You can pass data, known as parameters, into a function. Types: • User defined functions • Built-in-functions
  • 3. Python built-in-function • The Python built-in functions are defined as the functions whose functionality is pre-defined in Python. • abs() -Returns the absolute value of a number • all() -Returns True if all items in an iterable object are true • any() -Returns True if any item in an iterable object is true • ascii() -Returns a readable version of an object. Replaces none-ascii characters with escape character
  • 4. User defined functions • All the functions that are written by any us comes under the category of user defined functions • In Python, def keyword is used to declare user defined functions. • An indented block of statements follows the function name and arguments which contains the body of the function.
  • 5. Advantages for user defined function • User-defined functions help to decompose a large program into small segments which makes program easy to understand, maintain and debug. • If repeated code occurs in a program. Function can be used to include those codes and execute when needed by calling that function. • Programmars working on large project can divide the workload by making different functions.
  • 6. Defining function • In Python a function is defined using the def keyword: def my_function(): print("Hello from a function") Calling a Function • To call a function, use the function name followed by parenthesis: def my_function(): print("Hello from a function") my_function()
  • 7. Function arguments • Information can be passed into functions as arguments. • Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma. • Required arguments • Keyboard arguments • Default arguments • Variable-length arguments
  • 8. Required arguments • Arguments passed to a function in correct positional order. • Here, the number of arguments in the function call should match exactly with the function definition. def printme( str ): print str return; printme()
  • 9. Keyboard arguments • You can also send arguments with the key = value syntax. • This way the order of the arguments does not matter. def my_function(child3, child2, child1): print("The youngest child is " + child3) my_function(child1 = "Emil", child2 = "Tobias", child3 = "Linus")
  • 10. Default arguments • Default values indicate that the function argument will take that value if no argument value is passed during the function call • The default value is assigned by using the assignment(=) operator of the form keyword name=value.passed during the function call. def student(firstname, lastname ='Mark', standard ='Fifth'): print(firstname, lastname, 'studies in', standard, 'Standard')
  • 11. Variable-length arguments To process a function for more arguments than you specified while defining the function. These arguments are called variable-length arguments def function name([formal_args,] *var_args_tuple ): "function_docstring" function_suite return [expression]
  • 12. Function parameters • The terms parameter and argument can be used for the same thing: information that are passed into a function. • Name given in the function definition are called parameters. • Values supply in the function call are called arguments.
  • 13. Anonymous function -LAMBDA • A lambda function is a small anonymous function. • A lambda function can take any number of arguments, but can only have one expression. • lambda arguments : expression • x = lambda a : a + 10 print(x(5))
  • 14. Use of Lambda Functions • lambda is better shown when you use them as an anonymous function inside another function. def myfunc(n): return lambda a : a * n mydoubler = myfunc(2) print(mydoubler(11))
  • 15. filter() in python • The filter() method filters the given sequence with the help of a function that tests each element in the sequence to be true or not. filter(function, sequence) Parameters: • function: function that tests if each element of a sequence true or not. • sequence: sequence which needs to be filtered, it can be sets, lists, tuples, or containers of any iterators. • Returns: returns an iterator that is already filtered.
  • 16. Map() function • map() function returns a map object(which is an iterator) of the results after applying the given function to each item of a given iterable (list, tuple etc.) map(fun, iter) Parameters : • fun : It is a function to which map passes each element of given iterable. iter : It is a iterable which is to be mapped.
  • 17. Scope of variable • A variable is only available from inside the region it is created. This is called scope. Local Scope • A variable created inside a function belongs to the local scope of that function, and can only be used inside that function. Global Scope • A variable created in the main body of the Python code is a global variable and belongs to the global scope. • Global variables are available from within any scope, global and local.
  • 18. Local variable: def myfunc(): x = 300 print(x) myfunc() Global variable: x = 300 def myfunc(): print(x) myfunc() print(x)
  • 19. Function prototype • Function without arguments and without return type • Function with arguments and without return type • Function without arguments and with return type • Function with arguments and with return type
  • 20. Function without arguments and without return type • In this type no argument is passed through the function call and no output is return to main function • The sub function will read the input values perform the operation and print the result in the same block def add(): a=int(input("enter a")) b=int(input("enter b")) c=a+b print(c) add()
  • 21. Function with arguments and without return type • Arguments are passed through the function call but output is not return to the main function def add(a,b): c=a+b print(c) a=int(input("enter a")) b=int(input("enter b")) add(a,b)
  • 22. Function without arguments and with return type • In this type no argument is passed through the function call but output is return to the main function. def add(a,b): c=a+b return c a=int(input("enter a")) b=int(input("enter b")) c=add(a,b) print(c)
  • 23. Function with arguments and with return type • In this type arguments are passed through the function call and output is return to the main function def add(a,b): c=a+b return c a=int(input("enter a")) b=int(input("enter b")) c=add(a,b) print(c)
  • 24. Call by value • It is a way of passing arguments to a function in which the arguments get copied to the formal parameters of a function and are stored in different memory locations. • Any changes made within the function are not reflected in the actual parameters of the function when called.
  • 25. Call by Reference • It is a way of passing arguments to a function call in which both the actual argument and formal parameters refer to the same memory locations • Any changes made within the function are reflected in the actual parameters of the function when called.
  • 26. Recursion • Python also accepts function recursion, which means a defined function can call itself. def tri_recursion(k): if(k>0): result = k+tri_recursion(k-1) print(result) else: result = 0 return result print("nnRecursion Example Results") tri_recursion(6)