Skip to content

Instantly share code, notes, and snippets.

@AyumuSuzuki31
Last active September 21, 2021 05:24
Show Gist options
  • Save AyumuSuzuki31/02485fa3bd305325d95e077d3a884128 to your computer and use it in GitHub Desktop.
Save AyumuSuzuki31/02485fa3bd305325d95e077d3a884128 to your computer and use it in GitHub Desktop.
lists.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 5,
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.10"
},
"learntools_metadata": {
"lesson_index": 3,
"type": "tutorial"
},
"papermill": {
"default_parameters": {},
"duration": 16.753793,
"end_time": "2021-09-13T19:57:04.347333",
"environment_variables": {},
"exception": null,
"input_path": "__notebook__.ipynb",
"output_path": "__notebook__.ipynb",
"parameters": {},
"start_time": "2021-09-13T19:56:47.593540",
"version": "2.3.3"
},
"colab": {
"name": "lists.ipynb",
"provenance": [],
"collapsed_sections": [
"efe57e38",
"f353e7ca",
"4cc3f92e",
"a43d578b",
"1ebc40dc"
],
"include_colab_link": true
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/Chloe000111/02485fa3bd305325d95e077d3a884128/lists.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"papermill": {
"duration": 0.043247,
"end_time": "2021-09-13T19:56:55.087632",
"exception": false,
"start_time": "2021-09-13T19:56:55.044385",
"status": "completed"
},
"tags": [],
"id": "3ead56f4"
},
"source": [
"# Lists\n",
"\n",
"Lists in Python represent ordered sequences of values. Here is an example of how to create them:"
],
"id": "3ead56f4"
},
{
"cell_type": "code",
"metadata": {
"collapsed": true,
"execution": {
"iopub.execute_input": "2021-09-13T19:56:55.186138Z",
"iopub.status.busy": "2021-09-13T19:56:55.183999Z",
"iopub.status.idle": "2021-09-13T19:56:55.190004Z",
"shell.execute_reply": "2021-09-13T19:56:55.189292Z"
},
"papermill": {
"duration": 0.057202,
"end_time": "2021-09-13T19:56:55.190173",
"exception": false,
"start_time": "2021-09-13T19:56:55.132971",
"status": "completed"
},
"tags": [],
"id": "2d1f2b24"
},
"source": [
"primes = [2, 3, 5, 7]"
],
"id": "2d1f2b24",
"execution_count": 1,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"papermill": {
"duration": 0.043179,
"end_time": "2021-09-13T19:56:55.276714",
"exception": false,
"start_time": "2021-09-13T19:56:55.233535",
"status": "completed"
},
"tags": [],
"id": "b8019fcd"
},
"source": [
"We can put other types of things in lists:"
],
"id": "b8019fcd"
},
{
"cell_type": "code",
"metadata": {
"collapsed": true,
"execution": {
"iopub.execute_input": "2021-09-13T19:56:55.366235Z",
"iopub.status.busy": "2021-09-13T19:56:55.365578Z",
"iopub.status.idle": "2021-09-13T19:56:55.368718Z",
"shell.execute_reply": "2021-09-13T19:56:55.368133Z"
},
"papermill": {
"duration": 0.049437,
"end_time": "2021-09-13T19:56:55.368858",
"exception": false,
"start_time": "2021-09-13T19:56:55.319421",
"status": "completed"
},
"tags": [],
"id": "3709f7fe"
},
"source": [
"planets = ['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune']"
],
"id": "3709f7fe",
"execution_count": 2,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"papermill": {
"duration": 0.042217,
"end_time": "2021-09-13T19:56:55.453449",
"exception": false,
"start_time": "2021-09-13T19:56:55.411232",
"status": "completed"
},
"tags": [],
"id": "85530640"
},
"source": [
"We can even make a list of lists:"
],
"id": "85530640"
},
{
"cell_type": "code",
"metadata": {
"collapsed": true,
"execution": {
"iopub.execute_input": "2021-09-13T19:56:55.543495Z",
"iopub.status.busy": "2021-09-13T19:56:55.542885Z",
"iopub.status.idle": "2021-09-13T19:56:55.545723Z",
"shell.execute_reply": "2021-09-13T19:56:55.545227Z"
},
"papermill": {
"duration": 0.050055,
"end_time": "2021-09-13T19:56:55.545851",
"exception": false,
"start_time": "2021-09-13T19:56:55.495796",
"status": "completed"
},
"tags": [],
"id": "22c47f44"
},
"source": [
"hands = [\n",
" ['J', 'Q', 'K'],\n",
" ['2', '2', '2'],\n",
" ['6', 'A', 'K'], # (Comma after the last element is optional)\n",
"]\n",
"# (I could also have written this on one line, but it can get hard to read)\n",
"hands = [['J', 'Q', 'K'], ['2', '2', '2'], ['6', 'A', 'K']]"
],
"id": "22c47f44",
"execution_count": 3,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"papermill": {
"duration": 0.04197,
"end_time": "2021-09-13T19:56:55.630484",
"exception": false,
"start_time": "2021-09-13T19:56:55.588514",
"status": "completed"
},
"tags": [],
"id": "2ea21b1b"
},
"source": [
"A list can contain a mix of different types of variables:"
],
"id": "2ea21b1b"
},
{
"cell_type": "code",
"metadata": {
"collapsed": true,
"execution": {
"iopub.execute_input": "2021-09-13T19:56:55.718191Z",
"iopub.status.busy": "2021-09-13T19:56:55.717546Z",
"iopub.status.idle": "2021-09-13T19:56:55.721999Z",
"shell.execute_reply": "2021-09-13T19:56:55.721380Z"
},
"papermill": {
"duration": 0.049351,
"end_time": "2021-09-13T19:56:55.722133",
"exception": false,
"start_time": "2021-09-13T19:56:55.672782",
"status": "completed"
},
"tags": [],
"id": "fe2b5010"
},
"source": [
"my_favourite_things = [32, 'raindrops on roses', help]\n",
"# (Yes, Python's help function is *definitely* one of my favourite things)"
],
"id": "fe2b5010",
"execution_count": 4,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"papermill": {
"duration": 0.043855,
"end_time": "2021-09-13T19:56:55.808876",
"exception": false,
"start_time": "2021-09-13T19:56:55.765021",
"status": "completed"
},
"tags": [],
"id": "1aeb940a"
},
"source": [
"## Indexing\n",
"\n",
"You can access individual list elements with square brackets.\n",
"\n",
"Which planet is closest to the sun? Python uses *zero-based* indexing, so the first element has index 0."
],
"id": "1aeb940a"
},
{
"cell_type": "code",
"metadata": {
"execution": {
"iopub.execute_input": "2021-09-13T19:56:55.897232Z",
"iopub.status.busy": "2021-09-13T19:56:55.896597Z",
"iopub.status.idle": "2021-09-13T19:56:55.903091Z",
"shell.execute_reply": "2021-09-13T19:56:55.903691Z"
},
"papermill": {
"duration": 0.05215,
"end_time": "2021-09-13T19:56:55.903845",
"exception": false,
"start_time": "2021-09-13T19:56:55.851695",
"status": "completed"
},
"tags": [],
"id": "951b3a8e",
"outputId": "dbbedc9d-1ba3-48e3-e64d-69192ff5b018",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
}
},
"source": [
"planets[0]"
],
"id": "951b3a8e",
"execution_count": 5,
"outputs": [
{
"output_type": "execute_result",
"data": {
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
},
"text/plain": [
"'Mercury'"
]
},
"metadata": {},
"execution_count": 5
}
]
},
{
"cell_type": "markdown",
"metadata": {
"papermill": {
"duration": 0.043851,
"end_time": "2021-09-13T19:56:55.991805",
"exception": false,
"start_time": "2021-09-13T19:56:55.947954",
"status": "completed"
},
"tags": [],
"id": "f9b8775b"
},
"source": [
"What's the next closest planet?"
],
"id": "f9b8775b"
},
{
"cell_type": "code",
"metadata": {
"execution": {
"iopub.execute_input": "2021-09-13T19:56:56.082408Z",
"iopub.status.busy": "2021-09-13T19:56:56.081637Z",
"iopub.status.idle": "2021-09-13T19:56:56.085954Z",
"shell.execute_reply": "2021-09-13T19:56:56.086493Z"
},
"papermill": {
"duration": 0.050943,
"end_time": "2021-09-13T19:56:56.086678",
"exception": false,
"start_time": "2021-09-13T19:56:56.035735",
"status": "completed"
},
"tags": [],
"id": "6e6771de",
"outputId": "c52ac874-b6b5-4d8b-d52d-c5a01ad6977c"
},
"source": [
"planets[1]"
],
"id": "6e6771de",
"execution_count": null,
"outputs": [
{
"data": {
"text/plain": [
"'Venus'"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"papermill": {
"duration": 0.043363,
"end_time": "2021-09-13T19:56:56.173689",
"exception": false,
"start_time": "2021-09-13T19:56:56.130326",
"status": "completed"
},
"tags": [],
"id": "5b4c1a92"
},
"source": [
"Which planet is *furthest* from the sun?\n",
"\n",
"Elements at the end of the list can be accessed with negative numbers, starting from -1:"
],
"id": "5b4c1a92"
},
{
"cell_type": "code",
"metadata": {
"execution": {
"iopub.execute_input": "2021-09-13T19:56:56.265742Z",
"iopub.status.busy": "2021-09-13T19:56:56.263106Z",
"iopub.status.idle": "2021-09-13T19:56:56.268426Z",
"shell.execute_reply": "2021-09-13T19:56:56.268907Z"
},
"papermill": {
"duration": 0.051943,
"end_time": "2021-09-13T19:56:56.269067",
"exception": false,
"start_time": "2021-09-13T19:56:56.217124",
"status": "completed"
},
"tags": [],
"id": "6748991f",
"outputId": "c0a80875-2d61-4e82-bf8d-5177e0fcea25"
},
"source": [
"planets[-1]"
],
"id": "6748991f",
"execution_count": null,
"outputs": [
{
"data": {
"text/plain": [
"'Neptune'"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"cell_type": "code",
"metadata": {
"execution": {
"iopub.execute_input": "2021-09-13T19:56:56.364493Z",
"iopub.status.busy": "2021-09-13T19:56:56.363652Z",
"iopub.status.idle": "2021-09-13T19:56:56.366758Z",
"shell.execute_reply": "2021-09-13T19:56:56.367252Z"
},
"papermill": {
"duration": 0.053186,
"end_time": "2021-09-13T19:56:56.367415",
"exception": false,
"start_time": "2021-09-13T19:56:56.314229",
"status": "completed"
},
"tags": [],
"id": "05fbb089",
"outputId": "771bf74c-723f-48e9-d3cf-1381c253ed20"
},
"source": [
"planets[-2]"
],
"id": "05fbb089",
"execution_count": null,
"outputs": [
{
"data": {
"text/plain": [
"'Uranus'"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"papermill": {
"duration": 0.044007,
"end_time": "2021-09-13T19:56:56.455926",
"exception": false,
"start_time": "2021-09-13T19:56:56.411919",
"status": "completed"
},
"tags": [],
"id": "efe57e38"
},
"source": [
"## Slicing\n",
"\n",
"What are the first three planets? We can answer this question using *slicing*:"
],
"id": "efe57e38"
},
{
"cell_type": "code",
"metadata": {
"execution": {
"iopub.execute_input": "2021-09-13T19:56:56.548794Z",
"iopub.status.busy": "2021-09-13T19:56:56.547839Z",
"iopub.status.idle": "2021-09-13T19:56:56.553332Z",
"shell.execute_reply": "2021-09-13T19:56:56.553807Z"
},
"papermill": {
"duration": 0.053649,
"end_time": "2021-09-13T19:56:56.553970",
"exception": false,
"start_time": "2021-09-13T19:56:56.500321",
"status": "completed"
},
"tags": [],
"id": "047fcd8c",
"outputId": "4c3f7f09-8331-4682-8ec4-896e7344f7f2"
},
"source": [
"planets[0:3]"
],
"id": "047fcd8c",
"execution_count": null,
"outputs": [
{
"data": {
"text/plain": [
"['Mercury', 'Venus', 'Earth']"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"papermill": {
"duration": 0.052147,
"end_time": "2021-09-13T19:56:56.657864",
"exception": false,
"start_time": "2021-09-13T19:56:56.605717",
"status": "completed"
},
"tags": [],
"id": "1c8e74b1"
},
"source": [
"`planets[0:3]` is our way of asking for the elements of `planets` starting from index 0 and continuing up to *but not including* index 3.\n",
"\n",
"The starting and ending indices are both optional. If I leave out the start index, it's assumed to be 0. So I could rewrite the expression above as:"
],
"id": "1c8e74b1"
},
{
"cell_type": "code",
"metadata": {
"execution": {
"iopub.execute_input": "2021-09-13T19:56:56.757924Z",
"iopub.status.busy": "2021-09-13T19:56:56.757293Z",
"iopub.status.idle": "2021-09-13T19:56:56.760695Z",
"shell.execute_reply": "2021-09-13T19:56:56.760123Z"
},
"papermill": {
"duration": 0.055184,
"end_time": "2021-09-13T19:56:56.760828",
"exception": false,
"start_time": "2021-09-13T19:56:56.705644",
"status": "completed"
},
"tags": [],
"id": "ca1614a4",
"outputId": "28cc037b-401d-4e79-891e-9feec7fb14cb"
},
"source": [
"planets[:3]"
],
"id": "ca1614a4",
"execution_count": null,
"outputs": [
{
"data": {
"text/plain": [
"['Mercury', 'Venus', 'Earth']"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"papermill": {
"duration": 0.044336,
"end_time": "2021-09-13T19:56:56.851263",
"exception": false,
"start_time": "2021-09-13T19:56:56.806927",
"status": "completed"
},
"tags": [],
"id": "aa80ddc4"
},
"source": [
"If I leave out the end index, it's assumed to be the length of the list."
],
"id": "aa80ddc4"
},
{
"cell_type": "code",
"metadata": {
"execution": {
"iopub.execute_input": "2021-09-13T19:56:56.947609Z",
"iopub.status.busy": "2021-09-13T19:56:56.946837Z",
"iopub.status.idle": "2021-09-13T19:56:56.950495Z",
"shell.execute_reply": "2021-09-13T19:56:56.949857Z"
},
"papermill": {
"duration": 0.053984,
"end_time": "2021-09-13T19:56:56.950631",
"exception": false,
"start_time": "2021-09-13T19:56:56.896647",
"status": "completed"
},
"tags": [],
"id": "9fda10c1",
"outputId": "344f2b33-68ba-44ca-ce8d-bc1071bee74a"
},
"source": [
"planets[3:]"
],
"id": "9fda10c1",
"execution_count": null,
"outputs": [
{
"data": {
"text/plain": [
"['Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune']"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"papermill": {
"duration": 0.047881,
"end_time": "2021-09-13T19:56:57.045113",
"exception": false,
"start_time": "2021-09-13T19:56:56.997232",
"status": "completed"
},
"tags": [],
"id": "63154e51"
},
"source": [
"i.e. the expression above means \"give me all the planets from index 3 onward\".\n",
"\n",
"We can also use negative indices when slicing:"
],
"id": "63154e51"
},
{
"cell_type": "code",
"metadata": {
"execution": {
"iopub.execute_input": "2021-09-13T19:56:57.163235Z",
"iopub.status.busy": "2021-09-13T19:56:57.162599Z",
"iopub.status.idle": "2021-09-13T19:56:57.164982Z",
"shell.execute_reply": "2021-09-13T19:56:57.165465Z"
},
"papermill": {
"duration": 0.059012,
"end_time": "2021-09-13T19:56:57.165628",
"exception": false,
"start_time": "2021-09-13T19:56:57.106616",
"status": "completed"
},
"tags": [],
"id": "89ec9b90",
"outputId": "5e63aec0-cd24-4913-d6cf-e97807633cf6"
},
"source": [
"# All the planets except the first and last\n",
"planets[1:-1]"
],
"id": "89ec9b90",
"execution_count": null,
"outputs": [
{
"data": {
"text/plain": [
"['Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus']"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"cell_type": "code",
"metadata": {
"execution": {
"iopub.execute_input": "2021-09-13T19:56:57.260898Z",
"iopub.status.busy": "2021-09-13T19:56:57.260308Z",
"iopub.status.idle": "2021-09-13T19:56:57.264329Z",
"shell.execute_reply": "2021-09-13T19:56:57.264823Z"
},
"papermill": {
"duration": 0.053315,
"end_time": "2021-09-13T19:56:57.264975",
"exception": false,
"start_time": "2021-09-13T19:56:57.211660",
"status": "completed"
},
"tags": [],
"id": "4d71ceb5",
"outputId": "4091ea06-4950-4b74-877d-0e5f7470b460"
},
"source": [
"# The last 3 planets\n",
"planets[-3:]"
],
"id": "4d71ceb5",
"execution_count": null,
"outputs": [
{
"data": {
"text/plain": [
"['Saturn', 'Uranus', 'Neptune']"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"papermill": {
"duration": 0.046547,
"end_time": "2021-09-13T19:56:57.359069",
"exception": false,
"start_time": "2021-09-13T19:56:57.312522",
"status": "completed"
},
"tags": [],
"id": "f353e7ca"
},
"source": [
"## Changing lists\n",
"\n",
"Lists are \"mutable\", meaning they can be modified \"in place\".\n",
"\n",
"One way to modify a list is to assign to an index or slice expression.\n",
"\n",
"For example, let's say we want to rename Mars:"
],
"id": "f353e7ca"
},
{
"cell_type": "code",
"metadata": {
"execution": {
"iopub.execute_input": "2021-09-13T19:56:57.458384Z",
"iopub.status.busy": "2021-09-13T19:56:57.457598Z",
"iopub.status.idle": "2021-09-13T19:56:57.461429Z",
"shell.execute_reply": "2021-09-13T19:56:57.460830Z"
},
"papermill": {
"duration": 0.055605,
"end_time": "2021-09-13T19:56:57.461555",
"exception": false,
"start_time": "2021-09-13T19:56:57.405950",
"status": "completed"
},
"tags": [],
"id": "19476f5e",
"outputId": "59c5ed50-180a-417e-a23a-5f8c017db509"
},
"source": [
"planets[3] = 'Malacandra'\n",
"planets"
],
"id": "19476f5e",
"execution_count": null,
"outputs": [
{
"data": {
"text/plain": [
"['Mercury',\n",
" 'Venus',\n",
" 'Earth',\n",
" 'Malacandra',\n",
" 'Jupiter',\n",
" 'Saturn',\n",
" 'Uranus',\n",
" 'Neptune']"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"papermill": {
"duration": 0.06136,
"end_time": "2021-09-13T19:56:57.572026",
"exception": false,
"start_time": "2021-09-13T19:56:57.510666",
"status": "completed"
},
"tags": [],
"id": "59a1059c"
},
"source": [
"Hm, that's quite a mouthful. Let's compensate by shortening the names of the first 3 planets."
],
"id": "59a1059c"
},
{
"cell_type": "code",
"metadata": {
"execution": {
"iopub.execute_input": "2021-09-13T19:56:57.681069Z",
"iopub.status.busy": "2021-09-13T19:56:57.680437Z",
"iopub.status.idle": "2021-09-13T19:56:57.683014Z",
"shell.execute_reply": "2021-09-13T19:56:57.683506Z"
},
"papermill": {
"duration": 0.056358,
"end_time": "2021-09-13T19:56:57.683682",
"exception": false,
"start_time": "2021-09-13T19:56:57.627324",
"status": "completed"
},
"tags": [],
"id": "e50d84d0",
"outputId": "f9187210-7662-4fed-dcb1-a37771deac67"
},
"source": [
"planets[:3] = ['Mur', 'Vee', 'Ur']\n",
"print(planets)\n",
"# That was silly. Let's give them back their old names\n",
"planets[:4] = ['Mercury', 'Venus', 'Earth', 'Mars',]"
],
"id": "e50d84d0",
"execution_count": null,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['Mur', 'Vee', 'Ur', 'Malacandra', 'Jupiter', 'Saturn', 'Uranus', 'Neptune']\n"
]
}
]
},
{
"cell_type": "markdown",
"metadata": {
"papermill": {
"duration": 0.047336,
"end_time": "2021-09-13T19:56:57.778820",
"exception": false,
"start_time": "2021-09-13T19:56:57.731484",
"status": "completed"
},
"tags": [],
"id": "4cc3f92e"
},
"source": [
"## List functions\n",
"\n",
"Python has several useful functions for working with lists.\n",
"\n",
"`len` gives the length of a list:"
],
"id": "4cc3f92e"
},
{
"cell_type": "code",
"metadata": {
"execution": {
"iopub.execute_input": "2021-09-13T19:56:57.877349Z",
"iopub.status.busy": "2021-09-13T19:56:57.876732Z",
"iopub.status.idle": "2021-09-13T19:56:57.881215Z",
"shell.execute_reply": "2021-09-13T19:56:57.881693Z"
},
"papermill": {
"duration": 0.055133,
"end_time": "2021-09-13T19:56:57.881854",
"exception": false,
"start_time": "2021-09-13T19:56:57.826721",
"status": "completed"
},
"tags": [],
"id": "607c61da",
"outputId": "b30c68be-ccc4-4b3b-e608-15168c9c7e85"
},
"source": [
"# How many planets are there?\n",
"len(planets)"
],
"id": "607c61da",
"execution_count": null,
"outputs": [
{
"data": {
"text/plain": [
"8"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"papermill": {
"duration": 0.048549,
"end_time": "2021-09-13T19:56:57.979193",
"exception": false,
"start_time": "2021-09-13T19:56:57.930644",
"status": "completed"
},
"tags": [],
"id": "02ec6728"
},
"source": [
"`sorted` returns a sorted version of a list:"
],
"id": "02ec6728"
},
{
"cell_type": "code",
"metadata": {
"execution": {
"iopub.execute_input": "2021-09-13T19:56:58.082514Z",
"iopub.status.busy": "2021-09-13T19:56:58.081831Z",
"iopub.status.idle": "2021-09-13T19:56:58.084416Z",
"shell.execute_reply": "2021-09-13T19:56:58.084920Z"
},
"papermill": {
"duration": 0.056527,
"end_time": "2021-09-13T19:56:58.085087",
"exception": false,
"start_time": "2021-09-13T19:56:58.028560",
"status": "completed"
},
"tags": [],
"id": "7d6799c9",
"outputId": "03926b64-293c-4d1d-e7a5-4f8f34a8e436"
},
"source": [
"# The planets sorted in alphabetical order\n",
"sorted(planets)"
],
"id": "7d6799c9",
"execution_count": null,
"outputs": [
{
"data": {
"text/plain": [
"['Earth', 'Jupiter', 'Mars', 'Mercury', 'Neptune', 'Saturn', 'Uranus', 'Venus']"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"papermill": {
"duration": 0.049118,
"end_time": "2021-09-13T19:56:58.183933",
"exception": false,
"start_time": "2021-09-13T19:56:58.134815",
"status": "completed"
},
"tags": [],
"id": "5e78bd8f"
},
"source": [
"`sum` does what you might expect:"
],
"id": "5e78bd8f"
},
{
"cell_type": "code",
"metadata": {
"execution": {
"iopub.execute_input": "2021-09-13T19:56:58.285817Z",
"iopub.status.busy": "2021-09-13T19:56:58.285239Z",
"iopub.status.idle": "2021-09-13T19:56:58.289608Z",
"shell.execute_reply": "2021-09-13T19:56:58.290057Z"
},
"papermill": {
"duration": 0.056694,
"end_time": "2021-09-13T19:56:58.290235",
"exception": false,
"start_time": "2021-09-13T19:56:58.233541",
"status": "completed"
},
"tags": [],
"id": "688a9b44",
"outputId": "226442e7-0fe9-452f-fff5-e36330da717e"
},
"source": [
"primes = [2, 3, 5, 7]\n",
"sum(primes)"
],
"id": "688a9b44",
"execution_count": null,
"outputs": [
{
"data": {
"text/plain": [
"17"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"papermill": {
"duration": 0.048947,
"end_time": "2021-09-13T19:56:58.389466",
"exception": false,
"start_time": "2021-09-13T19:56:58.340519",
"status": "completed"
},
"tags": [],
"id": "356e806c"
},
"source": [
"We've previously used the `min` and `max` to get the minimum or maximum of several arguments. But we can also pass in a single list argument."
],
"id": "356e806c"
},
{
"cell_type": "code",
"metadata": {
"execution": {
"iopub.execute_input": "2021-09-13T19:56:58.492613Z",
"iopub.status.busy": "2021-09-13T19:56:58.491962Z",
"iopub.status.idle": "2021-09-13T19:56:58.494374Z",
"shell.execute_reply": "2021-09-13T19:56:58.494848Z"
},
"papermill": {
"duration": 0.056528,
"end_time": "2021-09-13T19:56:58.495001",
"exception": false,
"start_time": "2021-09-13T19:56:58.438473",
"status": "completed"
},
"tags": [],
"id": "d0d0f4fb",
"outputId": "690bba18-eefc-469c-d55a-20a30e1e3b16"
},
"source": [
"max(primes)"
],
"id": "d0d0f4fb",
"execution_count": null,
"outputs": [
{
"data": {
"text/plain": [
"7"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"papermill": {
"duration": 0.054622,
"end_time": "2021-09-13T19:56:58.599189",
"exception": false,
"start_time": "2021-09-13T19:56:58.544567",
"status": "completed"
},
"tags": [],
"id": "a43d578b"
},
"source": [
"## Interlude: objects\n",
"\n",
"I've used the term 'object' a lot so far - you may have even read that *everything* in Python is an object. What does that mean?\n",
"\n",
"In short, objects carry some things around with them. You access that stuff using Python's dot syntax.\n",
"\n",
"For example, numbers in Python carry around an associated variable called `imag` representing their imaginary part. (You'll probably never need to use this unless you're doing some very weird math.)"
],
"id": "a43d578b"
},
{
"cell_type": "code",
"metadata": {
"execution": {
"iopub.execute_input": "2021-09-13T19:56:58.722132Z",
"iopub.status.busy": "2021-09-13T19:56:58.721390Z",
"iopub.status.idle": "2021-09-13T19:56:58.724043Z",
"shell.execute_reply": "2021-09-13T19:56:58.724607Z"
},
"papermill": {
"duration": 0.062488,
"end_time": "2021-09-13T19:56:58.724791",
"exception": false,
"start_time": "2021-09-13T19:56:58.662303",
"status": "completed"
},
"tags": [],
"id": "83252b0d",
"outputId": "42ce8dbd-f55a-4574-a142-33b2bec46457"
},
"source": [
"x = 12\n",
"# x is a real number, so its imaginary part is 0.\n",
"print(x.imag)\n",
"# Here's how to make a complex number, in case you've ever been curious:\n",
"c = 12 + 3j\n",
"print(c.imag)"
],
"id": "83252b0d",
"execution_count": null,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"3.0\n"
]
}
]
},
{
"cell_type": "markdown",
"metadata": {
"papermill": {
"duration": 0.050213,
"end_time": "2021-09-13T19:56:58.825694",
"exception": false,
"start_time": "2021-09-13T19:56:58.775481",
"status": "completed"
},
"tags": [],
"id": "7eaaf920"
},
"source": [
"The things an object carries around can also include functions. A function attached to an object is called a **method**. (Non-function things attached to an object, such as `imag`, are called *attributes*).\n",
"\n",
"For example, numbers have a method called `bit_length`. Again, we access it using dot syntax:"
],
"id": "7eaaf920"
},
{
"cell_type": "code",
"metadata": {
"execution": {
"iopub.execute_input": "2021-09-13T19:56:58.929121Z",
"iopub.status.busy": "2021-09-13T19:56:58.928510Z",
"iopub.status.idle": "2021-09-13T19:56:58.933630Z",
"shell.execute_reply": "2021-09-13T19:56:58.934108Z"
},
"papermill": {
"duration": 0.058341,
"end_time": "2021-09-13T19:56:58.934289",
"exception": false,
"start_time": "2021-09-13T19:56:58.875948",
"status": "completed"
},
"tags": [],
"id": "8d30767c",
"outputId": "3ed0744f-1cd7-430c-dfe8-c0811a15f4a8"
},
"source": [
"x.bit_length"
],
"id": "8d30767c",
"execution_count": null,
"outputs": [
{
"data": {
"text/plain": [
"<function int.bit_length()>"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"papermill": {
"duration": 0.050257,
"end_time": "2021-09-13T19:56:59.035290",
"exception": false,
"start_time": "2021-09-13T19:56:58.985033",
"status": "completed"
},
"tags": [],
"id": "68d9172a"
},
"source": [
"To actually call it, we add parentheses:"
],
"id": "68d9172a"
},
{
"cell_type": "code",
"metadata": {
"execution": {
"iopub.execute_input": "2021-09-13T19:56:59.141663Z",
"iopub.status.busy": "2021-09-13T19:56:59.140246Z",
"iopub.status.idle": "2021-09-13T19:56:59.146119Z",
"shell.execute_reply": "2021-09-13T19:56:59.146630Z"
},
"papermill": {
"duration": 0.060253,
"end_time": "2021-09-13T19:56:59.146794",
"exception": false,
"start_time": "2021-09-13T19:56:59.086541",
"status": "completed"
},
"tags": [],
"id": "ce96a14a",
"outputId": "c0c5c7bc-a5f0-40dc-9ce7-8526b6daa30c"
},
"source": [
"x.bit_length()"
],
"id": "ce96a14a",
"execution_count": null,
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"papermill": {
"duration": 0.050933,
"end_time": "2021-09-13T19:56:59.248885",
"exception": false,
"start_time": "2021-09-13T19:56:59.197952",
"status": "completed"
},
"tags": [],
"id": "833b1bfe"
},
"source": [
"> **Aside:** You've actually been calling methods already if you've been doing the exercises. In the exercise notebooks `q1`, `q2`, `q3`, etc. are all objects which have methods called `check`, `hint`, and `solution`.\n",
"\n",
"In the same way that we can pass functions to the `help` function (e.g. `help(max)`), we can also pass in methods:"
],
"id": "833b1bfe"
},
{
"cell_type": "code",
"metadata": {
"execution": {
"iopub.execute_input": "2021-09-13T19:56:59.356948Z",
"iopub.status.busy": "2021-09-13T19:56:59.356293Z",
"iopub.status.idle": "2021-09-13T19:56:59.358900Z",
"shell.execute_reply": "2021-09-13T19:56:59.359413Z"
},
"papermill": {
"duration": 0.059496,
"end_time": "2021-09-13T19:56:59.359578",
"exception": false,
"start_time": "2021-09-13T19:56:59.300082",
"status": "completed"
},
"tags": [],
"id": "c6db64a7",
"outputId": "19bdaab1-59d7-4b4f-b532-329d4bd34010"
},
"source": [
"help(x.bit_length)"
],
"id": "c6db64a7",
"execution_count": null,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on built-in function bit_length:\n",
"\n",
"bit_length() method of builtins.int instance\n",
" Number of bits necessary to represent self in binary.\n",
" \n",
" >>> bin(37)\n",
" '0b100101'\n",
" >>> (37).bit_length()\n",
" 6\n",
"\n"
]
}
]
},
{
"cell_type": "markdown",
"metadata": {
"papermill": {
"duration": 0.051069,
"end_time": "2021-09-13T19:56:59.462477",
"exception": false,
"start_time": "2021-09-13T19:56:59.411408",
"status": "completed"
},
"tags": [],
"id": "83c60337"
},
"source": [
"<!-- TODO:\n",
"dir?\n",
"A useful builtin method for interacting with objects is `dir`. `dir` asks: what are the names of all the things (methods, and attributes) that this object is carrying around?\n",
"help(x)?\n",
"-->\n",
"\n",
"The examples above were utterly obscure. None of the types of objects we've looked at so far (numbers, functions, booleans) have attributes or methods you're likely ever to use.\n",
"\n",
"But it turns out that lists have several methods which you'll use all the time."
],
"id": "83c60337"
},
{
"cell_type": "markdown",
"metadata": {
"papermill": {
"duration": 0.051403,
"end_time": "2021-09-13T19:56:59.565285",
"exception": false,
"start_time": "2021-09-13T19:56:59.513882",
"status": "completed"
},
"tags": [],
"id": "e1dd9838"
},
"source": [
"## List methods"
],
"id": "e1dd9838"
},
{
"cell_type": "markdown",
"metadata": {
"papermill": {
"duration": 0.052081,
"end_time": "2021-09-13T19:56:59.669521",
"exception": false,
"start_time": "2021-09-13T19:56:59.617440",
"status": "completed"
},
"tags": [],
"id": "bb67e0c7"
},
"source": [
"`list.append` modifies a list by adding an item to the end:"
],
"id": "bb67e0c7"
},
{
"cell_type": "code",
"metadata": {
"collapsed": true,
"execution": {
"iopub.execute_input": "2021-09-13T19:56:59.774977Z",
"iopub.status.busy": "2021-09-13T19:56:59.774404Z",
"iopub.status.idle": "2021-09-13T19:56:59.777859Z",
"shell.execute_reply": "2021-09-13T19:56:59.778364Z"
},
"papermill": {
"duration": 0.057663,
"end_time": "2021-09-13T19:56:59.778528",
"exception": false,
"start_time": "2021-09-13T19:56:59.720865",
"status": "completed"
},
"tags": [],
"id": "91d3c86d"
},
"source": [
"# Pluto is a planet darn it!\n",
"planets.append('Pluto')"
],
"id": "91d3c86d",
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"papermill": {
"duration": 0.05111,
"end_time": "2021-09-13T19:56:59.881681",
"exception": false,
"start_time": "2021-09-13T19:56:59.830571",
"status": "completed"
},
"tags": [],
"id": "d90cdde4"
},
"source": [
"Why does the cell above have no output? Let's check the documentation by calling `help(planets.append)`.\n",
"\n",
"> **Aside:** `append` is a method carried around by *all* objects of type list, not just `planets`, so we also could have called `help(list.append)`. However, if we try to call `help(append)`, Python will complain that no variable exists called \"append\". The \"append\" name only exists within lists - it doesn't exist as a standalone name like builtin functions such as `max` or `len`."
],
"id": "d90cdde4"
},
{
"cell_type": "code",
"metadata": {
"execution": {
"iopub.execute_input": "2021-09-13T19:56:59.990700Z",
"iopub.status.busy": "2021-09-13T19:56:59.990005Z",
"iopub.status.idle": "2021-09-13T19:56:59.993660Z",
"shell.execute_reply": "2021-09-13T19:56:59.993181Z"
},
"papermill": {
"duration": 0.060862,
"end_time": "2021-09-13T19:56:59.993796",
"exception": false,
"start_time": "2021-09-13T19:56:59.932934",
"status": "completed"
},
"tags": [],
"id": "56bd4dc9",
"outputId": "f7b49918-8c06-4d35-e423-4d005b18f3f4"
},
"source": [
"help(planets.append)"
],
"id": "56bd4dc9",
"execution_count": null,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on built-in function append:\n",
"\n",
"append(object, /) method of builtins.list instance\n",
" Append object to the end of the list.\n",
"\n"
]
}
]
},
{
"cell_type": "markdown",
"metadata": {
"papermill": {
"duration": 0.051991,
"end_time": "2021-09-13T19:57:00.098208",
"exception": false,
"start_time": "2021-09-13T19:57:00.046217",
"status": "completed"
},
"tags": [],
"id": "ba257a7a"
},
"source": [
"The `-> None` part is telling us that `list.append` doesn't return anything. But if we check the value of `planets`, we can see that the method call modified the value of `planets`:"
],
"id": "ba257a7a"
},
{
"cell_type": "code",
"metadata": {
"execution": {
"iopub.execute_input": "2021-09-13T19:57:00.207235Z",
"iopub.status.busy": "2021-09-13T19:57:00.206604Z",
"iopub.status.idle": "2021-09-13T19:57:00.209375Z",
"shell.execute_reply": "2021-09-13T19:57:00.209869Z"
},
"papermill": {
"duration": 0.059574,
"end_time": "2021-09-13T19:57:00.210038",
"exception": false,
"start_time": "2021-09-13T19:57:00.150464",
"status": "completed"
},
"tags": [],
"id": "a9c00bab",
"outputId": "c5fddc96-264b-4bcf-fbee-e28043825a48"
},
"source": [
"planets"
],
"id": "a9c00bab",
"execution_count": null,
"outputs": [
{
"data": {
"text/plain": [
"['Mercury',\n",
" 'Venus',\n",
" 'Earth',\n",
" 'Mars',\n",
" 'Jupiter',\n",
" 'Saturn',\n",
" 'Uranus',\n",
" 'Neptune',\n",
" 'Pluto']"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"papermill": {
"duration": 0.052199,
"end_time": "2021-09-13T19:57:00.314108",
"exception": false,
"start_time": "2021-09-13T19:57:00.261909",
"status": "completed"
},
"tags": [],
"id": "c2871951"
},
"source": [
"`list.pop` removes and returns the last element of a list:"
],
"id": "c2871951"
},
{
"cell_type": "code",
"metadata": {
"execution": {
"iopub.execute_input": "2021-09-13T19:57:00.423451Z",
"iopub.status.busy": "2021-09-13T19:57:00.422809Z",
"iopub.status.idle": "2021-09-13T19:57:00.427814Z",
"shell.execute_reply": "2021-09-13T19:57:00.427273Z"
},
"papermill": {
"duration": 0.06049,
"end_time": "2021-09-13T19:57:00.427964",
"exception": false,
"start_time": "2021-09-13T19:57:00.367474",
"status": "completed"
},
"tags": [],
"id": "c07fc27b",
"outputId": "368ec503-1e2a-4416-b3ed-f7391002f325",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
}
},
"source": [
"planets.pop()"
],
"id": "c07fc27b",
"execution_count": 6,
"outputs": [
{
"output_type": "execute_result",
"data": {
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
},
"text/plain": [
"'Neptune'"
]
},
"metadata": {},
"execution_count": 6
}
]
},
{
"cell_type": "code",
"metadata": {
"execution": {
"iopub.execute_input": "2021-09-13T19:57:00.541085Z",
"iopub.status.busy": "2021-09-13T19:57:00.539032Z",
"iopub.status.idle": "2021-09-13T19:57:00.543332Z",
"shell.execute_reply": "2021-09-13T19:57:00.543776Z"
},
"papermill": {
"duration": 0.060937,
"end_time": "2021-09-13T19:57:00.543946",
"exception": false,
"start_time": "2021-09-13T19:57:00.483009",
"status": "completed"
},
"tags": [],
"id": "4689b965",
"outputId": "0001f964-d562-49af-b8ea-2f1234f9f0aa",
"colab": {
"base_uri": "https://localhost:8080/"
}
},
"source": [
"planets"
],
"id": "4689b965",
"execution_count": 9,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune']"
]
},
"metadata": {},
"execution_count": 9
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "3zsSJVNdyhTg"
},
"source": [
"planets.append('Neptune')"
],
"id": "3zsSJVNdyhTg",
"execution_count": 8,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"papermill": {
"duration": 0.053717,
"end_time": "2021-09-13T19:57:00.651342",
"exception": false,
"start_time": "2021-09-13T19:57:00.597625",
"status": "completed"
},
"tags": [],
"id": "3e34e130"
},
"source": [
"### Searching lists\n",
"\n",
"Where does Earth fall in the order of planets? We can get its index using the `list.index` method."
],
"id": "3e34e130"
},
{
"cell_type": "code",
"metadata": {
"execution": {
"iopub.execute_input": "2021-09-13T19:57:00.761361Z",
"iopub.status.busy": "2021-09-13T19:57:00.760768Z",
"iopub.status.idle": "2021-09-13T19:57:00.765082Z",
"shell.execute_reply": "2021-09-13T19:57:00.765574Z"
},
"papermill": {
"duration": 0.060899,
"end_time": "2021-09-13T19:57:00.765747",
"exception": false,
"start_time": "2021-09-13T19:57:00.704848",
"status": "completed"
},
"tags": [],
"id": "f4b9c8bf",
"outputId": "3ba5a67d-e9c1-41cb-d89f-e8f32a27bc27"
},
"source": [
"planets.index('Earth')"
],
"id": "f4b9c8bf",
"execution_count": null,
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"papermill": {
"duration": 0.054054,
"end_time": "2021-09-13T19:57:00.874728",
"exception": false,
"start_time": "2021-09-13T19:57:00.820674",
"status": "completed"
},
"tags": [],
"id": "af799a03"
},
"source": [
"It comes third (i.e. at index 2 - 0 indexing!).\n",
"\n",
"At what index does Pluto occur?"
],
"id": "af799a03"
},
{
"cell_type": "code",
"metadata": {
"execution": {
"iopub.execute_input": "2021-09-13T19:57:00.987881Z",
"iopub.status.busy": "2021-09-13T19:57:00.987265Z",
"iopub.status.idle": "2021-09-13T19:57:01.051437Z",
"shell.execute_reply": "2021-09-13T19:57:01.050868Z"
},
"papermill": {
"duration": 0.122211,
"end_time": "2021-09-13T19:57:01.051585",
"exception": false,
"start_time": "2021-09-13T19:57:00.929374",
"status": "completed"
},
"tags": [
"raises-exception"
],
"id": "12568c4f",
"outputId": "294309e9-2804-4206-cabf-fa88d4d52f95"
},
"source": [
"planets.index('Pluto')"
],
"id": "12568c4f",
"execution_count": null,
"outputs": [
{
"ename": "ValueError",
"evalue": "'Pluto' is not in list",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m/tmp/ipykernel_20/2263615293.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mplanets\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Pluto'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mValueError\u001b[0m: 'Pluto' is not in list"
]
}
]
},
{
"cell_type": "markdown",
"metadata": {
"papermill": {
"duration": 0.054313,
"end_time": "2021-09-13T19:57:01.160706",
"exception": false,
"start_time": "2021-09-13T19:57:01.106393",
"status": "completed"
},
"tags": [],
"id": "c36a416c"
},
"source": [
"Oh, that's right...\n",
"\n",
"To avoid unpleasant surprises like this, we can use the `in` operator to determine whether a list contains a particular value:"
],
"id": "c36a416c"
},
{
"cell_type": "code",
"metadata": {
"execution": {
"iopub.execute_input": "2021-09-13T19:57:01.275719Z",
"iopub.status.busy": "2021-09-13T19:57:01.275053Z",
"iopub.status.idle": "2021-09-13T19:57:01.278627Z",
"shell.execute_reply": "2021-09-13T19:57:01.278080Z"
},
"papermill": {
"duration": 0.063117,
"end_time": "2021-09-13T19:57:01.278763",
"exception": false,
"start_time": "2021-09-13T19:57:01.215646",
"status": "completed"
},
"tags": [],
"id": "e4f30720",
"outputId": "b2381d1b-b3d8-45b4-adb6-7e85417bd8f0"
},
"source": [
"# Is Earth a planet?\n",
"\"Earth\" in planets"
],
"id": "e4f30720",
"execution_count": null,
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"cell_type": "code",
"metadata": {
"execution": {
"iopub.execute_input": "2021-09-13T19:57:01.393591Z",
"iopub.status.busy": "2021-09-13T19:57:01.392660Z",
"iopub.status.idle": "2021-09-13T19:57:01.396968Z",
"shell.execute_reply": "2021-09-13T19:57:01.397490Z"
},
"papermill": {
"duration": 0.063815,
"end_time": "2021-09-13T19:57:01.397651",
"exception": false,
"start_time": "2021-09-13T19:57:01.333836",
"status": "completed"
},
"tags": [],
"id": "927aa0cb",
"outputId": "ff720818-714a-465a-c3a9-05456d112eef"
},
"source": [
"# Is Calbefraques a planet?\n",
"\"Calbefraques\" in planets"
],
"id": "927aa0cb",
"execution_count": null,
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"papermill": {
"duration": 0.055218,
"end_time": "2021-09-13T19:57:01.509004",
"exception": false,
"start_time": "2021-09-13T19:57:01.453786",
"status": "completed"
},
"tags": [],
"id": "6f6a57a4"
},
"source": [
"There are a few more interesting list methods we haven't covered. If you want to learn about all the methods and attributes attached to a particular object, we can call `help()` on the object itself. For example, `help(planets)` will tell us about *all* the list methods: "
],
"id": "6f6a57a4"
},
{
"cell_type": "code",
"metadata": {
"_kg_hide-output": true,
"execution": {
"iopub.execute_input": "2021-09-13T19:57:01.623822Z",
"iopub.status.busy": "2021-09-13T19:57:01.623203Z",
"iopub.status.idle": "2021-09-13T19:57:01.640891Z",
"shell.execute_reply": "2021-09-13T19:57:01.641399Z"
},
"papermill": {
"duration": 0.076609,
"end_time": "2021-09-13T19:57:01.641568",
"exception": false,
"start_time": "2021-09-13T19:57:01.564959",
"status": "completed"
},
"tags": [],
"id": "a97b77c3"
},
"source": [
"help(planets)"
],
"id": "a97b77c3",
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"papermill": {
"duration": 0.055862,
"end_time": "2021-09-13T19:57:01.753429",
"exception": false,
"start_time": "2021-09-13T19:57:01.697567",
"status": "completed"
},
"tags": [],
"id": "1458a239"
},
"source": [
"Click the \"output\" button to see the full help page. Lists have lots of methods with weird-looking names like `__eq__` and `__iadd__`. Don't worry too much about these for now. (You'll probably never call such methods directly. But they get called behind the scenes when we use syntax like indexing or comparison operators.) The most interesting methods are toward the bottom of the list (`append`, `clear`, `copy`, etc.)."
],
"id": "1458a239"
},
{
"cell_type": "markdown",
"metadata": {
"papermill": {
"duration": 0.056487,
"end_time": "2021-09-13T19:57:01.865655",
"exception": false,
"start_time": "2021-09-13T19:57:01.809168",
"status": "completed"
},
"tags": [],
"id": "1ebc40dc"
},
"source": [
"## Tuples\n",
"\n",
"Tuples are almost exactly the same as lists. They differ in just two ways.\n",
"\n",
"**1:** The syntax for creating them uses parentheses instead of square brackets"
],
"id": "1ebc40dc"
},
{
"cell_type": "code",
"metadata": {
"execution": {
"iopub.execute_input": "2021-09-13T19:57:01.981082Z",
"iopub.status.busy": "2021-09-13T19:57:01.980424Z",
"iopub.status.idle": "2021-09-13T19:57:01.983231Z",
"shell.execute_reply": "2021-09-13T19:57:01.983662Z"
},
"papermill": {
"duration": 0.061811,
"end_time": "2021-09-13T19:57:01.983824",
"exception": false,
"start_time": "2021-09-13T19:57:01.922013",
"status": "completed"
},
"tags": [],
"id": "c86bb04d"
},
"source": [
"t = (1, 2, 3)"
],
"id": "c86bb04d",
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"execution": {
"iopub.execute_input": "2021-09-13T19:57:02.100218Z",
"iopub.status.busy": "2021-09-13T19:57:02.099596Z",
"iopub.status.idle": "2021-09-13T19:57:02.104029Z",
"shell.execute_reply": "2021-09-13T19:57:02.104512Z"
},
"papermill": {
"duration": 0.064198,
"end_time": "2021-09-13T19:57:02.104692",
"exception": false,
"start_time": "2021-09-13T19:57:02.040494",
"status": "completed"
},
"tags": [],
"id": "67e379b6",
"outputId": "9fd01c6a-ed94-4243-dada-ec7551de93f1"
},
"source": [
"t = 1, 2, 3 # equivalent to above\n",
"t"
],
"id": "67e379b6",
"execution_count": null,
"outputs": [
{
"data": {
"text/plain": [
"(1, 2, 3)"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"papermill": {
"duration": 0.056419,
"end_time": "2021-09-13T19:57:02.217806",
"exception": false,
"start_time": "2021-09-13T19:57:02.161387",
"status": "completed"
},
"tags": [],
"id": "c3be469d"
},
"source": [
"**2:** They cannot be modified (they are *immutable*)."
],
"id": "c3be469d"
},
{
"cell_type": "code",
"metadata": {
"execution": {
"iopub.execute_input": "2021-09-13T19:57:02.345641Z",
"iopub.status.busy": "2021-09-13T19:57:02.335962Z",
"iopub.status.idle": "2021-09-13T19:57:02.348951Z",
"shell.execute_reply": "2021-09-13T19:57:02.349514Z"
},
"papermill": {
"duration": 0.074863,
"end_time": "2021-09-13T19:57:02.349696",
"exception": false,
"start_time": "2021-09-13T19:57:02.274833",
"status": "completed"
},
"tags": [
"raises-exception"
],
"id": "add44c0a",
"outputId": "090418d5-de50-4706-af8f-87be9fa03ec6"
},
"source": [
"t[0] = 100"
],
"id": "add44c0a",
"execution_count": null,
"outputs": [
{
"ename": "TypeError",
"evalue": "'tuple' object does not support item assignment",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m/tmp/ipykernel_20/816329950.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mt\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m100\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
]
}
]
},
{
"cell_type": "markdown",
"metadata": {
"papermill": {
"duration": 0.057336,
"end_time": "2021-09-13T19:57:02.464445",
"exception": false,
"start_time": "2021-09-13T19:57:02.407109",
"status": "completed"
},
"tags": [],
"id": "a7392131"
},
"source": [
"Tuples are often used for functions that have multiple return values.\n",
"\n",
"For example, the ``as_integer_ratio()`` method of float objects returns a numerator and a denominator in the form of a tuple:"
],
"id": "a7392131"
},
{
"cell_type": "code",
"metadata": {
"execution": {
"iopub.execute_input": "2021-09-13T19:57:02.582559Z",
"iopub.status.busy": "2021-09-13T19:57:02.581613Z",
"iopub.status.idle": "2021-09-13T19:57:02.586774Z",
"shell.execute_reply": "2021-09-13T19:57:02.587354Z"
},
"papermill": {
"duration": 0.065784,
"end_time": "2021-09-13T19:57:02.587511",
"exception": false,
"start_time": "2021-09-13T19:57:02.521727",
"status": "completed"
},
"tags": [],
"id": "1fad55f0",
"outputId": "47f5629f-ce6d-4a2e-aaad-0efb334a5e1d"
},
"source": [
"x = 0.125\n",
"x.as_integer_ratio()"
],
"id": "1fad55f0",
"execution_count": null,
"outputs": [
{
"data": {
"text/plain": [
"(1, 8)"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"papermill": {
"duration": 0.057051,
"end_time": "2021-09-13T19:57:02.701132",
"exception": false,
"start_time": "2021-09-13T19:57:02.644081",
"status": "completed"
},
"tags": [],
"id": "ab4d9f27"
},
"source": [
"These multiple return values can be individually assigned as follows:"
],
"id": "ab4d9f27"
},
{
"cell_type": "code",
"metadata": {
"execution": {
"iopub.execute_input": "2021-09-13T19:57:02.820124Z",
"iopub.status.busy": "2021-09-13T19:57:02.819253Z",
"iopub.status.idle": "2021-09-13T19:57:02.824373Z",
"shell.execute_reply": "2021-09-13T19:57:02.824839Z"
},
"papermill": {
"duration": 0.066432,
"end_time": "2021-09-13T19:57:02.824998",
"exception": false,
"start_time": "2021-09-13T19:57:02.758566",
"status": "completed"
},
"tags": [],
"id": "e81dcaf7",
"outputId": "c2bf29dc-81df-4315-8f38-6d5875e8c668"
},
"source": [
"numerator, denominator = x.as_integer_ratio()\n",
"print(numerator / denominator)"
],
"id": "e81dcaf7",
"execution_count": null,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.125\n"
]
}
]
},
{
"cell_type": "markdown",
"metadata": {
"papermill": {
"duration": 0.057011,
"end_time": "2021-09-13T19:57:02.940010",
"exception": false,
"start_time": "2021-09-13T19:57:02.882999",
"status": "completed"
},
"tags": [],
"id": "96eaec9e"
},
"source": [
"Finally we have some insight into the classic Stupid Python Trick™ for swapping two variables!"
],
"id": "96eaec9e"
},
{
"cell_type": "code",
"metadata": {
"execution": {
"iopub.execute_input": "2021-09-13T19:57:03.058500Z",
"iopub.status.busy": "2021-09-13T19:57:03.057844Z",
"iopub.status.idle": "2021-09-13T19:57:03.063358Z",
"shell.execute_reply": "2021-09-13T19:57:03.062747Z"
},
"papermill": {
"duration": 0.066153,
"end_time": "2021-09-13T19:57:03.063495",
"exception": false,
"start_time": "2021-09-13T19:57:02.997342",
"status": "completed"
},
"tags": [],
"id": "1c55a448",
"outputId": "49e128c5-02a8-486d-c4a1-b71d9edfcb02"
},
"source": [
"a = 1\n",
"b = 0\n",
"a, b = b, a\n",
"print(a, b)"
],
"id": "1c55a448",
"execution_count": null,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0 1\n"
]
}
]
},
{
"cell_type": "markdown",
"metadata": {
"papermill": {
"duration": 0.05828,
"end_time": "2021-09-13T19:57:03.180607",
"exception": false,
"start_time": "2021-09-13T19:57:03.122327",
"status": "completed"
},
"tags": [],
"id": "9075c08a"
},
"source": [
"# Your Turn\n",
"\n",
"You learn best by writing code, not just reading it. So try **[the coding challenge](https://www.kaggle.com/kernels/fork/1275173)** now."
],
"id": "9075c08a"
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment