"features of python, with emphasis on some of the common difficulties\n",
"features of python, with emphasis on some of the common difficulties\n",
"and pitfalls that are commonly encountered when moving to python.\n",
"and pitfalls that are commonly encountered when moving to python.\n",
"\n",
"\n",
"When going through this make sure that you _run_ each code block\n",
"When going through this make sure that you _run_ each code block and\n",
"and look at the output, as these are crucial for understanding the\n",
"look at the output, as these are crucial for understanding the\n",
"explanations. You can run each block by using _shift + enter_ (including the text blocks, so you can just move down the document with shift + enter).\n",
"explanations. You can run each block by using _shift + enter_\n",
"(including the text blocks, so you can just move down the document\n",
"with shift + enter).\n",
"\n",
"It is also possible to _change_ the contents of each code block (these pages are completely interactive) so do experiment with the code you see and try some variations!\n",
"Python has many different types and variables are dynamic and can change types (like MATLAB). Some of the most commonly used in-built types are:\n",
"Python has many different types and variables are dynamic and can change types (like MATLAB). Some of the most commonly used in-built types are:\n",
...
@@ -72,10 +106,11 @@
...
@@ -72,10 +106,11 @@
"\n",
"\n",
"---\n",
"---\n",
"\n",
"\n",
"<a class=\"anchor\" id=\"Strings\"></a>\n",
"## Strings\n",
"## Strings\n",
"\n",
"\n",
"Strings can be specified using single quotes *or* double quotes - as long as they are matched.\n",
"Strings can be specified using single quotes *or* double quotes - as long as they are matched.\n",
"Strings can be dereferenced like lists (see later).\n",
"Strings can be indexed like lists (see later).\n",
"\n",
"\n",
"For example:"
"For example:"
]
]
...
@@ -115,6 +150,7 @@
...
@@ -115,6 +150,7 @@
"cell_type": "markdown",
"cell_type": "markdown",
"metadata": {},
"metadata": {},
"source": [
"source": [
"<a class=\"anchor\" id=\"Format\"></a>\n",
"### Format\n",
"### Format\n",
"\n",
"\n",
"More interesting strings can be created using the `format` statement, which is very useful in print statements:"
"More interesting strings can be created using the `format` statement, which is very useful in print statements:"
...
@@ -137,8 +173,9 @@
...
@@ -137,8 +173,9 @@
"cell_type": "markdown",
"cell_type": "markdown",
"metadata": {},
"metadata": {},
"source": [
"source": [
"There are also other options along these lines, but this is the more modern version, although you will see plenty of the other alternatives in old code (i.e., code written before last week).\n",
"There are also other options along these lines, but this is the more modern version, although you will see plenty of the other alternatives in \"old\" code (to python coders this means anything written before last week).\n",
"The methods `lower()` and `upper()` are useful for strings. For example:"
"The methods `lower()` and `upper()` are useful for strings. For example:"
...
@@ -173,6 +210,23 @@
...
@@ -173,6 +210,23 @@
"print(s2)"
"print(s2)"
]
]
},
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Strings can be concatenated just by using the `+` operator:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"s3 = s + ' :: ' + s2\n",
"print(s3)"
]
},
{
{
"cell_type": "markdown",
"cell_type": "markdown",
"metadata": {},
"metadata": {},
...
@@ -200,8 +254,7 @@
...
@@ -200,8 +254,7 @@
"\n",
"\n",
"For more information on matching and substitutions, look up the regular expression module on the web.\n",
"For more information on matching and substitutions, look up the regular expression module on the web.\n",
"\n",
"\n",
"\n",
"Two common and convenient string methods are `strip()` and `split()`. The first will remove any whitespace at the beginning and end of a string:"
"You can also split, or tokenize, a string (to turn it into a list) like this:"
]
]
},
},
{
{
...
@@ -210,18 +263,79 @@
...
@@ -210,18 +263,79 @@
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [],
"source": [
"source": [
"print(s.split())"
"s2 = ' A very spacy string '\n",
"print('*' + s2 + '*')\n",
"print('*' + s2.strip() + '*')"
]
]
},
},
{
{
"cell_type": "markdown",
"cell_type": "markdown",
"metadata": {},
"metadata": {},
"source": [
"source": [
"With `split()` we can tokenize a string (to turn it into a list of strings) like this:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(s.split())\n",
"print(s2.split())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"By default it splits at whitespace, but it can also split at a specified delimiter:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"s4 = ' This is, as you can see , a very weirdly spaced and punctuated string ... '\n",
"print(s4.split(','))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"There are more powerful ways of dealing with this like csv files/strings, which are covered in later practicals, but even this can get you a long way.\n",
"\n",
"> Note that strings in python 3 are _unicode_ so can represent Chinese characters, etc, and is therefore very flexible. However, in general you can just be blissfully ignorant of this fact.\n",
"> Note that strings in python 3 are _unicode_ so can represent Chinese characters, etc, and is therefore very flexible. However, in general you can just be blissfully ignorant of this fact.\n",
"\n",
"\n",
"Strings can be converted to integer or floating-point values by using the `int()` and `float()` calls:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sint='23'\n",
"sfp='2.03'\n",
"print(sint + sfp)\n",
"print(int(sint) + float(sfp))\n",
"print(float(sint) + float(sfp))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"> Note that calling `int()` on a non-integer (e.g., on `sfp` above) will raise an error.\n",
"Multiplication can be used with lists, where multiplication implements replication."
"Multiplication can be used with lists, where multiplication implements replication."
...
@@ -504,6 +622,7 @@
...
@@ -504,6 +622,7 @@
"cell_type": "markdown",
"cell_type": "markdown",
"metadata": {},
"metadata": {},
"source": [
"source": [
"<a class=\"anchor\" id=\"Looping\"></a>\n",
"### Looping over elements in a list (or tuple)"
"### Looping over elements in a list (or tuple)"
]
]
},
},
...
@@ -524,6 +643,7 @@
...
@@ -524,6 +643,7 @@
"source": [
"source": [
"> Note that the indentation within the loop is _*crucial*_. All python control blocks are delineated purely by indentation.\n",
"> Note that the indentation within the loop is _*crucial*_. All python control blocks are delineated purely by indentation.\n",
"\n",
"\n",
"<a class=\"anchor\" id=\"Getting-help\"></a>\n",
"### Getting help\n",
"### Getting help\n",
"\n",
"\n",
"The function `help()` can be used to get information about any variable/object/function in python. It lists the possible operations. In `ipython` you can also just type `?<blah>` or `<blah>?` instead:"
"The function `help()` can be used to get information about any variable/object/function in python. It lists the possible operations. In `ipython` you can also just type `?<blah>` or `<blah>?` instead:"
...
@@ -562,6 +682,7 @@
...
@@ -562,6 +682,7 @@
"\n",
"\n",
"---\n",
"---\n",
"\n",
"\n",
"<a class=\"anchor\" id=\"Dictionaries\"></a>\n",
"## Dictionaries\n",
"## Dictionaries\n",
"\n",
"\n",
"These store key-value pairs. For example:"
"These store key-value pairs. For example:"
...
@@ -588,6 +709,7 @@
...
@@ -588,6 +709,7 @@
"Python is nothing if not flexible. However, each key must be unique\n",
"Python is nothing if not flexible. However, each key must be unique\n",
"In python there are immutable types (e.g. numbers) and mutable types (e.g. lists). The main thing to know is that assignment can sometimes create separate copies and sometimes create references (as in C++). In general, the more complicated types are assigned via references. For example:"
"In python there are immutable types (e.g. numbers) and mutable types (e.g. lists). The main thing to know is that assignment can sometimes create separate copies and sometimes create references (as in C++). In general, the more complicated types are assigned via references. For example:"
...
@@ -802,10 +927,16 @@
...
@@ -802,10 +927,16 @@
"cell_type": "markdown",
"cell_type": "markdown",
"metadata": {},
"metadata": {},
"source": [
"source": [
"> Note that we have defined some functions here - and the syntax\n",
"> should be relatively intuitive. See <a href=\"#functions\">below</a>\n",
"> for a bit more detail on function definitions.\n",
"There is a boolean type in python that can be `True` or `False` (note the capitals). Other values can also be used for True or False (e.g., 1 for True; 0 or None or [] or {} or \"\") although they are not considered 'equal' in the sense that the operator `==` would consider them the same.\n",
"There is a boolean type in python that can be `True` or `False` (note the capitals). Other values can also be used for True or False (e.g., 1 for True; 0 or None or [] or {} or \"\") although they are not considered 'equal' in the sense that the operator `==` would consider them the same.\n",
"The basic syntax of `if` statements is fairly standard, though don't forget that you _*must*_ indent the statements within the conditional/loop block as this is the way of delineating blocks of code in python. For example:"
"The basic syntax of `if` statements is fairly standard, though don't forget that you _*must*_ indent the statements within the conditional/loop block as this is the way of delineating blocks of code in python. For example:"
...
@@ -899,6 +1031,7 @@
...
@@ -899,6 +1031,7 @@
"\n",
"\n",
"---\n",
"---\n",
"\n",
"\n",
"<a class=\"anchor\" id=\"For-loops\"></a>\n",
"### For loops\n",
"### For loops\n",
"\n",
"\n",
"The `for` loop works like in bash:"
"The `for` loop works like in bash:"
...
@@ -979,6 +1112,7 @@
...
@@ -979,6 +1112,7 @@
"source": [
"source": [
"This type of loop can be used with any two lists (or similar) to iterate over them jointly.\n",
"This type of loop can be used with any two lists (or similar) to iterate over them jointly.\n",
"\n",
"\n",
"<a class=\"anchor\" id=\"While-loops\"></a>\n",
"### While loops\n",
"### While loops\n",
"\n",
"\n",
"The syntax for this is pretty standard:"
"The syntax for this is pretty standard:"
...
@@ -1009,10 +1143,12 @@
...
@@ -1009,10 +1143,12 @@
"\n",
"\n",
"---\n",
"---\n",
"\n",
"\n",
"<a class=\"anchor\" id=\"quick-intro\"></a>\n",
"### A quick intro to conditional expressions and list comprehensions\n",
"### A quick intro to conditional expressions and list comprehensions\n",
"\n",
"\n",
"These are more advanced bits of python but are really useful and common, so worth having a little familiarity with at this stage.\n",
"These are more advanced bits of python but are really useful and common, so worth having a little familiarity with at this stage.\n",
"This is a shorthand syntax for building a list like a for loop but doing it in one line, and is very popular in python. It is quite similar to mathematical set notation. For example:"
"This is a shorthand syntax for building a list like a for loop but doing it in one line, and is very popular in python. It is quite similar to mathematical set notation. For example:"
...
@@ -1057,7 +1194,113 @@
...
@@ -1057,7 +1194,113 @@
"cell_type": "markdown",
"cell_type": "markdown",
"metadata": {},
"metadata": {},
"source": [
"source": [
"You'll find that python programmers use this kind of construction _*a lot*_."
"You'll find that python programmers use this kind of construction _*a lot*_.\n",
"\n",
"\n",
"---\n",
"\n",
"<a class=\"anchor\" id=\"functions\"></a>\n",
"## Functions\n",
"\n",
"You will find functions pretty familiar in python to start with,\n",
"although they have a few options which are really handy and different\n",
"from C++ or matlab (to be covered in a later practical). To start\n",
"with we'll look at a simple function but note a few key points:\n",
" * you _must_ indent everything inside the function (it is a code\n",
" block and indentation is the only way of determining this - just\n",
" like for the guts of a loop)\n",
" * you can return _whatever you want_ from a python function, but only\n",
" a single object - it is usual to package up multiple things in a\n",
" tuple or list, which is easily unpacked by the calling invocation:\n",
" e.g., `a, b, c = myfunc(x)`\n",
" * parameters are passed by reference (see section on <a\n",
" href=\"#Copying-and-references\">copying and references</a>)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def myfunc(x, y, z=0):\n",
" r2 = x*x + y*y + z*z\n",
" r = r2**0.5\n",
" return (r, r2)\n",
"\n",
"rad = myfunc(10, 20)\n",
"print(rad)\n",
"rad, dummy = myfunc(10, 20, 30)\n",
"print(rad)\n",
"rad, _ = myfunc(10,20,30)\n",
"print(rad)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"> Note that the `_` is used as shorthand here for a dummy variable\n",
"> that you want to throw away.\n",
"\n",
"One nice feature of python functions is that you can name the\n",
"arguments when you call them, rather than only doing it by position.\n",
"For example:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def myfunc(x, y, z=0, flag=''):\n",
" if flag=='L1':\n",
" r = abs(x) + abs(y) + abs(z)\n",
" else:\n",
" r = (x*x + y*y + z*z)**0.5\n",
" return r\n",
"\n",
"rA = myfunc(10, 20)\n",
"rB = myfunc(10, 20, flag='L1')\n",
"rC = myfunc(10, 20, flag='L1', z=30)\n",
"print(rA, rB, rC)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You will often see python functions called with these named arguments.\n",
"\n",
"---\n",
"\n",
"<a class=\"anchor\" id=\"exercise\"></a>\n",
"## Exercises\n",
"\n",
"Let's say you are given a single string with comma separated elements\n",
"that represent filenames and ID codes: e.g., `/vols/Data/pytreat/AAC, 165873, /vols/Data/pytreat/AAG, 170285, ...`\n",
"\n",
"Write some code to do the following: \n",
" * separate out the filenames and ID codes into separate lists (ID's\n",
" should be numerical values, not strings)\n",
" * loop over the two and generate a _string_ that could be used to\n",
" rename the directories (e.g., `mv /vols/Data/pytreat/AAC /vols/Data/pytreat/S165873`) - we will cover how to actually execute these in a later practical\n",
" * convert your dual lists into a dictionary, with ID as the key\n",
" * write a small function to determine if an ID is present in this\n",
" set of not, and also return the filename if it is\n",
" * write a for loop to create a list of all the odd-numbered IDs (you can use the `%` operator for modulus - i.e., `5 % 2` is 1)\n",
" * re-write the for loop as a list comprehension\n",
" * now generate a list of the filenames corresponding to these odd-numbered IDs"
This tutorial is aimed at briefly introducing you to the main language
This tutorial is aimed at briefly introducing you to the main language
features of python, with emphasis on some of the common difficulties
features of python, with emphasis on some of the common difficulties
and pitfalls that are commonly encountered when moving to python.
and pitfalls that are commonly encountered when moving to python.
When going through this make sure that you _run_ each code block
When going through this make sure that you _run_ each code block and
and look at the output, as these are crucial for understanding the
look at the output, as these are crucial for understanding the
explanations. You can run each block by using _shift + enter_ (including the text blocks, so you can just move down the document with shift + enter).
explanations. You can run each block by using _shift + enter_
(including the text blocks, so you can just move down the document
with shift + enter).
It is also possible to _change_ the contents of each code block (these pages are completely interactive) so do experiment with the code you see and try some variations!
## Contents
*[Basic types](#Basic-types)
-[Strings](#Strings)
+[Format](#Format)
+[String manipulation](#String-manipulation)
-[Tuples and lists](#Tuples-and-lists)
+[Adding to a list](#Adding-to-a-list)
+[Indexing](#Indexing)
+[Slicing](#Slicing)
-[List operations](#List-operations)
+[Looping over elements in a list (or tuple)](#Looping)
+[Getting help](#Getting-help)
-[Dictionaries](#Dictionaries)
+[Adding to a dictionary](#Adding-to-a-dictionary)
+[Removing elements from a dictionary](#Removing-elements-dictionary)
+[Looping over everything in a dictionary](#Looping-dictionary)
-[Copying and references](#Copying-and-references)
*[Control flow](#Control-flow)
-[Boolean operators](#Boolean-operators)
-[If statements](#If-statements)
-[For loops](#For-loops)
-[While loops](#While-loops)
-[A quick intro to conditional expressions and list comprehensions](#quick-intro)
Python has many different types and variables are dynamic and can change types (like MATLAB). Some of the most commonly used in-built types are:
Python has many different types and variables are dynamic and can change types (like MATLAB). Some of the most commonly used in-built types are:
* integer and floating point scalars
* integer and floating point scalars
* strings
* strings
* tuples
* tuples
* lists
* lists
* dictionary
* dictionary
N-dimensional arrays and other types are supported through common modules (e.g., numpy, scipy, scikit-learn). These will be covered in a subsequent exercise.
N-dimensional arrays and other types are supported through common modules (e.g., numpy, scipy, scikit-learn). These will be covered in a subsequent exercise.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
a = 4
a = 4
b = 3.6
b = 3.6
c = 'abc'
c = 'abc'
d = [10, 20, 30]
d = [10, 20, 30]
e = {'a' : 10, 'b': 20}
e = {'a' : 10, 'b': 20}
print(a)
print(a)
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Any variable or combination of variables can be printed using the function `print()`:
Any variable or combination of variables can be printed using the function `print()`:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
print(d)
print(d)
print(e)
print(e)
print(a, b, c)
print(a, b, c)
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
> _*Python 3 versus python 2*_:
> _*Python 3 versus python 2*_:
>
>
> Print - for the print statement the brackets are compulsory for *python 3*, but are optional in python 2. So you will see plenty of code without the brackets but you should never use `print` without brackets, as this is incompatible with Python 3.
> Print - for the print statement the brackets are compulsory for *python 3*, but are optional in python 2. So you will see plenty of code without the brackets but you should never use `print` without brackets, as this is incompatible with Python 3.
>
>
> Division - in python 3 all division is floating point (like in MATLAB), even if the values are integers, but in python 2 integer division works like it does in C.
> Division - in python 3 all division is floating point (like in MATLAB), even if the values are integers, but in python 2 integer division works like it does in C.
---
---
<aclass="anchor"id="Strings"></a>
## Strings
## Strings
Strings can be specified using single quotes *or* double quotes - as long as they are matched.
Strings can be specified using single quotes *or* double quotes - as long as they are matched.
Strings can be dereferenced like lists (see later).
Strings can be indexed like lists (see later).
For example:
For example:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
s1 = "test string"
s1 = "test string"
s2 = 'another test string'
s2 = 'another test string'
print(s1, ' :: ', s2)
print(s1, ' :: ', s2)
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
You can also use triple quotes to capture multi-line strings. For example:
You can also use triple quotes to capture multi-line strings. For example:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
s3 = '''This is
s3 = '''This is
a string over
a string over
multiple lines
multiple lines
'''
'''
print(s3)
print(s3)
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
<aclass="anchor"id="Format"></a>
### Format
### Format
More interesting strings can be created using the `format` statement, which is very useful in print statements:
More interesting strings can be created using the `format` statement, which is very useful in print statements:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
x = 1
x = 1
y = 'PyTreat'
y = 'PyTreat'
s = 'The numerical value is {} and a name is {}'.format(x, y)
s = 'The numerical value is {} and a name is {}'.format(x, y)
print(s)
print(s)
print('A name is {} and a number is {}'.format(y, x))
print('A name is {} and a number is {}'.format(y, x))
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
There are also other options along these lines, but this is the more modern version, although you will see plenty of the other alternatives in old code (i.e., code written before last week).
There are also other options along these lines, but this is the more modern version, although you will see plenty of the other alternatives in "old" code (to python coders this means anything written before last week).
<aclass="anchor"id="String-manipulation"></a>
### String manipulation
### String manipulation
The methods `lower()` and `upper()` are useful for strings. For example:
The methods `lower()` and `upper()` are useful for strings. For example:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
s = 'This is a Test String'
s = 'This is a Test String'
print(s.upper())
print(s.upper())
print(s.lower())
print(s.lower())
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Another useful method is:
Another useful method is:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
s = 'This is a Test String'
s = 'This is a Test String'
s2 = s.replace('Test', 'Better')
s2 = s.replace('Test', 'Better')
print(s2)
print(s2)
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Strings can be concatenated just by using the `+` operator:
%% Cell type:code id: tags:
```
s3 = s + ' :: ' + s2
print(s3)
```
%% Cell type:markdown id: tags:
If you like regular expressions then you're in luck as these are well supported in python using the `re` module. To use this (like many other "extensions" - called _modules_ in Python - you need to `import` it). For example:
If you like regular expressions then you're in luck as these are well supported in python using the `re` module. To use this (like many other "extensions" - called _modules_ in Python - you need to `import` it). For example:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
import re
import re
s = 'This is a test of a Test String'
s = 'This is a test of a Test String'
s1 = re.sub(r'a [Tt]est', "an example", s)
s1 = re.sub(r'a [Tt]est', "an example", s)
print(s1)
print(s1)
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
where the `r` before the quote is used to force the regular expression specification to be a `raw string`.
where the `r` before the quote is used to force the regular expression specification to be a `raw string`.
For more information on matching and substitutions, look up the regular expression module on the web.
For more information on matching and substitutions, look up the regular expression module on the web.
Two common and convenient string methods are `strip()` and `split()`. The first will remove any whitespace at the beginning and end of a string:
You can also split, or tokenize, a string (to turn it into a list) like this:
%% Cell type:code id: tags:
```
s2 = ' A very spacy string '
print('*' + s2 + '*')
print('*' + s2.strip() + '*')
```
%% Cell type:markdown id: tags:
With `split()` we can tokenize a string (to turn it into a list of strings) like this:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
print(s.split())
print(s.split())
print(s2.split())
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
By default it splits at whitespace, but it can also split at a specified delimiter:
%% Cell type:code id: tags:
```
s4 = ' This is, as you can see , a very weirdly spaced and punctuated string ... '
print(s4.split(','))
```
%% Cell type:markdown id: tags:
There are more powerful ways of dealing with this like csv files/strings, which are covered in later practicals, but even this can get you a long way.
> Note that strings in python 3 are _unicode_ so can represent Chinese characters, etc, and is therefore very flexible. However, in general you can just be blissfully ignorant of this fact.
> Note that strings in python 3 are _unicode_ so can represent Chinese characters, etc, and is therefore very flexible. However, in general you can just be blissfully ignorant of this fact.
Strings can be converted to integer or floating-point values by using the `int()` and `float()` calls:
%% Cell type:code id: tags:
```
sint='23'
sfp='2.03'
print(sint + sfp)
print(int(sint) + float(sfp))
print(float(sint) + float(sfp))
```
%% Cell type:markdown id: tags:
> Note that calling `int()` on a non-integer (e.g., on `sfp` above) will raise an error.
---
---
## Tuples and Lists
<aclass="anchor"id="Tuples-and-lists"></a>
## Tuples and lists
Both tuples and lists are builtin python types and are like vectors,
Both tuples and lists are builtin python types and are like vectors,
but for numerical vectors and arrays it is much better to use _numpy_
but for numerical vectors and arrays it is much better to use _numpy_
arrays (or matrices), which are covered in a later tutorial.
arrays (or matrices), which are covered in a later tutorial.
A tuple is like a list or a vector, but with less flexibility than a full list, however anything can be stored in either a list or tuple, without any consistency being required. For example:
A tuple is like a list or a vector, but with less flexibility than a full list, however anything can be stored in either a list or tuple, without any consistency being required. For example:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
xtuple = (3, 7.6, 'str')
xtuple = (3, 7.6, 'str')
xlist = [1, 'mj', -5.4]
xlist = [1, 'mj', -5.4]
print(xtuple)
print(xtuple)
print(xlist)
print(xlist)
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
They can also be nested:
They can also be nested:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
x2 = (xtuple, xlist)
x2 = (xtuple, xlist)
x3 = [xtuple, xlist]
x3 = [xtuple, xlist]
print('x2 is: ', x2)
print('x2 is: ', x2)
print('x3 is: ', x3)
print('x3 is: ', x3)
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
<aclass="anchor"id="Adding-to-a-list"></a>
### Adding to a list
### Adding to a list
This is easy:
This is easy:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
a = [10, 20, 30]
a = [10, 20, 30]
a = a + [70]
a = a + [70]
a += [80]
a += [80]
print(a)
print(a)
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
<aclass="anchor"id="Indexing"></a>
### Indexing
### Indexing
Square brackets are used to index tuples, lists, dictionaries, etc. For example:
Square brackets are used to index tuples, lists, dictionaries, etc. For example:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
d = [10, 20, 30]
d = [10, 20, 30]
print(d[1])
print(d[1])
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
> _*Pitfall:*_
> _*Pitfall:*_
> Python uses zero-based indexing, unlike MATLAB
> Python uses zero-based indexing, unlike MATLAB
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
a = [10, 20, 30, 40, 50, 60]
a = [10, 20, 30, 40, 50, 60]
print(a[0])
print(a[0])
print(a[2])
print(a[2])
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Indices naturally run from 0 to N-1, _but_ negative numbers can be used to reference from the end (circular wrap-around).
Indices naturally run from 0 to N-1, _but_ negative numbers can be used to reference from the end (circular wrap-around).
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
print(a[-1])
print(a[-1])
print(a[-6])
print(a[-6])
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
However, this is only true for -1 to -N. Outside of -N to N-1 will generate an `index out of range` error.
However, this is only true for -1 to -N. Outside of -N to N-1 will generate an `index out of range` error.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
print(a[-7])
print(a[-7])
```
```
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
print(a[6])
print(a[6])
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Length of a tuple or list is given by the `len()` function:
Length of a tuple or list is given by the `len()` function:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
print(len(a))
print(len(a))
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Nested lists can have nested indexing:
Nested lists can have nested indexing:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
b = [[10, 20, 30], [40, 50, 60]]
b = [[10, 20, 30], [40, 50, 60]]
print(b[0][1])
print(b[0][1])
print(b[1][0])
print(b[1][0])
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
but *not* an index like b[0, 1].
but *not* an index like b[0, 1].
> Note that `len` will only give the length of the top level.
> Note that `len` will only give the length of the top level.
> In general, numpy arrays should be preferred to nested lists when the contents are numerical.
> In general, numpy arrays should be preferred to nested lists when the contents are numerical.
<aclass="anchor"id="Slicing"></a>
### Slicing
### Slicing
A range of values for the indices can be specified to extract values from a list. For example:
A range of values for the indices can be specified to extract values from a list. For example:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
print(a[0:3])
print(a[0:3])
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
> _*Pitfall:*_
> _*Pitfall:*_
>
>
> Slicing syntax is different from MATLAB in that second number is
> Slicing syntax is different from MATLAB in that second number is
> exclusive (i.e., one plus final index) - this is in addition to the zero index difference.
> exclusive (i.e., one plus final index) - this is in addition to the zero index difference.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
a = [10, 20, 30, 40, 50, 60]
a = [10, 20, 30, 40, 50, 60]
print(a[0:3]) # same as a(1:3) in MATLAB
print(a[0:3]) # same as a(1:3) in MATLAB
print(a[1:3]) # same as a(2:3) in MATLAB
print(a[1:3]) # same as a(2:3) in MATLAB
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
> _*Pitfall:*_
> _*Pitfall:*_
>
>
> Unlike in MATLAB, you cannot use a list as indices instead of an
> Unlike in MATLAB, you cannot use a list as indices instead of an
> integer or a slice (although these can be done in _numpy_).
> integer or a slice (although these can be done in _numpy_).
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
b = [3, 4]
b = [3, 4]
print(a[b])
print(a[b])
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
<aclass="anchor"id="List-operations"></a>
### List operations
### List operations
Multiplication can be used with lists, where multiplication implements replication.
Multiplication can be used with lists, where multiplication implements replication.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
d = [10, 20, 30]
d = [10, 20, 30]
print(d * 4)
print(d * 4)
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
There are also other operations such as:
There are also other operations such as:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
d.append(40)
d.append(40)
print(d)
print(d)
d.remove(20)
d.remove(20)
print(d)
print(d)
d.pop(0)
d.pop(0)
print(d)
print(d)
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
<aclass="anchor"id="Looping"></a>
### Looping over elements in a list (or tuple)
### Looping over elements in a list (or tuple)
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
d = [10, 20, 30]
d = [10, 20, 30]
for x in d:
for x in d:
print(x)
print(x)
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
> Note that the indentation within the loop is _*crucial*_. All python control blocks are delineated purely by indentation.
> Note that the indentation within the loop is _*crucial*_. All python control blocks are delineated purely by indentation.
<aclass="anchor"id="Getting-help"></a>
### Getting help
### Getting help
The function `help()` can be used to get information about any variable/object/function in python. It lists the possible operations. In `ipython` you can also just type `?<blah>` or `<blah>?` instead:
The function `help()` can be used to get information about any variable/object/function in python. It lists the possible operations. In `ipython` you can also just type `?<blah>` or `<blah>?` instead:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
help(d)
help(d)
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
There is also a `dir()` function that gives a basic listing of the operations:
There is also a `dir()` function that gives a basic listing of the operations:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
dir(d)
dir(d)
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
> Note that google is often more helpful!
> Note that google is often more helpful!
---
---
<aclass="anchor"id="Dictionaries"></a>
## Dictionaries
## Dictionaries
These store key-value pairs. For example:
These store key-value pairs. For example:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
e = {'a' : 10, 'b': 20}
e = {'a' : 10, 'b': 20}
print(len(e))
print(len(e))
print(e.keys())
print(e.keys())
print(e.values())
print(e.values())
print(e['a'])
print(e['a'])
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
The keys and values can take on almost any type, even dictionaries!
The keys and values can take on almost any type, even dictionaries!
Python is nothing if not flexible. However, each key must be unique
Python is nothing if not flexible. However, each key must be unique
Several variables can jointly work as loop variables in python, which is very convenient. For example:
Several variables can jointly work as loop variables in python, which is very convenient. For example:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
e = {'a' : 10, 'b': 20, 'c':555}
e = {'a' : 10, 'b': 20, 'c':555}
for k, v in e.items():
for k, v in e.items():
print((k, v))
print((k, v))
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
The print statement here constructs a tuple, which is often used in python.
The print statement here constructs a tuple, which is often used in python.
Another option is:
Another option is:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
for k in e:
for k in e:
print((k, e[k]))
print((k, e[k]))
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
> Note that in both cases the order is arbitrary. The `sorted` function can be used if you want keys in a sorted order; e.g. `for k in sorted(e):` ...
> Note that in both cases the order is arbitrary. The `sorted` function can be used if you want keys in a sorted order; e.g. `for k in sorted(e):` ...
>
>
> There are also other options if you want a dictionary with ordering.
> There are also other options if you want a dictionary with ordering.
---
---
<aclass="anchor"id="Copying-and-references"></a>
## Copying and references
## Copying and references
In python there are immutable types (e.g. numbers) and mutable types (e.g. lists). The main thing to know is that assignment can sometimes create separate copies and sometimes create references (as in C++). In general, the more complicated types are assigned via references. For example:
In python there are immutable types (e.g. numbers) and mutable types (e.g. lists). The main thing to know is that assignment can sometimes create separate copies and sometimes create references (as in C++). In general, the more complicated types are assigned via references. For example:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
a = 7
a = 7
b = a
b = a
a = 2348
a = 2348
print(b)
print(b)
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
As opposed to:
As opposed to:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
a = [7]
a = [7]
b = a
b = a
a[0] = 8888
a[0] = 8888
print(b)
print(b)
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
But if an operation is performed then a copy might be made:
But if an operation is performed then a copy might be made:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
a = [7]
a = [7]
b = a * 2
b = a * 2
a[0] = 8888
a[0] = 8888
print(b)
print(b)
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
If an explicit copy is necessary then this can be made using the `list()` constructor:
If an explicit copy is necessary then this can be made using the `list()` constructor:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
a = [7]
a = [7]
b = list(a)
b = list(a)
a[0] = 8888
a[0] = 8888
print(b)
print(b)
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
There is a constructor for each type and this con be useful for converting between types:
There is a constructor for each type and this con be useful for converting between types:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
xt = (2, 5, 7)
xt = (2, 5, 7)
xl = list(xt)
xl = list(xt)
print(xt)
print(xt)
print(xl)
print(xl)
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
> _*Pitfall:*_
> _*Pitfall:*_
>
>
> When writing functions you need to be particularly careful about references and copies.
> When writing functions you need to be particularly careful about references and copies.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
def foo1(x):
def foo1(x):
x.append(10)
x.append(10)
def foo2(x):
def foo2(x):
x = x + [10]
x = x + [10]
def foo3(x):
def foo3(x):
return x + [10]
return x + [10]
a = [5]
a = [5]
print(a)
print(a)
foo1(a)
foo1(a)
print(a)
print(a)
foo2(a)
foo2(a)
print(a)
print(a)
foo3(a)
foo3(a)
print(a)
print(a)
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
> Note that we have defined some functions here - and the syntax
> should be relatively intuitive. See <a href="#functions">below</a>
> for a bit more detail on function definitions.
---
---
<aclass="anchor"id="Control-flow"></a>
## Control flow
## Control flow
<aclass="anchor"id="Boolean-operators"></a>
### Boolean operators
### Boolean operators
There is a boolean type in python that can be `True` or `False` (note the capitals). Other values can also be used for True or False (e.g., 1 for True; 0 or None or [] or {} or "") although they are not considered 'equal' in the sense that the operator `==` would consider them the same.
There is a boolean type in python that can be `True` or `False` (note the capitals). Other values can also be used for True or False (e.g., 1 for True; 0 or None or [] or {} or "") although they are not considered 'equal' in the sense that the operator `==` would consider them the same.
Relevant boolean and comparison operators include: `not`, `and`, `or`, `==` and `!=`
Relevant boolean and comparison operators include: `not`, `and`, `or`, `==` and `!=`
For example:
For example:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
a = True
a = True
print('Not a is:', not a)
print('Not a is:', not a)
print('Not 1 is:', not 1)
print('Not 1 is:', not 1)
print('Not 0 is:', not 0)
print('Not 0 is:', not 0)
print('Not {} is:', not {})
print('Not {} is:', not {})
print('{}==0 is:', {}==0)
print('{}==0 is:', {}==0)
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
There is also the `in` test for strings, lists, etc:
There is also the `in` test for strings, lists, etc:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
print('the' in 'a number of words')
print('the' in 'a number of words')
print('of' in 'a number of words')
print('of' in 'a number of words')
print(3 in [1, 2, 3, 4])
print(3 in [1, 2, 3, 4])
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
<aclass="anchor"id="If-statements"></a>
### If statements
### If statements
The basic syntax of `if` statements is fairly standard, though don't forget that you _*must*_ indent the statements within the conditional/loop block as this is the way of delineating blocks of code in python. For example:
The basic syntax of `if` statements is fairly standard, though don't forget that you _*must*_ indent the statements within the conditional/loop block as this is the way of delineating blocks of code in python. For example:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
import random
import random
a = random.uniform(-1, 1)
a = random.uniform(-1, 1)
print(a)
print(a)
if a>0:
if a>0:
print('Positive')
print('Positive')
elif a<0:
elif a<0:
print('Negative')
print('Negative')
else:
else:
print('Zero')
print('Zero')
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Or more generally:
Or more generally:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
a = [] # just one of many examples
a = [] # just one of many examples
if not a:
if not a:
print('Variable is true, or at least not empty')
print('Variable is true, or at least not empty')
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
This can be useful for functions where a variety of possible input types are being dealt with.
This can be useful for functions where a variety of possible input types are being dealt with.
---
---
<aclass="anchor"id="For-loops"></a>
### For loops
### For loops
The `for` loop works like in bash:
The `for` loop works like in bash:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
for x in [2, 'is', 'more', 'than', 1]:
for x in [2, 'is', 'more', 'than', 1]:
print(x)
print(x)
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
where a list or any other sequence (e.g. tuple) can be used.
where a list or any other sequence (e.g. tuple) can be used.
If you want a numerical range then use:
If you want a numerical range then use:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
for x in range(2, 9):
for x in range(2, 9):
print(x)
print(x)
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Note that, like slicing, the maximum value is one less than the value specified. Also, `range` actually returns an object that can be iterated over but is not just a list of numbers. If you want a list of numbers then `list(range(2, 9))` will give you this.
Note that, like slicing, the maximum value is one less than the value specified. Also, `range` actually returns an object that can be iterated over but is not just a list of numbers. If you want a list of numbers then `list(range(2, 9))` will give you this.
A very nice feature of python is that multiple variables can be assigned from a tuple or list:
A very nice feature of python is that multiple variables can be assigned from a tuple or list:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
x, y = [4, 7]
x, y = [4, 7]
print(x)
print(x)
print(y)
print(y)
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
and this can be combined with a function called `zip` to make very convenient dual variable loops:
and this can be combined with a function called `zip` to make very convenient dual variable loops:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
alist = ['Some', 'set', 'of', 'items']
alist = ['Some', 'set', 'of', 'items']
blist = list(range(len(alist)))
blist = list(range(len(alist)))
print(list(zip(alist, blist)))
print(list(zip(alist, blist)))
for x, y in zip(alist, blist):
for x, y in zip(alist, blist):
print(y, x)
print(y, x)
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
This type of loop can be used with any two lists (or similar) to iterate over them jointly.
This type of loop can be used with any two lists (or similar) to iterate over them jointly.
<aclass="anchor"id="While-loops"></a>
### While loops
### While loops
The syntax for this is pretty standard:
The syntax for this is pretty standard:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
import random
import random
n = 0
n = 0
x = 0
x = 0
while n<100:
while n<100:
x += random.uniform(0, 1)**2 # where ** is a power operation
x += random.uniform(0, 1)**2 # where ** is a power operation
if x>50:
if x>50:
break
break
n += 1
n += 1
print(x)
print(x)
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
You can also use `continue` as in other languages.
You can also use `continue` as in other languages.
---
---
<aclass="anchor"id="quick-intro"></a>
### A quick intro to conditional expressions and list comprehensions
### A quick intro to conditional expressions and list comprehensions
These are more advanced bits of python but are really useful and common, so worth having a little familiarity with at this stage.
These are more advanced bits of python but are really useful and common, so worth having a little familiarity with at this stage.
<aclass="anchor"id="Conditional-expressions"></a>
#### Conditional expressions
#### Conditional expressions
A general expression that can be used in python is: A `if` condition `else` B
A general expression that can be used in python is: A `if` condition `else` B
For example:
For example:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
import random
import random
x = random.uniform(0, 1)
x = random.uniform(0, 1)
y = x**2 if x<0.5 else (1 - x)**2
y = x**2 if x<0.5 else (1 - x)**2
print(x, y)
print(x, y)
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
<aclass="anchor"id="List-comprehensions"></a>
#### List comprehensions
#### List comprehensions
This is a shorthand syntax for building a list like a for loop but doing it in one line, and is very popular in python. It is quite similar to mathematical set notation. For example:
This is a shorthand syntax for building a list like a for loop but doing it in one line, and is very popular in python. It is quite similar to mathematical set notation. For example:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
v1 = [ x**2 for x in range(10) ]
v1 = [ x**2 for x in range(10) ]
print(v1)
print(v1)
v2 = [ x**2 for x in range(10) if x!=7 ]
v2 = [ x**2 for x in range(10) if x!=7 ]
print(v2)
print(v2)
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
You'll find that python programmers use this kind of construction _*a lot*_.
You'll find that python programmers use this kind of construction _*a lot*_.
---
<a class="anchor" id="functions"></a>
## Functions
You will find functions pretty familiar in python to start with,
although they have a few options which are really handy and different
from C++ or matlab (to be covered in a later practical). To start
with we'll look at a simple function but note a few key points:
*you _must_ indent everything inside the function (it is a code
block and indentation is the only way of determining this - just
like for the guts of a loop)
* you can return _whatever you want_ from a python function, but only
a single object - it is usual to package up multiple things in a
tuple or list, which is easily unpacked by the calling invocation:
e.g., `a, b, c = myfunc(x)`
* parameters are passed by reference (see section on <a
href="#Copying-and-references">copying and references</a>)
One nice feature of python functions is that you can name the
arguments when you call them, rather than only doing it by position.
For example:
%% Cell type:code id: tags:
```
def myfunc(x, y, z=0, flag=''):
if flag=='L1':
r = abs(x) + abs(y) + abs(z)
else:
r = (x*x + y*y + z*z)**0.5
return r
rA = myfunc(10, 20)
rB = myfunc(10, 20, flag='L1')
rC = myfunc(10, 20, flag='L1', z=30)
print(rA, rB, rC)
```
%% Cell type:markdown id: tags:
You will often see python functions called with these named arguments.
---
<aclass="anchor"id="exercise"></a>
## Exercises
Let's say you are given a single string with comma separated elements
that represent filenames and ID codes: e.g., `/vols/Data/pytreat/AAC, 165873, /vols/Data/pytreat/AAG, 170285, ...`
Write some code to do the following:
* separate out the filenames and ID codes into separate lists (ID's
should be numerical values, not strings)
* loop over the two and generate a _string_ that could be used to
rename the directories (e.g., `mv /vols/Data/pytreat/AAC /vols/Data/pytreat/S165873`) - we will cover how to actually execute these in a later practical
* convert your dual lists into a dictionary, with ID as the key
* write a small function to determine if an ID is present in this
set of not, and also return the filename if it is
* write a for loop to create a list of all the odd-numbered IDs (you can use the `%` operator for modulus - i.e., `5 % 2` is 1)
* re-write the for loop as a list comprehension
* now generate a list of the filenames corresponding to these odd-numbered IDs
@@ -4,12 +4,46 @@ This tutorial is aimed at briefly introducing you to the main language
...
@@ -4,12 +4,46 @@ This tutorial is aimed at briefly introducing you to the main language
features of python, with emphasis on some of the common difficulties
features of python, with emphasis on some of the common difficulties
and pitfalls that are commonly encountered when moving to python.
and pitfalls that are commonly encountered when moving to python.
When going through this make sure that you _run_ each code block
When going through this make sure that you _run_ each code block and
and look at the output, as these are crucial for understanding the
look at the output, as these are crucial for understanding the
explanations. You can run each block by using _shift + enter_ (including the text blocks, so you can just move down the document with shift + enter).
explanations. You can run each block by using _shift + enter_
(including the text blocks, so you can just move down the document
with shift + enter).
It is also possible to _change_ the contents of each code block (these pages are completely interactive) so do experiment with the code you see and try some variations!
## Contents
*[Basic types](#Basic-types)
-[Strings](#Strings)
+[Format](#Format)
+[String manipulation](#String-manipulation)
-[Tuples and lists](#Tuples-and-lists)
+[Adding to a list](#Adding-to-a-list)
+[Indexing](#Indexing)
+[Slicing](#Slicing)
-[List operations](#List-operations)
+[Looping over elements in a list (or tuple)](#Looping)
+[Getting help](#Getting-help)
-[Dictionaries](#Dictionaries)
+[Adding to a dictionary](#Adding-to-a-dictionary)
+[Removing elements from a dictionary](#Removing-elements-dictionary)
+[Looping over everything in a dictionary](#Looping-dictionary)
-[Copying and references](#Copying-and-references)
*[Control flow](#Control-flow)
-[Boolean operators](#Boolean-operators)
-[If statements](#If-statements)
-[For loops](#For-loops)
-[While loops](#While-loops)
-[A quick intro to conditional expressions and list comprehensions](#quick-intro)
Python has many different types and variables are dynamic and can change types (like MATLAB). Some of the most commonly used in-built types are:
Python has many different types and variables are dynamic and can change types (like MATLAB). Some of the most commonly used in-built types are:
...
@@ -45,6 +79,7 @@ print(a, b, c)
...
@@ -45,6 +79,7 @@ print(a, b, c)
---
---
<aclass="anchor"id="Strings"></a>
## Strings
## Strings
Strings can be specified using single quotes *or* double quotes - as long as they are matched.
Strings can be specified using single quotes *or* double quotes - as long as they are matched.
...
@@ -66,6 +101,7 @@ multiple lines
...
@@ -66,6 +101,7 @@ multiple lines
print(s3)
print(s3)
```
```
<aclass="anchor"id="Format"></a>
### Format
### Format
More interesting strings can be created using the `format` statement, which is very useful in print statements:
More interesting strings can be created using the `format` statement, which is very useful in print statements:
...
@@ -77,8 +113,9 @@ print(s)
...
@@ -77,8 +113,9 @@ print(s)
print('A name is {} and a number is {}'.format(y, x))
print('A name is {} and a number is {}'.format(y, x))
```
```
There are also other options along these lines, but this is the more modern version, although you will see plenty of the other alternatives in old code (i.e., code written before last week).
There are also other options along these lines, but this is the more modern version, although you will see plenty of the other alternatives in "old" code (to python coders this means anything written before last week).
<aclass="anchor"id="String-manipulation"></a>
### String manipulation
### String manipulation
The methods `lower()` and `upper()` are useful for strings. For example:
The methods `lower()` and `upper()` are useful for strings. For example:
Strings can be concatenated just by using the `+` operator:
```
s3 = s + ' :: ' + s2
print(s3)
```
If you like regular expressions then you're in luck as these are well supported in python using the `re` module. To use this (like many other "extensions" - called _modules_ in Python - you need to `import` it). For example:
If you like regular expressions then you're in luck as these are well supported in python using the `re` module. To use this (like many other "extensions" - called _modules_ in Python - you need to `import` it). For example:
```
```
import re
import re
...
@@ -106,17 +150,46 @@ where the `r` before the quote is used to force the regular expression specifica
...
@@ -106,17 +150,46 @@ where the `r` before the quote is used to force the regular expression specifica
For more information on matching and substitutions, look up the regular expression module on the web.
For more information on matching and substitutions, look up the regular expression module on the web.
Two common and convenient string methods are `strip()` and `split()`. The first will remove any whitespace at the beginning and end of a string:
You can also split, or tokenize, a string (to turn it into a list) like this:
```
s2 = ' A very spacy string '
print('*' + s2 + '*')
print('*' + s2.strip() + '*')
```
With `split()` we can tokenize a string (to turn it into a list of strings) like this:
```
```
print(s.split())
print(s.split())
print(s2.split())
```
By default it splits at whitespace, but it can also split at a specified delimiter:
```
s4 = ' This is, as you can see , a very weirdly spaced and punctuated string ... '
print(s4.split(','))
```
```
There are more powerful ways of dealing with this like csv files/strings, which are covered in later practicals, but even this can get you a long way.
> Note that strings in python 3 are _unicode_ so can represent Chinese characters, etc, and is therefore very flexible. However, in general you can just be blissfully ignorant of this fact.
> Note that strings in python 3 are _unicode_ so can represent Chinese characters, etc, and is therefore very flexible. However, in general you can just be blissfully ignorant of this fact.
Strings can be converted to integer or floating-point values by using the `int()` and `float()` calls:
```
sint='23'
sfp='2.03'
print(sint + sfp)
print(int(sint) + float(sfp))
print(float(sint) + float(sfp))
```
> Note that calling `int()` on a non-integer (e.g., on `sfp` above) will raise an error.
---
---
## Tuples and Lists
<aclass="anchor"id="Tuples-and-lists"></a>
## Tuples and lists
Both tuples and lists are builtin python types and are like vectors,
Both tuples and lists are builtin python types and are like vectors,
but for numerical vectors and arrays it is much better to use _numpy_
but for numerical vectors and arrays it is much better to use _numpy_
...
@@ -138,6 +211,7 @@ print('x2 is: ', x2)
...
@@ -138,6 +211,7 @@ print('x2 is: ', x2)
print('x3 is: ', x3)
print('x3 is: ', x3)
```
```
<aclass="anchor"id="Adding-to-a-list"></a>
### Adding to a list
### Adding to a list
This is easy:
This is easy:
...
@@ -148,6 +222,7 @@ a += [80]
...
@@ -148,6 +222,7 @@ a += [80]
print(a)
print(a)
```
```
<aclass="anchor"id="Indexing"></a>
### Indexing
### Indexing
Square brackets are used to index tuples, lists, dictionaries, etc. For example:
Square brackets are used to index tuples, lists, dictionaries, etc. For example:
...
@@ -195,6 +270,7 @@ but *not* an index like b[0, 1].
...
@@ -195,6 +270,7 @@ but *not* an index like b[0, 1].
> Note that `len` will only give the length of the top level.
> Note that `len` will only give the length of the top level.
> In general, numpy arrays should be preferred to nested lists when the contents are numerical.
> In general, numpy arrays should be preferred to nested lists when the contents are numerical.
<aclass="anchor"id="Slicing"></a>
### Slicing
### Slicing
A range of values for the indices can be specified to extract values from a list. For example:
A range of values for the indices can be specified to extract values from a list. For example:
...
@@ -223,6 +299,7 @@ b = [3, 4]
...
@@ -223,6 +299,7 @@ b = [3, 4]
print(a[b])
print(a[b])
```
```
<aclass="anchor"id="List-operations"></a>
### List operations
### List operations
Multiplication can be used with lists, where multiplication implements replication.
Multiplication can be used with lists, where multiplication implements replication.
...
@@ -242,6 +319,7 @@ d.pop(0)
...
@@ -242,6 +319,7 @@ d.pop(0)
print(d)
print(d)
```
```
<aclass="anchor"id="Looping"></a>
### Looping over elements in a list (or tuple)
### Looping over elements in a list (or tuple)
```
```
...
@@ -252,6 +330,7 @@ for x in d:
...
@@ -252,6 +330,7 @@ for x in d:
> Note that the indentation within the loop is _*crucial*_. All python control blocks are delineated purely by indentation.
> Note that the indentation within the loop is _*crucial*_. All python control blocks are delineated purely by indentation.
<aclass="anchor"id="Getting-help"></a>
### Getting help
### Getting help
The function `help()` can be used to get information about any variable/object/function in python. It lists the possible operations. In `ipython` you can also just type `?<blah>` or `<blah>?` instead:
The function `help()` can be used to get information about any variable/object/function in python. It lists the possible operations. In `ipython` you can also just type `?<blah>` or `<blah>?` instead:
...
@@ -272,6 +351,7 @@ dir(d)
...
@@ -272,6 +351,7 @@ dir(d)
---
---
<aclass="anchor"id="Dictionaries"></a>
## Dictionaries
## Dictionaries
These store key-value pairs. For example:
These store key-value pairs. For example:
...
@@ -287,6 +367,7 @@ The keys and values can take on almost any type, even dictionaries!
...
@@ -287,6 +367,7 @@ The keys and values can take on almost any type, even dictionaries!
Python is nothing if not flexible. However, each key must be unique
Python is nothing if not flexible. However, each key must be unique
and the dictionary must be "hashable".
and the dictionary must be "hashable".
<aclass="anchor"id="Adding-to-a-dictionary"></a>
### Adding to a dictionary
### Adding to a dictionary
This is very easy:
This is very easy:
...
@@ -295,7 +376,7 @@ e['c'] = 555 # just like in Biobank! ;)
...
@@ -295,7 +376,7 @@ e['c'] = 555 # just like in Biobank! ;)
Several variables can jointly work as loop variables in python, which is very convenient. For example:
Several variables can jointly work as loop variables in python, which is very convenient. For example:
...
@@ -329,6 +411,7 @@ for k in e:
...
@@ -329,6 +411,7 @@ for k in e:
---
---
<aclass="anchor"id="Copying-and-references"></a>
## Copying and references
## Copying and references
In python there are immutable types (e.g. numbers) and mutable types (e.g. lists). The main thing to know is that assignment can sometimes create separate copies and sometimes create references (as in C++). In general, the more complicated types are assigned via references. For example:
In python there are immutable types (e.g. numbers) and mutable types (e.g. lists). The main thing to know is that assignment can sometimes create separate copies and sometimes create references (as in C++). In general, the more complicated types are assigned via references. For example:
...
@@ -393,10 +476,16 @@ foo3(a)
...
@@ -393,10 +476,16 @@ foo3(a)
print(a)
print(a)
```
```
> Note that we have defined some functions here - and the syntax
> should be relatively intuitive. See <a href="#functions">below</a>
> for a bit more detail on function definitions.
---
---
<aclass="anchor"id="Control-flow"></a>
## Control flow
## Control flow
<aclass="anchor"id="Boolean-operators"></a>
### Boolean operators
### Boolean operators
There is a boolean type in python that can be `True` or `False` (note the capitals). Other values can also be used for True or False (e.g., 1 for True; 0 or None or [] or {} or "") although they are not considered 'equal' in the sense that the operator `==` would consider them the same.
There is a boolean type in python that can be `True` or `False` (note the capitals). Other values can also be used for True or False (e.g., 1 for True; 0 or None or [] or {} or "") although they are not considered 'equal' in the sense that the operator `==` would consider them the same.
...
@@ -420,7 +509,7 @@ print('of' in 'a number of words')
...
@@ -420,7 +509,7 @@ print('of' in 'a number of words')
print(3 in [1, 2, 3, 4])
print(3 in [1, 2, 3, 4])
```
```
<aclass="anchor"id="If-statements"></a>
### If statements
### If statements
The basic syntax of `if` statements is fairly standard, though don't forget that you _*must*_ indent the statements within the conditional/loop block as this is the way of delineating blocks of code in python. For example:
The basic syntax of `if` statements is fairly standard, though don't forget that you _*must*_ indent the statements within the conditional/loop block as this is the way of delineating blocks of code in python. For example:
...
@@ -446,6 +535,7 @@ This can be useful for functions where a variety of possible input types are bei
...
@@ -446,6 +535,7 @@ This can be useful for functions where a variety of possible input types are bei
---
---
<aclass="anchor"id="For-loops"></a>
### For loops
### For loops
The `for` loop works like in bash:
The `for` loop works like in bash:
...
@@ -480,6 +570,7 @@ for x, y in zip(alist, blist):
...
@@ -480,6 +570,7 @@ for x, y in zip(alist, blist):
This type of loop can be used with any two lists (or similar) to iterate over them jointly.
This type of loop can be used with any two lists (or similar) to iterate over them jointly.
<aclass="anchor"id="While-loops"></a>
### While loops
### While loops
The syntax for this is pretty standard:
The syntax for this is pretty standard:
...
@@ -499,10 +590,12 @@ You can also use `continue` as in other languages.
...
@@ -499,10 +590,12 @@ You can also use `continue` as in other languages.
---
---
<aclass="anchor"id="quick-intro"></a>
### A quick intro to conditional expressions and list comprehensions
### A quick intro to conditional expressions and list comprehensions
These are more advanced bits of python but are really useful and common, so worth having a little familiarity with at this stage.
These are more advanced bits of python but are really useful and common, so worth having a little familiarity with at this stage.
<aclass="anchor"id="Conditional-expressions"></a>
#### Conditional expressions
#### Conditional expressions
A general expression that can be used in python is: A `if` condition `else` B
A general expression that can be used in python is: A `if` condition `else` B
...
@@ -515,7 +608,7 @@ y = x**2 if x<0.5 else (1 - x)**2
...
@@ -515,7 +608,7 @@ y = x**2 if x<0.5 else (1 - x)**2
print(x, y)
print(x, y)
```
```
<aclass="anchor"id="List-comprehensions"></a>
#### List comprehensions
#### List comprehensions
This is a shorthand syntax for building a list like a for loop but doing it in one line, and is very popular in python. It is quite similar to mathematical set notation. For example:
This is a shorthand syntax for building a list like a for loop but doing it in one line, and is very popular in python. It is quite similar to mathematical set notation. For example:
...
@@ -529,6 +622,83 @@ print(v2)
...
@@ -529,6 +622,83 @@ print(v2)
You'll find that python programmers use this kind of construction _*a lot*_.
You'll find that python programmers use this kind of construction _*a lot*_.
---
<a class="anchor" id="functions"></a>
## Functions
You will find functions pretty familiar in python to start with,
although they have a few options which are really handy and different
from C++ or matlab (to be covered in a later practical). To start
with we'll look at a simple function but note a few key points:
*you _must_ indent everything inside the function (it is a code
block and indentation is the only way of determining this - just
like for the guts of a loop)
* you can return _whatever you want_ from a python function, but only
a single object - it is usual to package up multiple things in a
tuple or list, which is easily unpacked by the calling invocation:
e.g., `a, b, c = myfunc(x)`
* parameters are passed by reference (see section on <a
href="#Copying-and-references">copying and references</a>)
One nice feature of python functions is that you can name the
arguments when you call them, rather than only doing it by position.
For example:
```
def myfunc(x, y, z=0, flag=''):
if flag=='L1':
r = abs(x) + abs(y) + abs(z)
else:
r = (x*x + y*y + z*z)**0.5
return r
rA = myfunc(10, 20)
rB = myfunc(10, 20, flag='L1')
rC = myfunc(10, 20, flag='L1', z=30)
print(rA, rB, rC)
```
You will often see python functions called with these named arguments.
---
<aclass="anchor"id="exercise"></a>
## Exercises
Let's say you are given a single string with comma separated elements
that represent filenames and ID codes: e.g., `/vols/Data/pytreat/AAC, 165873, /vols/Data/pytreat/AAG, 170285, ...`
Write some code to do the following:
* separate out the filenames and ID codes into separate lists (ID's
should be numerical values, not strings)
* loop over the two and generate a _string_ that could be used to
rename the directories (e.g., `mv /vols/Data/pytreat/AAC /vols/Data/pytreat/S165873`) - we will cover how to actually execute these in a later practical
* convert your dual lists into a dictionary, with ID as the key
* write a small function to determine if an ID is present in this
set of not, and also return the filename if it is
* write a for loop to create a list of all the odd-numbered IDs (you can use the `%` operator for modulus - i.e., `5 % 2` is 1)
* re-write the for loop as a list comprehension
* now generate a list of the filenames corresponding to these odd-numbered IDs