# python functions (pyatl beginners night)

of 97/97
Python Functions Rick Copeland

Post on 24-Jun-2015

361 views

Category:

## Technology

Tags:

• #### parameters placeholders

Embed Size (px)

DESCRIPTION

Introduction to Python functions for those new to Python, programming, or both

TRANSCRIPT

• 1. Python FunctionsRick Copeland

2. Python FunctionsRick Copeland 3. Python FunctionsRick Copeland 4. BeginnersNightPython FunctionsRick Copeland 5. What is a function, anyway? 6. What is a function, anyway? Reusable bit of Python code 7. What is a function, anyway? Reusable bit of Python code beginning with the keyword def 8. What is a function, anyway? Reusable bit of Python code beginning with the keyword def that can use parameters (placeholders) 9. What is a function, anyway? Reusable bit of Python code beginning with the keyword def that can use parameters (placeholders) that can provide a return value 10. Define a Function>>> def x_squared(x):!... return x * x 11. Use a Function>>> def x_squared(x):!... return x * x!...!>>> x_squared(1)!1!>>> x_squared(2)!4!>>> x_squared(4)!16 12. Use a Function To actually execute thefunction weve defined, weneed to call or invoke it.>>> def x_squared(x):!... return x * x!...!>>> x_squared(1)!1!>>> x_squared(2)!4!>>> x_squared(4)!16 13. Use a Function To actually execute thefunction weve defined, weneed to call or invoke it. In Python, we call functionsby placing parentheses afterthe function name>>> def x_squared(x):!... return x * x!...!>>> x_squared(1)!1!>>> x_squared(2)!4!>>> x_squared(4)!16 14. Use a Function To actually execute thefunction weve defined, weneed to call or invoke it. In Python, we call functionsby placing parentheses afterthe function name providing argumentswhich match up with theparameters used whendefining the function>>> def x_squared(x):!... return x * x!...!>>> x_squared(1)!1!>>> x_squared(2)!4!>>> x_squared(4)!16 15. Substitutionx_squared(4) 16. Substitutionx_squared(4) >>> def x_squared(x):!... return x * x 17. Substitutionx_squared(4) >>> def x_squared(x):!... return x * xx = 4!return x * x 18. Substitutionx_squared(4) >>> def x_squared(x):!... return x * xx = 4!return x * x4 * 4 19. Substitutionx_squared(4) >>> def x_squared(x):!... return x * xx = 4!return x * x4 * 416 20. Recursion 21. Recursion Functions can call themselves 22. Recursion Functions can call themselves we call this recursion 23. Recursion>>> def x_fact(x):!... if x < 2:!... return 1!... else:!... return x * x_fact(x-1)!...!>>> x_fact(3)!6 24. Recursion, step-by-stepfact(3) 25. Recursion, step-by-stepfact(3)def x_fact(x):!if x < 2:!return 1!else:!return x * x_fact(x-1) 26. Recursion, step-by-stepfact(3)return 3 * x_fact(3-1)def x_fact(x):!if x < 2:!return 1!else:!return x * x_fact(x-1) 27. Recursion, step-by-stepfact(3)(3 * fact(2)) return 3 * x_fact(3-1)def x_fact(x):!if x < 2:!return 1!else:!return x * x_fact(x-1) 28. Recursion, step-by-stepfact(3)(3 * fact(2)) return 3 * x_fact(3-1)return 2 * x_fact(2-1)def x_fact(x):!if x < 2:!return 1!else:!return x * x_fact(x-1) 29. Recursion, step-by-stepfact(3)(3 * fact(2))(3 * (2 * fact(1)))return 3 * x_fact(3-1)return 2 * x_fact(2-1)def x_fact(x):!if x < 2:!return 1!else:!return x * x_fact(x-1) 30. Recursion, step-by-stepfact(3)(3 * fact(2))(3 * (2 * fact(1)))return 3 * x_fact(3-1)return 2 * x_fact(2-1)return 1def x_fact(x):!if x < 2:!return 1!else:!return x * x_fact(x-1) 31. Recursion, step-by-stepfact(3)(3 * fact(2))(3 * (2 * fact(1)))(3 * (2 * (1)))return 3 * x_fact(3-1)return 2 * x_fact(2-1)return 1def x_fact(x):!if x < 2:!return 1!else:!return x * x_fact(x-1) 32. Recursion, step-by-stepfact(3)(3 * fact(2))(3 * (2 * fact(1)))(3 * (2 * (1)))(3 * (2))return 3 * x_fact(3-1)return 2 * x_fact(2-1)return 1def x_fact(x):!if x < 2:!return 1!else:!return x * x_fact(x-1) 33. Recursion, step-by-stepfact(3)(3 * fact(2))(3 * (2 * fact(1)))(3 * (2 * (1)))(3 * (2))return 3 * x_fact(3-1)return 2 * x_fact(2-1)return 1(6)def x_fact(x):!if x < 2:!return 1!else:!return x * x_fact(x-1) 34. Different ways to callfunctions 35. Different ways to callfunctionsdef my_awesome_function(something): 36. Different ways to callfunctionsdef my_awesome_function(something): 37. Different ways to callfunctionsdef my_awesome_function(something): def my_awesome_function(something, something_else): 38. Different ways to callfunctionsdef my_awesome_function(something): def my_awesome_function(something, something_else): 39. Different ways to callfunctionsdef my_awesome_function(something): def my_awesome_function(something, something_else): def my_awesome_function(something, something_else, banana): 40. Different ways to callfunctionsdef my_awesome_function(something): def my_awesome_function(something, something_else): def my_awesome_function(something, something_else, banana): 41. Different ways to callfunctionsdef my_awesome_function(something): def my_awesome_function(something, something_else): def my_awesome_function(something, something_else, banana): def my_awesome_function(something, something_else, banana, apple): 42. Different ways to callfunctionsdef my_awesome_function(something): def my_awesome_function(something, something_else): def my_awesome_function(something, something_else, banana): def my_awesome_function(something, something_else, banana, apple): 43. Different ways to callfunctionsdef my_awesome_function(something): def my_awesome_function(something, something_else): def my_awesome_function(something, something_else, banana): def my_awesome_function(something, something_else, banana, apple): def my_awesome_function(something, something_else, banana, apple, pear): ... 44. Different ways to callfunctionsdef my_awesome_function(something): def my_awesome_function(something, something_else): def my_awesome_function(something, something_else, banana): def my_awesome_function(something, something_else, banana, apple): def my_awesome_function(something, something_else, banana, apple, pear): ...>>> my_awesome_function(1, 4, 6, ... # now was itbanana, apple, pear or apple, pear, banana?! 45. Named Arguments>>> my_awesome_function(!something=1,!something_else=4,!banana=6,!pear=9,!apple=12) 46. Default Arguments When youre calling a function, you have to givePython all the argument values (1 perparameter) 47. Default Arguments When youre calling a function, you have to givePython all the argument values (1 perparameter) but some of these can have default values 48. Default Arguments 49. Default Argumentsdef my_awesome_function(something, something_else, 50. Default Argumentsdef my_awesome_function(something, something_else,banana=1, apple=2, pear=3): ... 51. Default Argumentsdef my_awesome_function(something, something_else,banana=1, apple=2, pear=3): ...Now when you say this: 52. Default Argumentsdef my_awesome_function(something, something_else,banana=1, apple=2, pear=3): ...Now when you say this:>>> my_awesome_function(1, 4, 6) 53. Default Argumentsdef my_awesome_function(something, something_else,banana=1, apple=2, pear=3): ...Now when you say this:>>> my_awesome_function(1, 4, 6)It means this: 54. Default Argumentsdef my_awesome_function(something, something_else,banana=1, apple=2, pear=3): ...Now when you say this:>>> my_awesome_function(1, 4, 6)It means this:>>> my_awesome_function(!1, 4, 6,!apple=2, pear=3) 55. Variable Parameters Sometimes its nice to be able to call a functionwith different numbers of arguments 56. Variable Parameters Sometimes its nice to be able to call a functionwith different numbers of arguments something like sum(1,2,3) or sum(1,2) 57. Variable Parametersdef sum(*values):!result = 0!for v in values:!result += v!return result 58. Variable Parameters Python packs all theremaining arguments into atupledef sum(*values):!result = 0!for v in values:!result += v!return result 59. Variable Parameters Python packs all theremaining arguments into atuple You can then pass anynumber of positionalarguments to the functiondef sum(*values):!result = 0!for v in values:!result += v!return result 60. Variable Parameters Python packs all theremaining arguments into atuple You can then pass anynumber of positionalarguments to the functiondef sum(*values):!result = 0!for v in values:!result += v!return resultA tuple is like a list youcant modify, e.g. (1,2,3) 61. Variable Arguments>>> def sum(*values):!... result = 0!... for v in values:!... result += v!... return result!...!>>> sum(*[1,2,3])!6 62. Variable Arguments Python unpacks the tuple/list/etc. into separatearguments>>> def sum(*values):!... result = 0!... for v in values:!... result += v!... return result!...!>>> sum(*[1,2,3])!6 63. Variable Arguments Python unpacks the tuple/list/etc. into separatearguments You can call functions that usevariable or fixed argumentsthis way>>> def sum(*values):!... result = 0!... for v in values:!... result += v!... return result!...!>>> sum(*[1,2,3])!6 64. Variable Arguments Python unpacks the tuple/list/etc. into separatearguments You can call functions that usevariable or fixed argumentsthis way>>> def sum(*values):!... result = 0!... for v in values:!... result += v!... return result!...!>>> sum(*[1,2,3])!6>>> def x_times_y(x, y):!... return x * y!...!>>> x_times_y(*[4,2])!8 65. Keyword Parameters 66. Keyword Parameters Sometimes I want a function, but I dont knowwhat I want to name the arguments 67. Keyword Parameters Sometimes I want a function, but I dont knowwhat I want to name the arguments hard to come up with a really simple example,but hopefully this makes sense 68. Keyword Parameters 69. Keyword Parameters>>> def make_dict(**kwargs):!... result = {}!... for k, v in kwargs.items():!... result[k] = v!... return result!...!>>> make_dict(a=5, b=6)!{'a': 5, 'b': 6} 70. Keyword Parameters Python packs all theremaining named argumentsinto a dict>>> def make_dict(**kwargs):!... result = {}!... for k, v in kwargs.items():!... result[k] = v!... return result!...!>>> make_dict(a=5, b=6)!{'a': 5, 'b': 6} 71. Keyword Parameters Python packs all theremaining named argumentsinto a dict You can then pass anynumber of named argumentsto the function>>> def make_dict(**kwargs):!... result = {}!... for k, v in kwargs.items():!... result[k] = v!... return result!...!>>> make_dict(a=5, b=6)!{'a': 5, 'b': 6} 72. Keyword Parameters Python packs all theremaining named argumentsinto a dict You can then pass anynumber of named argumentsto the functionA dict is like a directorymapping keys tovalues (e.g. {key: value})>>> def make_dict(**kwargs):!... result = {}!... for k, v in kwargs.items():!... result[k] = v!... return result!...!>>> make_dict(a=5, b=6)!{'a': 5, 'b': 6} 73. Keyword Parameters Step byStep>>> make_dict(a=5, b=6) 74. Keyword Parameters Step byStep>>> make_dict(a=5, b=6) def make_dict(**kwargs):!result = {}!for k, v in kwargs.items():!result[k] = v!return result 75. Keyword Parameters Step byStep>>> make_dict(a=5, b=6) def make_dict(**kwargs):!result = {}!for k, v in kwargs.items():!result[k] = v!return resultkwargs = {'a': 5, 'b': 6}!make_dict(**kwargs) 76. Keyword Parameters Step byStep>>> make_dict(a=5, b=6) def make_dict(**kwargs):!result = {}!for k, v in kwargs.items():!result[k] = v!return resultkwargs = {'a': 5, 'b': 6}!make_dict(**kwargs)kwargs = {'a': 5, 'b': 6}!result = {}!for k, v in {'a': 5, 'b': 6}.items():!result[k] = v!return result 77. Keyword Parameters Step byStep>>> make_dict(a=5, b=6) def make_dict(**kwargs):!result = {}!for k, v in kwargs.items():!result[k] = v!return resultkwargs = {'a': 5, 'b': 6}!make_dict(**kwargs)kwargs = {'a': 5, 'b': 6}!result = {}!for k, v in {'a': 5, 'b': 6}.items():!result[k] = v!return resultresult = {}!result = {'a': 5}!result = {'a': 5, 'b': 6}!return result 78. Keyword Parameters Step byStep>>> make_dict(a=5, b=6) def make_dict(**kwargs):!result = {}!for k, v in kwargs.items():!result[k] = v!return resultkwargs = {'a': 5, 'b': 6}!make_dict(**kwargs)kwargs = {'a': 5, 'b': 6}!result = {}!for k, v in {'a': 5, 'b': 6}.items():!result[k] = v!return resultresult = {}!result = {'a': 5}!result = {'a': 5, 'b': 6}!return result{'a': 5, 'b': 6} 79. Keyword Arguments>>> make_dict(**{'a': 10, 'b': 20})!{'a': 10, 'b': 20} 80. Keyword Arguments Python unpacks the dict intoseparate arguments>>> make_dict(**{'a': 10, 'b': 20})!{'a': 10, 'b': 20} 81. Keyword Arguments Python unpacks the dict intoseparate arguments You can call functions that usekeyword or fixed argumentsthis way>>> make_dict(**{'a': 10, 'b': 20})!{'a': 10, 'b': 20} 82. Keyword Arguments>>> make_dict(**{'a': 10, 'b': 20})!{'a': 10, 'b': 20} Python unpacks the dict intoseparate arguments You can call functions that usekeyword or fixed argumentsthis way>>> dct = {'x': 2, 'y': 6}!>>> x_times_y(**dct)!12 83. Keyword Arguments Step byStep>>> dct = {'x': 2, 'y': 6}!>>> x_times_y(**dct) 84. Keyword Arguments Step byStep>>> dct = {'x': 2, 'y': 6}!>>> x_times_y(**dct)def x_times_y(x, y):!return x * y 85. Keyword Arguments Step byStep>>> dct = {'x': 2, 'y': 6}!>>> x_times_y(**dct)x_times_y(x=2, y=6)def x_times_y(x, y):!return x * y 86. Keyword Arguments Step byStep>>> dct = {'x': 2, 'y': 6}!>>> x_times_y(**dct)x_times_y(x=2, y=6)def x_times_y(x, y):!return x * yx=2!y=6!return x * y 87. Keyword Arguments Step byStep>>> dct = {'x': 2, 'y': 6}!>>> x_times_y(**dct)x_times_y(x=2, y=6)def x_times_y(x, y):!return x * yx=2!y=6!return x * yreturn 2 * 6 88. Keyword Arguments Step byStep>>> dct = {'x': 2, 'y': 6}!>>> x_times_y(**dct)x_times_y(x=2, y=6)def x_times_y(x, y):!return x * yx=2!y=6!return x * yreturn 2 * 612 89. Is there more? 90. Is there more? Of course theres more, but its beginners night ;-) 91. Is there more? Of course theres more, but its beginners night ;-) But what can I do now? 92. Is there more? Of course theres more, but its beginners night ;-) But what can I do now? Write functions to reuse code (DRY) 93. Is there more? Of course theres more, but its beginners night ;-) But what can I do now? Write functions to reuse code (DRY) Write recursive functions 94. Is there more? Of course theres more, but its beginners night ;-) But what can I do now? Write functions to reuse code (DRY) Write recursive functions Create and use functions with different kinds ofarguments and parameters (named, *args,**kwargs) 95. Any questions?Rick Copeland