python functions (pyatl beginners night)

97
Python Functions Rick Copeland

Upload: rick-copeland

Post on 24-Jun-2015

370 views

Category:

Technology


2 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Python Functions (PyAtl Beginners Night)

Python FunctionsRick Copeland

Page 2: Python Functions (PyAtl Beginners Night)

Python FunctionsRick Copeland

Page 3: Python Functions (PyAtl Beginners Night)

Python FunctionsRick Copeland

Page 4: Python Functions (PyAtl Beginners Night)

Python FunctionsRick Copeland

Beginner’s

Night

Page 5: Python Functions (PyAtl Beginners Night)

What is a function, anyway?

Page 6: Python Functions (PyAtl Beginners Night)

What is a function, anyway?

• Reusable bit of Python code

Page 7: Python Functions (PyAtl Beginners Night)

What is a function, anyway?

• Reusable bit of Python code

• … beginning with the keyword def

Page 8: Python Functions (PyAtl Beginners Night)

What is a function, anyway?

• Reusable bit of Python code

• … beginning with the keyword def

• … that can use parameters (placeholders)

Page 9: Python Functions (PyAtl Beginners Night)

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

Page 10: Python Functions (PyAtl Beginners Night)

Define a Function

>>> def x_squared(x):!... return x * x

Page 11: Python Functions (PyAtl Beginners Night)

Use a Function

>>> def x_squared(x):!... return x * x!...!>>> x_squared(1)!1!>>> x_squared(2)!4!>>> x_squared(4)!16

Page 12: Python Functions (PyAtl Beginners Night)

Use a Function• To actually execute the

function we’ve defined, we need to call or invoke it.

>>> def x_squared(x):!... return x * x!...!>>> x_squared(1)!1!>>> x_squared(2)!4!>>> x_squared(4)!16

Page 13: Python Functions (PyAtl Beginners Night)

Use a Function• To actually execute the

function we’ve defined, we need to call or invoke it.

• In Python, we call functions by placing parentheses after the function name…

>>> def x_squared(x):!... return x * x!...!>>> x_squared(1)!1!>>> x_squared(2)!4!>>> x_squared(4)!16

Page 14: Python Functions (PyAtl Beginners Night)

Use a Function• To actually execute the

function we’ve defined, we need to call or invoke it.

• In Python, we call functions by placing parentheses after the function name…

• … providing arguments which match up with the parameters used when defining the function

>>> def x_squared(x):!... return x * x!...!>>> x_squared(1)!1!>>> x_squared(2)!4!>>> x_squared(4)!16

Page 15: Python Functions (PyAtl Beginners Night)

Substitutionx_squared(4)

Page 16: Python Functions (PyAtl Beginners Night)

Substitutionx_squared(4)

>>> def x_squared(x):!... return x * x

Page 17: Python Functions (PyAtl Beginners Night)

Substitutionx_squared(4)

>>> def x_squared(x):!... return x * x

x = 4!return x * x

Page 18: Python Functions (PyAtl Beginners Night)

Substitutionx_squared(4)

>>> def x_squared(x):!... return x * x

x = 4!return x * x

4 * 4

Page 19: Python Functions (PyAtl Beginners Night)

Substitutionx_squared(4)

>>> def x_squared(x):!... return x * x

x = 4!return x * x

4 * 4

16

Page 20: Python Functions (PyAtl Beginners Night)

Recursion

Page 21: Python Functions (PyAtl Beginners Night)

Recursion

• Functions can call themselves

Page 22: Python Functions (PyAtl Beginners Night)

Recursion

• Functions can call themselves

• … we call this recursion

Page 23: Python Functions (PyAtl Beginners Night)

Recursion>>> def x_fact(x):!... if x < 2:!... return 1!... else:!... return x * x_fact(x-1)!...!>>> x_fact(3)!6

Page 24: Python Functions (PyAtl Beginners Night)

Recursion, step-by-stepfact(3)

Page 25: Python Functions (PyAtl Beginners Night)

Recursion, step-by-stepfact(3)

def x_fact(x):! if x < 2:! return 1! else:! return x * x_fact(x-1)

Page 26: Python Functions (PyAtl Beginners Night)

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)

Page 27: Python Functions (PyAtl Beginners Night)

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)

Page 28: Python Functions (PyAtl Beginners Night)

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)

Page 29: Python Functions (PyAtl Beginners Night)

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)

Page 30: Python Functions (PyAtl Beginners Night)

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 1

def x_fact(x):! if x < 2:! return 1! else:! return x * x_fact(x-1)

Page 31: Python Functions (PyAtl Beginners Night)

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 1

def x_fact(x):! if x < 2:! return 1! else:! return x * x_fact(x-1)

Page 32: Python Functions (PyAtl Beginners Night)

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

def x_fact(x):! if x < 2:! return 1! else:! return x * x_fact(x-1)

Page 33: Python Functions (PyAtl Beginners Night)

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)

Page 34: Python Functions (PyAtl Beginners Night)

Different ways to call functions

Page 35: Python Functions (PyAtl Beginners Night)

Different ways to call functions

def my_awesome_function(something): …

Page 36: Python Functions (PyAtl Beginners Night)

Different ways to call functions

def my_awesome_function(something): …

Page 37: Python Functions (PyAtl Beginners Night)

Different ways to call functions

def my_awesome_function(something): …

def my_awesome_function(something, something_else): …

Page 38: Python Functions (PyAtl Beginners Night)

Different ways to call functions

def my_awesome_function(something): …

def my_awesome_function(something, something_else): …

Page 39: Python Functions (PyAtl Beginners Night)

Different ways to call functions

def my_awesome_function(something): …

def my_awesome_function(something, something_else): …

def my_awesome_function(something, something_else, banana): …

Page 40: Python Functions (PyAtl Beginners Night)

Different ways to call functions

def my_awesome_function(something): …

def my_awesome_function(something, something_else): …

def my_awesome_function(something, something_else, banana): …

Page 41: Python Functions (PyAtl Beginners Night)

Different ways to call functions

def 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): …

Page 42: Python Functions (PyAtl Beginners Night)

Different ways to call functions

def 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): …

Page 43: Python Functions (PyAtl Beginners Night)

Different ways to call functions

def 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): ...

Page 44: Python Functions (PyAtl Beginners Night)

Different ways to call functions

def 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 it banana, apple, pear or apple, pear, banana?!

Page 45: Python Functions (PyAtl Beginners Night)

Named Arguments>>> my_awesome_function(! something=1,! something_else=4,! banana=6,! pear=9,! apple=12)

Page 46: Python Functions (PyAtl Beginners Night)

Default Arguments

• When you’re calling a function, you have to give Python all the argument values (1 per parameter)

Page 47: Python Functions (PyAtl Beginners Night)

Default Arguments

• When you’re calling a function, you have to give Python all the argument values (1 per parameter)

• … but some of these can have default values

Page 48: Python Functions (PyAtl Beginners Night)

Default Arguments

Page 49: Python Functions (PyAtl Beginners Night)

Default Argumentsdef my_awesome_function(something, something_else,

Page 50: Python Functions (PyAtl Beginners Night)

Default Argumentsdef my_awesome_function(something, something_else, banana=1, apple=2, pear=3): ...

Page 51: Python Functions (PyAtl Beginners Night)

Default Argumentsdef my_awesome_function(something, something_else, banana=1, apple=2, pear=3): ...

Now when you say this:

Page 52: Python Functions (PyAtl Beginners Night)

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)

Page 53: Python Functions (PyAtl Beginners Night)

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:

Page 54: Python Functions (PyAtl Beginners Night)

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)

Page 55: Python Functions (PyAtl Beginners Night)

Variable Parameters

• Sometimes it’s nice to be able to call a function with different numbers of arguments…

Page 56: Python Functions (PyAtl Beginners Night)

Variable Parameters

• Sometimes it’s nice to be able to call a function with different numbers of arguments…

• … something like sum(1,2,3) or sum(1,2)…

Page 57: Python Functions (PyAtl Beginners Night)

Variable Parameters

def sum(*values):! result = 0! for v in values:! result += v! return result

Page 58: Python Functions (PyAtl Beginners Night)

Variable Parameters

• Python “packs” all the remaining arguments into a tuple

def sum(*values):! result = 0! for v in values:! result += v! return result

Page 59: Python Functions (PyAtl Beginners Night)

Variable Parameters

• Python “packs” all the remaining arguments into a tuple

• You can then pass any number of positional arguments to the function

def sum(*values):! result = 0! for v in values:! result += v! return result

Page 60: Python Functions (PyAtl Beginners Night)

Variable Parameters

• Python “packs” all the remaining arguments into a tuple

• You can then pass any number of positional arguments to the function

def sum(*values):! result = 0! for v in values:! result += v! return result

A tuple is like a list you can’t modify, e.g. (1,2,3)

Page 61: Python Functions (PyAtl Beginners Night)

Variable Arguments>>> def sum(*values):!... result = 0!... for v in values:!... result += v!... return result!...!>>> sum(*[1,2,3])!6

Page 62: Python Functions (PyAtl Beginners Night)

Variable Arguments

• Python “unpacks” the tuple/list/etc. into separate arguments

>>> def sum(*values):!... result = 0!... for v in values:!... result += v!... return result!...!>>> sum(*[1,2,3])!6

Page 63: Python Functions (PyAtl Beginners Night)

Variable Arguments

• Python “unpacks” the tuple/list/etc. into separate arguments

• You can call functions that use variable or fixed arguments this way

>>> def sum(*values):!... result = 0!... for v in values:!... result += v!... return result!...!>>> sum(*[1,2,3])!6

Page 64: Python Functions (PyAtl Beginners Night)

Variable Arguments

• Python “unpacks” the tuple/list/etc. into separate arguments

• You can call functions that use variable or fixed arguments this way

>>> def x_times_y(x, y):!... return x * y!...!>>> x_times_y(*[4,2])!8

>>> def sum(*values):!... result = 0!... for v in values:!... result += v!... return result!...!>>> sum(*[1,2,3])!6

Page 65: Python Functions (PyAtl Beginners Night)

Keyword Parameters

Page 66: Python Functions (PyAtl Beginners Night)

Keyword Parameters

• Sometimes I want a function, but I don’t know what I want to name the arguments…

Page 67: Python Functions (PyAtl Beginners Night)

Keyword Parameters

• Sometimes I want a function, but I don’t know what I want to name the arguments…

• … hard to come up with a really simple example, but hopefully this makes sense…

Page 68: Python Functions (PyAtl Beginners Night)

Keyword Parameters

Page 69: Python Functions (PyAtl Beginners Night)

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}

Page 70: Python Functions (PyAtl Beginners Night)

Keyword Parameters

• Python “packs” all the remaining named arguments into 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}

Page 71: Python Functions (PyAtl Beginners Night)

Keyword Parameters

• Python “packs” all the remaining named arguments into a dict

• You can then pass any number of named arguments to 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}

Page 72: Python Functions (PyAtl Beginners Night)

Keyword Parameters

• Python “packs” all the remaining named arguments into a dict

• You can then pass any number of named arguments to 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}

A dict is like a directory mapping “keys” to

“values” (e.g. {key: value})

Page 73: Python Functions (PyAtl Beginners Night)

Keyword Parameters Step by Step

>>> make_dict(a=5, b=6)

Page 74: Python Functions (PyAtl Beginners Night)

Keyword Parameters Step by Step

>>> make_dict(a=5, b=6) def make_dict(**kwargs):! result = {}! for k, v in kwargs.items():! result[k] = v! return result

Page 75: Python Functions (PyAtl Beginners Night)

Keyword Parameters Step by Step

>>> make_dict(a=5, b=6) def make_dict(**kwargs):! result = {}! for k, v in kwargs.items():! result[k] = v! return result

kwargs = {'a': 5, 'b': 6}!make_dict(**kwargs)

Page 76: Python Functions (PyAtl Beginners Night)

Keyword Parameters Step by Step

>>> make_dict(a=5, b=6) def make_dict(**kwargs):! result = {}! for k, v in kwargs.items():! result[k] = v! return result

kwargs = {'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

Page 77: Python Functions (PyAtl Beginners Night)

Keyword Parameters Step by Step

>>> make_dict(a=5, b=6) def make_dict(**kwargs):! result = {}! for k, v in kwargs.items():! result[k] = v! return result

kwargs = {'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

result = {}!result = {'a': 5}!result = {'a': 5, 'b': 6}!return result

Page 78: Python Functions (PyAtl Beginners Night)

Keyword Parameters Step by Step

>>> make_dict(a=5, b=6) def make_dict(**kwargs):! result = {}! for k, v in kwargs.items():! result[k] = v! return result

kwargs = {'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

result = {}!result = {'a': 5}!result = {'a': 5, 'b': 6}!return result

{'a': 5, 'b': 6}

Page 79: Python Functions (PyAtl Beginners Night)

Keyword Arguments>>> make_dict(**{'a': 10, 'b': 20})!{'a': 10, 'b': 20}

Page 80: Python Functions (PyAtl Beginners Night)

Keyword Arguments

• Python “unpacks” the dict into separate arguments

>>> make_dict(**{'a': 10, 'b': 20})!{'a': 10, 'b': 20}

Page 81: Python Functions (PyAtl Beginners Night)

Keyword Arguments

• Python “unpacks” the dict into separate arguments

• You can call functions that use keyword or fixed arguments this way

>>> make_dict(**{'a': 10, 'b': 20})!{'a': 10, 'b': 20}

Page 82: Python Functions (PyAtl Beginners Night)

Keyword Arguments

• Python “unpacks” the dict into separate arguments

• You can call functions that use keyword or fixed arguments this way

>>> dct = {'x': 2, 'y': 6}!>>> x_times_y(**dct)!12

>>> make_dict(**{'a': 10, 'b': 20})!{'a': 10, 'b': 20}

Page 83: Python Functions (PyAtl Beginners Night)

Keyword Arguments Step by Step

>>> dct = {'x': 2, 'y': 6}!>>> x_times_y(**dct)

Page 84: Python Functions (PyAtl Beginners Night)

Keyword Arguments Step by Step

>>> dct = {'x': 2, 'y': 6}!>>> x_times_y(**dct)

def x_times_y(x, y):! return x * y

Page 85: Python Functions (PyAtl Beginners Night)

Keyword Arguments Step by Step

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

Page 86: Python Functions (PyAtl Beginners Night)

Keyword Arguments Step by Step

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

x=2!y=6!return x * y

Page 87: Python Functions (PyAtl Beginners Night)

Keyword Arguments Step by Step

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

x=2!y=6!return x * y

return 2 * 6

Page 88: Python Functions (PyAtl Beginners Night)

Keyword Arguments Step by Step

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

x=2!y=6!return x * y

return 2 * 6

12

Page 89: Python Functions (PyAtl Beginners Night)

Is there more?

Page 90: Python Functions (PyAtl Beginners Night)

Is there more?• Of course there’s more, but it’s beginner’s night ;-)

Page 91: Python Functions (PyAtl Beginners Night)

Is there more?• Of course there’s more, but it’s beginner’s night ;-)

• But what can I do now?

Page 92: Python Functions (PyAtl Beginners Night)

Is there more?• Of course there’s more, but it’s beginner’s night ;-)

• But what can I do now?

• Write functions to reuse code (DRY)

Page 93: Python Functions (PyAtl Beginners Night)

Is there more?• Of course there’s more, but it’s beginner’s night ;-)

• But what can I do now?

• Write functions to reuse code (DRY)

• Write recursive functions

Page 94: Python Functions (PyAtl Beginners Night)

Is there more?• Of course there’s more, but it’s beginner’s night ;-)

• But what can I do now?

• Write functions to reuse code (DRY)

• Write recursive functions

• Create and use functions with different kinds of arguments and parameters (named, *args, **kwargs)

Page 95: Python Functions (PyAtl Beginners Night)
Page 96: Python Functions (PyAtl Beginners Night)
Page 97: Python Functions (PyAtl Beginners Night)

–Rick Copeland

“Any questions?”