Python Functions (PyAtl Beginners Night)

Post on 24-Jun-2015

370 views 2 download

Tags:

description

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

Transcript of Python Functions (PyAtl Beginners Night)

Python FunctionsRick Copeland

Python FunctionsRick Copeland

Python FunctionsRick Copeland

Python FunctionsRick Copeland

Beginner’s

Night

What is a function, anyway?

What is a function, anyway?

• Reusable bit of Python code

What is a function, anyway?

• Reusable bit of Python code

• … beginning with the keyword def

What is a function, anyway?

• Reusable bit of Python code

• … beginning with the keyword def

• … that can use parameters (placeholders)

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

Define a Function

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

Use a Function

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

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

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

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

Substitutionx_squared(4)

Substitutionx_squared(4)

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

Substitutionx_squared(4)

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

x = 4!return x * x

Substitutionx_squared(4)

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

x = 4!return x * x

4 * 4

Substitutionx_squared(4)

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

x = 4!return x * x

4 * 4

16

Recursion

Recursion

• Functions can call themselves

Recursion

• Functions can call themselves

• … we call this recursion

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

Recursion, step-by-stepfact(3)

Recursion, step-by-stepfact(3)

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

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)

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)

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)

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)

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)

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)

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)

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)

Different ways to call functions

Different ways to call functions

def my_awesome_function(something): …

Different ways to call functions

def my_awesome_function(something): …

Different ways to call functions

def my_awesome_function(something): …

def my_awesome_function(something, something_else): …

Different ways to call functions

def my_awesome_function(something): …

def my_awesome_function(something, something_else): …

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

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

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

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

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

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

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

Default Arguments

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

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

Default Arguments

Default Argumentsdef my_awesome_function(something, something_else,

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

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

Now when you say this:

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)

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:

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)

Variable Parameters

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

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

Variable Parameters

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

Variable Parameters

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

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

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

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)

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

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

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

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

Keyword Parameters

Keyword Parameters

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

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…

Keyword Parameters

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}

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}

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}

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

Keyword Parameters Step by Step

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

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

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)

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

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

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}

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

Keyword Arguments

• Python “unpacks” the dict into separate arguments

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

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}

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}

Keyword Arguments Step by Step

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

Keyword Arguments Step by Step

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

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

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

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

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

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

Is there more?

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

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

• But what can I do now?

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)

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

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)

–Rick Copeland

“Any questions?”