Skip to content
Open
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
60 changes: 59 additions & 1 deletion lists.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1019,11 +1019,69 @@
"\n",
"Complete Exercise 3-8 in Matthes (2023, p. 45)."
]
},
{
"cell_type": "code",
"execution_count": 94,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['santiago', 'istanbul', 'florence', 'tokyo', 'beirut']\n",
"['beirut', 'florence', 'istanbul', 'santiago', 'tokyo']\n",
"['santiago', 'istanbul', 'florence', 'tokyo', 'beirut']\n",
"['santiago', 'istanbul', 'florence', 'tokyo', 'beirut']\n",
"['beirut', 'tokyo', 'florence', 'istanbul', 'santiago']\n",
"['santiago', 'istanbul', 'florence', 'tokyo', 'beirut']\n",
"['beirut', 'florence', 'istanbul', 'santiago', 'tokyo']\n",
"['tokyo', 'santiago', 'istanbul', 'florence', 'beirut']\n"
]
}
],
"source": [
"#Exercise 3-8 work shown\n",
"\n",
"#Storing five places in a list called places\n",
"places = [\"santiago\", \"istanbul\", \"florence\", \"tokyo\", \"beirut\"]\n",
"\n",
"#Printing the list in raw form\n",
"print(places)\n",
"\n",
"#Sorting list in alphabetical order\n",
"print(sorted(places))\n",
"\n",
"#Print list to show the original order is unchanged\n",
"print(places)\n",
"\n",
"#Change the order of the list to reverse alphabetical\n",
"sorted(places,reverse=True)\n",
"\n",
"#Change the order of the list back to original order:\n",
"print(places)\n",
"\n",
"#Change order of list and print to show change:\n",
"places.reverse()\n",
"print(places)\n",
"\n",
"#Change order of list to reverse and print change:\n",
"places.reverse()\n",
"print(places)\n",
"\n",
"#Use sort to change to store list in alphabetical order:\n",
"places.sort()\n",
"print(places)\n",
"\n",
"#Use sort to change list to reverse alphabetical order:\n",
"places.sort(reverse=True)\n",
"print(places)\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": ".venv",
"language": "python",
"name": "python3"
},
Expand Down