-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Rick Copeland <[email protected]>
- Loading branch information
Showing
32 changed files
with
950 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"metadata": { | ||
"name": "02 - Useful Builtin Functions" | ||
"name": "02-Builtins" | ||
}, | ||
"nbformat": 2, | ||
"worksheets": [ | ||
|
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"metadata": { | ||
"name": "03 - File IO" | ||
"name": "03-FileIO" | ||
}, | ||
"nbformat": 2, | ||
"worksheets": [ | ||
|
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"metadata": { | ||
"name": "04 - Using Modules" | ||
"name": "04-UsingModules" | ||
}, | ||
"nbformat": 2, | ||
"worksheets": [ | ||
|
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"metadata": { | ||
"name": "05 - String Processing" | ||
"name": "05-Strings" | ||
}, | ||
"nbformat": 2, | ||
"worksheets": [ | ||
|
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"metadata": { | ||
"name": "06 - Regular Expressions" | ||
"name": "06-Regex" | ||
}, | ||
"nbformat": 2, | ||
"worksheets": [ | ||
|
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,342 @@ | ||
{ | ||
"metadata": { | ||
"name": "07-Functions" | ||
}, | ||
"nbformat": 2, | ||
"worksheets": [ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"source": [ | ||
"# Functions in Python", | ||
"", | ||
"Functions without arguments are straighforward. You can use the `def` keyword to define a", | ||
"function:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"collapsed": false, | ||
"input": [ | ||
"def myfunction():", | ||
" print 'Called myfunction'", | ||
"myfunction()" | ||
], | ||
"language": "python", | ||
"outputs": [ | ||
{ | ||
"output_type": "stream", | ||
"stream": "stdout", | ||
"text": [ | ||
"Called myfunction" | ||
] | ||
} | ||
], | ||
"prompt_number": 2 | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"source": [ | ||
"We can also define functions with arguments:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"collapsed": false, | ||
"input": [ | ||
"def myfunction2(a, b):", | ||
" print 'Called myfunction2(%r,%r)' % (a,b)", | ||
"myfunction2('avalue', 'bvalue')" | ||
], | ||
"language": "python", | ||
"outputs": [ | ||
{ | ||
"output_type": "stream", | ||
"stream": "stdout", | ||
"text": [ | ||
"Called myfunction2('avalue','bvalue')" | ||
] | ||
} | ||
], | ||
"prompt_number": 14 | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"source": [ | ||
"When calling a function with arguments, you may also pass the arguments *by name* rather than ", | ||
"*positionally.* This is often useful when there are a large number of arguments and you ", | ||
"don't want to remember the order in which they appear." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"collapsed": false, | ||
"input": [ | ||
"myfunction2(b='bvalue first', a='avalue second')" | ||
], | ||
"language": "python", | ||
"outputs": [ | ||
{ | ||
"output_type": "stream", | ||
"stream": "stdout", | ||
"text": [ | ||
"Called myfunction2('avalue second','bvalue first')" | ||
] | ||
} | ||
], | ||
"prompt_number": 15 | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"source": [ | ||
"## `lambda` functions", | ||
"", | ||
"Python provides a special form of defining functions that consist of nothing more than a single", | ||
"expression using the `lambda` keyword:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"collapsed": false, | ||
"input": [ | ||
"lambda_adder = lambda a,b: a+b", | ||
"lambda_adder(1, 2)" | ||
], | ||
"language": "python", | ||
"outputs": [ | ||
{ | ||
"output_type": "pyout", | ||
"prompt_number": 19, | ||
"text": [ | ||
"3" | ||
] | ||
} | ||
], | ||
"prompt_number": 19 | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"source": [ | ||
"Note the fact that `lambda` returns the function object itself. This form is often used", | ||
"when a function needs to be passed as an argument to another function, as in a callback. Also", | ||
"note the fact that the `return` statement is implicit.", | ||
"", | ||
"The equivalent function defined with `def` would be the following" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"collapsed": false, | ||
"input": [ | ||
"def lambda_adder_equiv(a,b):", | ||
" return a+b", | ||
"lambda_adder_equiv(1,2)" | ||
], | ||
"language": "python", | ||
"outputs": [ | ||
{ | ||
"output_type": "pyout", | ||
"prompt_number": 21, | ||
"text": [ | ||
"3" | ||
] | ||
} | ||
], | ||
"prompt_number": 21 | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"source": [ | ||
"## Default arguments", | ||
"", | ||
"You can define a function with default argument values. If a value is not passed for an argument", | ||
"with a default value, the default will be used instead:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"collapsed": false, | ||
"input": [ | ||
"def myfunction3(a, b='default value'):", | ||
" print 'Called myfunction3(%r,%r)' % (a,b)", | ||
"myfunction3('avalue')" | ||
], | ||
"language": "python", | ||
"outputs": [ | ||
{ | ||
"output_type": "stream", | ||
"stream": "stdout", | ||
"text": [ | ||
"Called myfunction3('avalue','default value')" | ||
] | ||
} | ||
], | ||
"prompt_number": 17 | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"source": [ | ||
"You can, of course, override the default:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"collapsed": false, | ||
"input": [ | ||
"myfunction3('avalue', 'bvalue')" | ||
], | ||
"language": "python", | ||
"outputs": [ | ||
{ | ||
"output_type": "stream", | ||
"stream": "stdout", | ||
"text": [ | ||
"Called myfunction3('avalue','bvalue')" | ||
] | ||
} | ||
], | ||
"prompt_number": 18 | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"source": [ | ||
"## Variable arguments", | ||
"", | ||
"We can define a function that takes any number of arguments using the `*args` syntax. The", | ||
"value of the `args` parameter below is any *positional* arguments that remain after ", | ||
"accounting for other arguments:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"collapsed": false, | ||
"input": [ | ||
"def va_adder(prompt, *args):", | ||
" print 'Called va_adder(%r, *%r)' % (prompt, args)", | ||
" return sum(args)", | ||
"va_adder('ThePrompt>', 1,2,3)" | ||
], | ||
"language": "python", | ||
"outputs": [ | ||
{ | ||
"output_type": "stream", | ||
"stream": "stdout", | ||
"text": [ | ||
"Called va_adder('ThePrompt>', *(1, 2, 3))" | ||
] | ||
}, | ||
{ | ||
"output_type": "pyout", | ||
"prompt_number": 26, | ||
"text": [ | ||
"6" | ||
] | ||
} | ||
], | ||
"prompt_number": 26 | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"source": [ | ||
"Likewise, we can call a function with a `tuple` of arguments using a similar syntax:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"collapsed": false, | ||
"input": [ | ||
"def normal_function(a,b):", | ||
" print 'Called normal_function(%r, %r)' % (a,b)", | ||
"argument_tuple = ('avalue', 'bvalue')", | ||
"normal_function(*argument_tuple)" | ||
], | ||
"language": "python", | ||
"outputs": [ | ||
{ | ||
"output_type": "stream", | ||
"stream": "stdout", | ||
"text": [ | ||
"Called normal_function('avalue', 'bvalue')" | ||
] | ||
} | ||
], | ||
"prompt_number": 25 | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"source": [ | ||
"If you want to define a function with variable *keyword* arguments, you can do that as well", | ||
"with the `**kwargs` syntax. In this case, the keyword arguments are passed as a `dict`:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"collapsed": false, | ||
"input": [ | ||
"def kw_function(**kwargs):", | ||
" print kwargs", | ||
"kw_function(a='avalue', b='bvalue')" | ||
], | ||
"language": "python", | ||
"outputs": [ | ||
{ | ||
"output_type": "stream", | ||
"stream": "stdout", | ||
"text": [ | ||
"{'a': 'avalue', 'b': 'bvalue'}" | ||
] | ||
} | ||
], | ||
"prompt_number": 27 | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"source": [ | ||
"Of course, we can also pass a dictionary as the keyword arguments of a function:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"collapsed": false, | ||
"input": [ | ||
"arguments = { 'a': 'avalue', 'b': 'bvalue' }", | ||
"normal_function(**arguments)" | ||
], | ||
"language": "python", | ||
"outputs": [ | ||
{ | ||
"output_type": "stream", | ||
"stream": "stdout", | ||
"text": [ | ||
"Called normal_function('avalue', 'bvalue')" | ||
] | ||
} | ||
], | ||
"prompt_number": 28 | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"source": [ | ||
"### Exercise", | ||
"", | ||
"Write a function with the signature `def log(format, *args, **kwargs):` which prints a ", | ||
"line, formatted according to the format string. Some sample results are below:", | ||
"", | ||
" >>> log('The pair is (%r,%r)', 1, 2)", | ||
" The pair is (1,2)", | ||
" >>> log('The value of a is %(a)r', a='foo')", | ||
" The value of a is 'foo'" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"collapsed": true, | ||
"input": [], | ||
"language": "python", | ||
"outputs": [] | ||
} | ||
] | ||
} | ||
] | ||
} |
Oops, something went wrong.