"Remember the previous exercise of finding (unique) substrings of length 3.\n",
"\n",
"* Make a function from your implementation.\n",
"* Have `k` as an argument to the function.\n",
"* Test the function on several input strings.\n",
"\n",
"**Note:** Editing multi-line statements in the console can be frustrating. You can try the QT console (`ipython qtconsole`) or edit your function in an editor with `%edit`:\n",
"\n",
" def my_function(arg):\n",
" print arg * 4\n",
" %edit my_function"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"$\\S$ Exercise: k-mer counting (2/2)\n",
"===\n",
"\n",
"\n",
"Modify your function to use a dictionary with substring counts.\n",
"<ol>\n",
" <li>Write a Python function that returns the maximum of two numbers.</li>\n",
" <li>Write a Python function that returns the maximum of three numbers. Try to reuse the first maximum of two numbers function.</li>\n",
" <li>Write a Python function that accepts a string as parameter. Next, it calculates and prints the number of upper case letters and lower case letters. Make us of the `isupper` and `islower` built in methods.</li>\n",
"</ol>\n",
"\n",
"\n",
"* Use the substrings as dictionary keys.\n",
"</div>"
"* Use the counts as dictionary values.\n",
"* Have the function return the dictionary.\n",
"* Add a docstring to the function.\n",
"* Use the function to print k-mer counts for some strings."
]
]
},
},
{
{
...
@@ -708,7 +690,9 @@
...
@@ -708,7 +690,9 @@
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 23,
"execution_count": 23,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"outputs": [],
"source": [
"source": [
"%edit examples/fsquare.py"
"%edit examples/fsquare.py"
...
@@ -737,22 +721,6 @@
...
@@ -737,22 +721,6 @@
" b = a"
" b = a"
]
]
},
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"$\\S$ Exercise: Running code from a file\n",
"===\n",
"\n",
"* Save your k-mer counting code to a file `kmer_counting.py`.\n",
"* Include some code using it on an example string and printing the results.\n",
"* Run the code from the command line."
]
},
{
{
"cell_type": "markdown",
"cell_type": "markdown",
"metadata": {
"metadata": {
...
@@ -789,6 +757,20 @@
...
@@ -789,6 +757,20 @@
" Series of articles providing a tour of the Python standard library through short examples."
" Series of articles providing a tour of the Python standard library through short examples."
]
]
},
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Homework assignment\n",
"===\n",
"\n",
"https://classroom.github.com/a/QU2iPYKn"
]
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 1,
"execution_count": 1,
...
@@ -867,21 +849,21 @@
...
@@ -867,21 +849,21 @@
"metadata": {
"metadata": {
"celltoolbar": "Slideshow",
"celltoolbar": "Slideshow",
"kernelspec": {
"kernelspec": {
"display_name": "Python 2",
"display_name": "Python 3",
"language": "python",
"language": "python",
"name": "python2"
"name": "python3"
},
},
"language_info": {
"language_info": {
"codemirror_mode": {
"codemirror_mode": {
"name": "ipython",
"name": "ipython",
"version": 2
"version": 3
},
},
"file_extension": ".py",
"file_extension": ".py",
"mimetype": "text/x-python",
"mimetype": "text/x-python",
"name": "python",
"name": "python",
"nbconvert_exporter": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"pygments_lexer": "ipython3",
"version": "2.7.12"
"version": "3.5.2"
}
}
},
},
"nbformat": 4,
"nbformat": 4,
...
...
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
<span style="font-size: 200%">Introduction to Python (3)</span>
<span style="font-size: 200%">Introduction to Python (3)</span>
===
===
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Functions
Functions
===
===
A _function_ is a named sequence of statements that performs some piece of work.
A _function_ is a named sequence of statements that performs some piece of work.
Later on that function can be called by using its name.
Later on that function can be called by using its name.
Defining a function
Defining a function
---
---
A function definition includes its _name_, _arguments_ and _body_.
A function definition includes its _name_, _arguments_ and _body_.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` python
``` python
defadd_two(number):
defadd_two(number):
returnnumber+2
returnnumber+2
```
```
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` python
``` python
foriinrange(5):
foriinrange(5):
printadd_two(i)
printadd_two(i)
```
```
%% Output
%% Output
2
2
3
3
4
4
5
5
6
6
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Functions
Functions
===
===
Keyword arguments
Keyword arguments
---
---
Besides regular arguments, functions can have keyword arguments.
Besides regular arguments, functions can have keyword arguments.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` python
``` python
defadd_some_other_number(number,other_number=12):
defadd_some_other_number(number,other_number=12):
returnnumber+other_number
returnnumber+other_number
```
```
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` python
``` python
add_some_other_number(2,6)
add_some_other_number(2,6)
```
```
%% Output
%% Output
8
8
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` python
``` python
add_some_other_number(3,other_number=4)
add_some_other_number(3,other_number=4)
```
```
%% Output
%% Output
7
7
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` python
``` python
add_some_other_number(5)
add_some_other_number(5)
```
```
%% Output
%% Output
17
17
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Functions
Functions
===
===
Docstrings
Docstrings
---
---
Like many other definitions, functions can have docstrings.
Like many other definitions, functions can have docstrings.
* Docstrings are regular string values which you start the definition body with.
* Docstrings are regular string values which you start the definition body with.
* You can access an object's docstring using `help`.
* You can access an object's docstring using `help`.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` python
``` python
deffactorial(n):
deffactorial(n):
"""Compute factorial of n in the obious way."""
"""Compute factorial of n in the obious way."""
ifn==0:
ifn==0:
return1
return1
else:
else:
returnfactorial(n-1)*n
returnfactorial(n-1)*n
```
```
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` python
``` python
help(factorial)
help(factorial)
```
```
%% Output
%% Output
Help on function factorial in module __main__:
Help on function factorial in module __main__:
factorial(n)
factorial(n)
Compute factorial of n in the obious way.
Compute factorial of n in the obious way.
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Functions
Functions
===
===
Functions are values
Functions are values
---
---
We can pass functions around just like other values, and call them.
We can pass functions around just like other values, and call them.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` python
``` python
functions=[add_two,add_some_other_number]
functions=[add_two,add_some_other_number]
forfunctioninfunctions:
forfunctioninfunctions:
printfunction(7)
printfunction(7)
```
```
%% Output
%% Output
9
9
19
19
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Simple anonymous functions can be created with `lambda`.
Simple anonymous functions can be created with `lambda`.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` python
``` python
functions.append(lambdax:x*7)
functions.append(lambdax:x*7)
forfunctioninfunctions:
forfunctioninfunctions:
printfunction(4)
printfunction(4)
```
```
%% Output
%% Output
6
6
16
16
28
28
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Functions
Functions
===
===
Higher-order functions
Higher-order functions
---
---
A function that takes a function as argument is a higher-order function.
A function that takes a function as argument is a higher-order function.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` python
``` python
help(map)
help(map)
```
```
%% Output
%% Output
Help on built-in function map in module __builtin__:
Help on built-in function map in module __builtin__:
map(...)
map(...)
map(function, sequence[, sequence, ...]) -> list
map(function, sequence[, sequence, ...]) -> list
Return a list of the results of applying the function to the items of
Return a list of the results of applying the function to the items of
the argument sequence(s). If more than one sequence is given, the
the argument sequence(s). If more than one sequence is given, the
function is called with an argument list consisting of the corresponding
function is called with an argument list consisting of the corresponding
item of each sequence, substituting None for missing values when not all
item of each sequence, substituting None for missing values when not all
sequences have the same length. If the function is None, return a list of
sequences have the same length. If the function is None, return a list of
the items of the sequence (or a list of tuples if more than one sequence).
the items of the sequence (or a list of tuples if more than one sequence).
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` python
``` python
map(add_two,[1,2,3,4])
map(add_two,[1,2,3,4])
```
```
%% Output
%% Output
[3, 4, 5, 6]
[3, 4, 5, 6]
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
$\S$ Exercise: k-mer counting (1/2)
<divclass="alert alert-success">
===
<h1>Hands on!</h1>
Remember the previous exercise of finding (unique) substrings of length 3.
* Make a function from your implementation.
* Have `k` as an argument to the function.
* Test the function on several input strings.
**Note:** Editing multi-line statements in the console can be frustrating. You can try the QT console (`ipython qtconsole`) or edit your function in an editor with `%edit`:
def my_function(arg):
print arg * 4
%edit my_function
%% Cell type:markdown id: tags:
<ol>
<li>Write a Python function that returns the maximum of two numbers.</li>
$\S$ Exercise: k-mer counting (2/2)
<li>Write a Python function that returns the maximum of three numbers. Try to reuse the first maximum of two numbers function.</li>
===
<li>Write a Python function that accepts a string as parameter. Next, it calculates and prints the number of upper case letters and lower case letters. Make us of the `isupper` and `islower` built in methods.</li>
</ol>
Modify your function to use a dictionary with substring counts.
</div>
* Use the substrings as dictionary keys.
* Use the counts as dictionary values.
* Have the function return the dictionary.
* Add a docstring to the function.
* Use the function to print k-mer counts for some strings.
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Comprehensions
Comprehensions
===
===
List comprehensions
List comprehensions
---
---
Similar to mathematical set notation (e.g., $\{ x ~|~ x \in \mathbf R \land x > 0\}$), we can create lists.
Similar to mathematical set notation (e.g., $\{ x ~|~ x \in \mathbf R \land x > 0\}$), we can create lists.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` python
``` python
[(x,x*x)forxinrange(10)ifx%2]
[(x,x*x)forxinrange(10)ifx%2]
```
```
%% Output
%% Output
[(1, 1), (3, 9), (5, 25), (7, 49), (9, 81)]
[(1, 1), (3, 9), (5, 25), (7, 49), (9, 81)]
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
We can do the same thing using `map` and `filter`, but list comprehensions are often more readable.
We can do the same thing using `map` and `filter`, but list comprehensions are often more readable.
*[Learn Python The Hard Way](http://learnpythonthehardway.org/book/)
*[Learn Python The Hard Way](http://learnpythonthehardway.org/book/)
<br>
<br>
Book on learning Python by exercises, online available for free.
Book on learning Python by exercises, online available for free.
*[The Hitchhiker's Guide to Python](http://docs.python-guide.org/en/latest/)
*[The Hitchhiker's Guide to Python](http://docs.python-guide.org/en/latest/)
<br>
<br>
This opinionated guide exists to provide both novice and expert Python developers a best-practice handbook to the installation, configuration, and usage of Python on a daily basis.
This opinionated guide exists to provide both novice and expert Python developers a best-practice handbook to the installation, configuration, and usage of Python on a daily basis.
*[A Primer on Scientific Programming with Python](http://codingcat.com/knjige/python/A%20Primer%20on%20Scientific%20Programming%20with%20Python.pdf)
*[A Primer on Scientific Programming with Python](http://codingcat.com/knjige/python/A%20Primer%20on%20Scientific%20Programming%20with%20Python.pdf)
<br>
<br>
Complete PDF version of the book. The aim of this book is to teach computer programming using examples from mathematics and the natural sciences.
Complete PDF version of the book. The aim of this book is to teach computer programming using examples from mathematics and the natural sciences.
*[Python Module of the Week](http://pymotw.com/)
*[Python Module of the Week](http://pymotw.com/)
<br>
<br>
Series of articles providing a tour of the Python standard library through short examples.
Series of articles providing a tour of the Python standard library through short examples.