Learnbay.Datascience (2) PowerPoint PPT Presentation

presentation player overlay
About This Presentation
Transcript and Presenter's Notes

Title: Learnbay.Datascience (2)


1
FUNCTIONS IN PYTHON
2
CONCEPT OF FUNCTION IN PYTHON
  • A function is a series of statements that take inp
    uts, perform some particular computation, and gen
    erate output. 
  • The idea is to bring together some frequently or 
    repeatedly performed task and create a function, s
  • that instead of writing the same code again and ag
    ain for multiple levels, we can call the function
  • Python offers built-in functions like print() and 
    so 
  • on but we can also build your own functions. 
  • These functions are known as user-defined function
    s.
  • Note
  • Function definition is not function execution .To
    execute you need to all a function.

3
Syntax of function
  • A parameter is the variable listed inside the
    parentheses in the function definition .An
    argument is the value that is sent to the
    function when it is called.

4
Rules to define a function
  • Here are some simple rules for defining a function
     in Python
  • Function blocks start with the def keyword followe
    d by the name of the function and the parenthesis
     (()). 
  • Any input parameters or arguments should be placed
     within these brackets. Inside these brackets, yo
    u can also define parameters. 
  • The first statement of a function can be an option
    al statement-a document string of a function or a 
    document string. 
  • The code block within each function starts with a 
    colon () and is indented. 
  • The expression return statement exits the functi
    on, and optionally returns the expression to the 
    caller. 
  • The return statement with no arguments is the same
     as the return of None.

5
  • Eg whether x is even or odd def even Odd( x
    )
  •   if (x 2 0)
  •          print "even"
  •      else print "odd"
  • even Odd(2)
  • even Odd(3)
  • O/P even odd

6
Eg 2
7
Types of arguments
  • There are 4 types of arguments in python.

8
Positional/Required Arguments
  • Required arguments are the arguments passed to a
    function in correct positional order. During
    function call, values passed through arguments
    should be in the order of parameters in the
    function definition.
  • Hence, the number of arguments in the function
    call should match exactly with the function
    definition else it gives error.

9
E.g. of Positional Arguments
  • E.g. def add(a, b , c) return (a b c )
    print (add(10,20,30))
  • Output60
  • Note10 is assigned to a,20 is assigned
    to b and 30 is assigned to c

10
Key worded Arguments
  • Here function parameters are mentioned while
    calling the function as well.
  • You can also send arguments with
    the key  value syntax.
  • Thus values can be given in any position or
    order.
  • Also, if we have positional and key worded
    arguments in same function then key worded
    argument should always come after the positional
    argument else it gives error.

11
E.g.
  • def add(a , b , c , d) total sum  a b c
    d  total _ prod  abcd return total _
    sum, total _ prodadd(d  30, c  90, a  10, b  
    40)
  • O/P 170 1080000

12
Eg 2
  • Mix of positional arguments and key
    worded arguments  key worded arguments should al
    ways come after the positional arguments
  • def add( a, b ,c ,d ) total _ sum  a b c
    d  total _ prod  abcd return total
    _ sum, total _ prodadd( 30,  90, d 10, c 40)
  • O/P 170 1080000

13
 Default Parameters/Arguments 
  • E.g. def add( a, b, c  0,d  0)  print( a,
    b, c, d ) total _ sum  a b c
    d    return total _ sumprint(add(10,20))
  • O/P30 a10,b20,c0,d0
  • print(add(10,20,30))
  • O/P60 ...a10,b20,c30,d0

14
  • Mix of positional and default - default should fol
    low the positional
  • E.g.def add( a, b, c  0,d  0) total _
    sum  a b c d  return total _
    sumprint(add(10,20,d  30))
  • O/P 60

15
Variable length arguments
  • These are basically of 2 types
  • argsIn this case all the arguments are accepted
    in the form of tuple. Any number of arguments are
    accepted.
  • kwargs variable length key worded argument
    Here, all the arguments are accepted in the form
    of dictionary.

16
Example of args
  • def add(var)  print(var, type(var))
      total  1   for i in var     
    total  total  i    print(total) add(10)
  • O/P (10,) ltclass 'tuple'gt 10
  • add(10,20)
  • O/P (10, 20) ltclass 'tuple'gt
    200  

17
Example of kwargs
  • def add(var)   print(var, type(var)) 
    total  0    for i in var
     total  total  vari  print(total)add(a  10
    )
  • O/P 'a' 10 ltclass 'dict'gt 10
  • add(a10,b20,c30,d40,e50)
  • O/P'a' 10, 'b' 20, 'c' 30, 'd' 40, 'e'
    50 ltclass 'dict'gt150

18
Thanks For Watching!
  • For more such content visit http//bit.ly/Datasci
    enceCourse
  • Or follow us on our social media platforms
  • Facebook Learnbay
  • Instagram Learnbay_datascience
  • Twitter Learnbay1
  • LiknkedIn Learnbay
  • Youtube (Tutorials) https//www.youtube.com/chann
    el/UC-ntE_GnjjiUuKYqih9ENYA
Write a Comment
User Comments (0)
About PowerShow.com