Commit 7ece974e authored by Marc Gouw's avatar Marc Gouw

Merge branch 'py36_updates' into 'master'

Py36 updates

edits to recognise changes introduced in python 3.6.

See merge request !15
parents cc8eb484 1a335401
......@@ -50,7 +50,7 @@
"width": 50
},
"source": [
"![An example of the IPython-Qtshell window](images/JupyterConsole.png)"
"![An example of the Jupyter-Qtshell window](images/JupyterConsole.png)"
]
},
{
......@@ -1234,7 +1234,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.0"
"version": "3.5.2"
}
},
"nbformat": 4,
......
......@@ -504,7 +504,61 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"In Python's case, formatting of strings can be taken care of by using the `.format` method or by using the `%` operator that is common amongst a lot of languages. We will use the newer `.format()` approach, but you might prefer to use `%` - I recommend that you [read this](https://pyformat.info) for a good introduction and comparison of the two approaches."
"In Python's case, formatting of strings can be taken care of in several different ways. \n",
"\n",
"1. by using the `%` operator that is common amongst a lot of languages\n",
"2. by using the `.format` method, or\n",
"3. (from Python v3.6 onwards) by using the `f''` syntax with variable names in placeholder.\n",
"\n",
"Let's compare these options. We have three variables, `name`, `date` and `job`, which we want to substitute into some text. It's possible to control exactly the formatting of values such as dates when constructing strings like this, but for simplicity here we perform simple string substitutions at each step."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```\n",
"#variables for substitution\n",
"name = 'Betty'\n",
"date = '15th June 2016'\n",
"job = 'engineer'\n",
"\n",
"# option 1 - using the % operator\n",
"text = 'Hi, my name is %s and I am an %s. I have been an %s since %s.' % (name, job, job, date)\n",
"print('1. using %')\n",
"print(text)\n",
"\n",
"# option 2 - using the .format method of the string object\n",
"text = 'Hi, my name is {0} and I am an {1}. I have been an {1} since {2}.'.format(name, job, date)\n",
"print('2. using .format()')\n",
"print(text)\n",
"\n",
"# option 3 - using f'' (v3.6 only)\n",
"text = f'Hi, my name is {name} and I am an {job}. I have been an {job} since {date}.'\n",
"print('3. using f\\'\\' (v3.6 only)')\n",
"print(text)\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```\n",
"1. using %\n",
"Hi, my name is Betty and I am an engineer. I have been an engineer since 15th June 2016.\n",
"2. using the .format method of the string object\n",
"Hi, my name is Betty and I am an engineer. I have been an engineer since 15th June 2016.\n",
"3. using f'' (v3.6 only)\n",
"Hi, my name is Betty and I am an engineer. I have been an engineer since 15th June 2016.\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"From now on, we will use the newer `.format()` approach, but you might prefer to use `%` (or `f''` if you are using v3.6) - I recommend that you [read this](https://pyformat.info) for a good introduction and comparison of the `.format` and `%` approaches."
]
},
{
......@@ -741,7 +795,14 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"That should work as expected. Also note that the key-value pairs are traversed in the same order as we defined them. Now, although this may not surprise you, it is worth noting that in Python 2.x dictionaries are _unordered_. Although this behaviour is different in Python 3.x it is still not recommended to write code which relies on a dictionary being in a specific order: Future versions of Python are still not guaranteed to preseve order in dictionaries, and anyone trying to adapt your code to work in earlier versions of Python will most likely get unexpected results."
"That should work as expected. Python doesn’t make any promises about the order the keys will be supplied in: they will be given the way Python thinks is best. It almost certainly won’t be either the order the keys were added to the dictionary or alphabetical order*."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*__Note__ The way that dictionaries are implemented in Python fundamentally changed in v3.6, resulting in them taking up ~1/2 the space and working ~2x as fast as they used to. A side effect of this is that dictionary objects in Python 3.6 remember the order that entries were created in and you should be able to access their entries in this order. Regardless, in the examples and exercises in this course, we assume that this order cannot be relied upon - we don't expect everyone to be using v3.6 or above, and anyway this is not yet considered a 'stable' feature of the language i.e. future versions of Python are not guaranteed to preserve the order of dictionaries. When writing your own code, if you want to access dictionary entries in a particular order, you should make sure to do so by providing keys in a specific order, as we will show below."
]
},
{
......@@ -1012,7 +1073,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Debugging Exercise"
"#### _Debugging Exercise_"
]
},
{
......
......@@ -280,7 +280,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Things have improved a little. Now we are printing the name of the group each time we start a new one, but these group names are not in alphabetical order (unless you got lucky - remember that the order that keys are accessed from a dictionary using `.keys()` can't be relied upon or predicted) and they're quite hard to spot amongst the names of the students. To make sure that you understand what just happened, let's take a look at that code block above step-by-step."
"Things have improved a little. Now we are printing the name of the group each time we start a new one, but these group names are not in alphabetical order (unless you got lucky - remember that the order that keys are accessed from a dictionary using `.keys()` can't be relied upon or predicted, or you're using Python 3.6) and they're quite hard to spot amongst the names of the students. To make sure that you understand what just happened, let's take a look at that code block above step-by-step."
]
},
{
......@@ -376,7 +376,7 @@
"metadata": {},
"source": [
"As mentioned before, there are a couple of problems with the output that we are getting from the code block above. Try to find a way to make the names of the groups stand out a bit more from the names of the students. \n",
"After you have achieved that, see if you can find a way to control the order in which the groups are displayed, so that they appear alphabetically - Group A, Group B, Group C."
"After you have achieved that, see if you can find a way to explicitly control the order in which the groups are displayed, so that they appear alphabetically - Group A, Group B, Group C."
]
},
{
......@@ -636,7 +636,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Debugging exercise"
"#### _Debugging Exercise_"
]
},
{
......@@ -682,7 +682,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.1"
"version": "3.5.2"
}
},
"nbformat": 4,
......
......@@ -1256,7 +1256,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.1"
"version": "3.5.2"
}
},
"nbformat": 4,
......
......@@ -66,7 +66,7 @@ def main ():
tex = re.sub("\\\section\{.*?\}", "", tex, flags=re.DOTALL) # Delete all sections (would be duplicate)
tex = re.sub("\\\subsection\{(\d+\. )?", "\\\chapter{", tex) # rename numbered subsections to chapters
tex = re.sub("\\\documentclass\{.*?\}", "\\\documentclass{report}", tex) # change documentclass to report
tex = re.sub("\\\documentclass\[(\d+pt)\]\{.*?\}", "\\\documentclass[\\1]{report}", tex) # change documentclass to report
tex = re.sub("\\\maketitle", "\\\\tableofcontents", tex) # include tableof
with open(tex_filename, 'w') as texfile:
......
......@@ -56,7 +56,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.1"
"version": "3.5.2"
}
},
"nbformat": 4,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment