Commit aaada960 authored by Marc Gouw's avatar Marc Gouw

Small changes and edits to Worksheet 2.

parent 3446440e
......@@ -302,7 +302,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"In Python 2, `range()` will give you a list of integers, while, in Python 3, you will get a `Range` object, which can be used to generate these integers. In either case, a loop like this:"
"The `range()` function gives a `Range` object, which can be used to generate a list of integers. In Python 2 `range()` directly creates a list of integers instead of a `Range` generator, which is technically slightly different. In either case, a loop like this:"
]
},
{
......@@ -464,24 +464,21 @@
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`print 'I need to buy', amounts[i], shopping[i] # Python v2`"
]
},
{
"cell_type": "markdown",
"metadata": {},
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"or this,"
"print(“I need to buy”, amounts[i], shopping[i])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`print(“I need to buy”, amounts[i], shopping[i]) # Python v3` "
"In Python 2.x this would be: `print 'I need to buy', amounts[i], shopping[i]`"
]
},
{
......@@ -744,7 +741,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"That should work as expected. Note that 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."
"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."
]
},
{
......@@ -851,7 +848,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Go back to your shopping list code from exercise 2.4 and change the program so that the amounts and shopping items are stored in a dictionary, then print out the items and their respective amounts by looping over the dictionary. Do it twice, once looping over the the dictionary to get the keys (or use the keys to get the values) and once by getting the key-value pairs directly from the dictionary."
"Go back to your shopping list code from exercise 2.5 and change the program so that the amounts and shopping items are stored in a dictionary, then print out the items and their respective amounts by looping over the dictionary. Do it twice, once looping over the the dictionary to get the keys (or use the keys to get the values) and once by getting the key-value pairs directly from the dictionary."
]
},
{
......@@ -1095,7 +1092,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
"version": "3.6.0"
}
},
"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