Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 58 additions & 7 deletions Day_2/07_Lists.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Mutable also means that any other variables pointing to a list will be changed accordingly:"
"### Mutability has consequences.\n",
"\n",
"The fact that lists are mutable also means that any other variables pointing to a list will *also* changed if that list gets changed:"
]
},
{
Expand All @@ -221,6 +223,38 @@
"print(\"new_list: \", new_list)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Slicing creates copies.\n",
"On the other hand, variables created by assigning their value to a slice of a list are \"copies\" of that slice of the list, and they *do not* change when the original list gets changed:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"derived_list = country_list[:3]\n",
"print(\"first three countries:\", derived_list)\n",
"\n",
"country_list[1] = \"Thailand\"\n",
"print(\"changed country_list: \", country_list)\n",
"\n",
"print(\"first three countries:\", derived_list, \"...still!\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"These are the normal behaviors of lists and other mutable data types in Python, but they can sometimes have unexpected results, especially with nested data structures. (If you need to make certain that data don't change, you can use the `copy` and `deepcopy` functions, but these are too much for this introduction.)"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -471,10 +505,26 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Challenge 5: join\n",
"\n",
"Read the help file for the `join` method. \n",
"## Challenge 5: Join\n",
"\n",
"Read the help file (or the [Python documentation](https://docs.python.org/3/library/stdtypes.html?highlight=str.join#str.join)) for `join()`, a string method."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"str.join?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Using the join method, concatenate all the values in this list into one string:"
]
},
Expand Down Expand Up @@ -502,7 +552,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Now use the `join` method to make one string with all the names from the list `Wheres_Waldo`, which prints each name on a separate line. (HINT: Remember a new line is represented by `\\n`"
"Now use the `join` method to make one string with all the names from the list `Wheres_Waldo`, which prints each name on a separate line. (HINT: Remember a new line is represented by `'\\n'`.)"
]
},
{
Expand Down Expand Up @@ -558,8 +608,9 @@
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python [default]",
"language": "python",
"name": "python3"
},
Expand All @@ -573,7 +624,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.3"
"version": "3.5.2"
}
},
"nbformat": 4,
Expand Down
5 changes: 3 additions & 2 deletions Day_2/08_Loops.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -475,8 +475,9 @@
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python [default]",
"language": "python",
"name": "python3"
},
Expand All @@ -490,7 +491,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.3"
"version": "3.5.2"
}
},
"nbformat": 4,
Expand Down