From d7b485b120fd4b09fb130eb887ab86c2181e7866 Mon Sep 17 00:00:00 2001 From: awells-nrao Date: Mon, 3 Jun 2024 19:03:11 -0400 Subject: [PATCH 1/7] Created using Colab --- Lesson_1_Python_basics.ipynb | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/Lesson_1_Python_basics.ipynb b/Lesson_1_Python_basics.ipynb index f0d7a03..f6099a8 100644 --- a/Lesson_1_Python_basics.ipynb +++ b/Lesson_1_Python_basics.ipynb @@ -7,7 +7,7 @@ "colab_type": "text" }, "source": [ - "\"Open" + "\"Open" ] }, { @@ -30,6 +30,30 @@ "All along the lessons you will see comments on the code starting with `#`, these are comment lines and are used to describe what the code is doing" ] }, + { + "cell_type": "code", + "source": [ + "print(\"Test1\")" + ], + "metadata": { + "id": "puGBUv0gVvGQ", + "outputId": "8a52b530-a040-4961-d76f-ffd2650f8e4a", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "id": "puGBUv0gVvGQ", + "execution_count": 2, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Test1\n" + ] + } + ] + }, { "cell_type": "markdown", "id": "d927a42b", @@ -3695,7 +3719,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "id": "311dbb98", "metadata": { "id": "311dbb98", From b428f5229ae22d25b3cb1fc943a3bfc2a5ff8a56 Mon Sep 17 00:00:00 2001 From: awells-nrao Date: Mon, 3 Jun 2024 19:11:03 -0400 Subject: [PATCH 2/7] Created using Colab --- Lesson_3b_Pandas_Key_Concepts.ipynb | 7141 +++++++++++++++++++++++++++ 1 file changed, 7141 insertions(+) create mode 100644 Lesson_3b_Pandas_Key_Concepts.ipynb diff --git a/Lesson_3b_Pandas_Key_Concepts.ipynb b/Lesson_3b_Pandas_Key_Concepts.ipynb new file mode 100644 index 0000000..3090746 --- /dev/null +++ b/Lesson_3b_Pandas_Key_Concepts.ipynb @@ -0,0 +1,7141 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "\"Open" + ] + }, + { + "cell_type": "markdown", + "id": "47c6dfd4", + "metadata": { + "id": "47c6dfd4" + }, + "source": [ + "## Pandas deep dive\n", + "Lets review some of the key concepts for Pandas and Numpy that we will use during this course. First lets import our libraries, Pandas." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0b4543db", + "metadata": { + "id": "0b4543db" + }, + "outputs": [], + "source": [ + "import pandas as pd" + ] + }, + { + "cell_type": "markdown", + "id": "8ee7440a", + "metadata": { + "id": "8ee7440a" + }, + "source": [ + "A Pandas Series is a unidimentional matrix of indexed data. We can create one from a list, like this example" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7e125e9e", + "metadata": { + "id": "7e125e9e", + "outputId": "3d3ddff8-0e4f-4b81-ae33-ef506775e77d" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "0 0.25\n", + "1 0.50\n", + "2 0.75\n", + "3 1.00\n", + "dtype: float64" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data = pd.Series([0.25, 0.5, 0.75, 1.0])\n", + "data" + ] + }, + { + "cell_type": "markdown", + "id": "11f897a5", + "metadata": { + "id": "11f897a5" + }, + "source": [ + "We can see in our output that the Series is wrpaped by a sequence of values and a sequence of indexes. Values are simply a Numpy matrix" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5131e265", + "metadata": { + "id": "5131e265", + "outputId": "347da66d-7621-424d-df45-6a2a3029e0d7" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "array([0.25, 0.5 , 0.75, 1. ])" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data.values" + ] + }, + { + "cell_type": "markdown", + "id": "54601234", + "metadata": { + "id": "54601234" + }, + "source": [ + "And the index is a matrix of type pd.Index" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "83945325", + "metadata": { + "id": "83945325", + "outputId": "dfd6a08c-edcb-4afe-de69-2e7a67d1b994" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "RangeIndex(start=0, stop=4, step=1)" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data.index" + ] + }, + { + "cell_type": "markdown", + "id": "2d779865", + "metadata": { + "id": "2d779865" + }, + "source": [ + "We can access data through the associated index" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "194981dd", + "metadata": { + "id": "194981dd", + "outputId": "05e4ec73-dda8-4653-e750-fd6117c7be2d" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "0.5" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data[1]" + ] + }, + { + "cell_type": "markdown", + "id": "a715938e", + "metadata": { + "id": "a715938e" + }, + "source": [ + "And do data slicing" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "dd886170", + "metadata": { + "id": "dd886170", + "outputId": "c99c0186-0c4d-401c-d579-d6fc0b5e0133" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "1 0.50\n", + "2 0.75\n", + "dtype: float64" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data[1:3]" + ] + }, + { + "cell_type": "markdown", + "id": "90937a53", + "metadata": { + "id": "90937a53" + }, + "source": [ + "Now, the main difference between Numpy indices and the Series object is that the Series index can be something other than an integer, we can use strings for our index." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c475dafb", + "metadata": { + "id": "c475dafb", + "outputId": "ad3ba3fc-9a36-40f5-a018-f212043e3871" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "a 0.25\n", + "b 0.50\n", + "c 0.75\n", + "d 1.00\n", + "dtype: float64" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data = pd.Series([0.25, 0.5, 0.75, 1.0],\n", + " index=['a', 'b', 'c', 'd'])\n", + "data" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "bbd4d126", + "metadata": { + "id": "bbd4d126", + "outputId": "e62e5006-02cd-45a5-e96e-b22fd46ec8eb" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "0.5" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data['b']" + ] + }, + { + "cell_type": "markdown", + "id": "d5566396", + "metadata": { + "id": "d5566396" + }, + "source": [ + "We can think of the Series as a specialized dictionary. We can even create a Pandas Series object from a Python dictionary" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "64d4682d", + "metadata": { + "id": "64d4682d", + "outputId": "01847695-a7b6-4300-f1c1-732502b9f785" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Sun 1.989 × 10^30 kg\n", + "Mercury 3.285 × 10^23 kg\n", + "Venus 4.867 × 10^24 kg\n", + "Earth 5.972 × 10^24 kg\n", + "Mars 6.39 × 10^23 kg\n", + "dtype: object" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mass_dict = {'Sun': \"1.989 × 10^30 kg\",\n", + " 'Mercury': \"3.285 × 10^23 kg\",\n", + " 'Venus': \"4.867 × 10^24 kg\",\n", + " 'Earth': \"5.972 × 10^24 kg\",\n", + " 'Mars': \"6.39 × 10^23 kg\"}\n", + "mass = pd.Series(mass_dict)\n", + "mass" + ] + }, + { + "cell_type": "markdown", + "id": "85ff84fe", + "metadata": { + "id": "85ff84fe" + }, + "source": [ + "We can slice our Series" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "dce8f49e", + "metadata": { + "id": "dce8f49e", + "outputId": "8fcbcaad-958c-420d-8d47-94a563141328" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Mercury 3.285 × 10^23 kg\n", + "Venus 4.867 × 10^24 kg\n", + "Earth 5.972 × 10^24 kg\n", + "dtype: object" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mass['Mercury':'Earth']" + ] + }, + { + "cell_type": "markdown", + "id": "0734efd1", + "metadata": { + "id": "0734efd1" + }, + "source": [ + "* The pandas Dataframe*\n", + "The next key object in Pandas is the DataFrame. This object can be considered a generalization of a matrix.\n", + "\n", + "This object can be thinked of an ordered sequence of columns, sharing a row index. Lets create a new Series and then use this Series to create a Dataframe" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "57ccff30", + "metadata": { + "id": "57ccff30", + "outputId": "a8c2785a-4752-46f4-84f7-fedf8dbbc1c7" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Sun 274 m/s²\n", + "Mercury 3.7 m/s²\n", + "Venus 8.87 m/s²\n", + "Earth 9.807 m/s²\n", + "Mars 3.721 m/s²\n", + "dtype: object" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "grav_dict = {'Sun': \"274 m/s²\", 'Mercury': \"3.7 m/s²\", 'Venus': \"8.87 m/s²\",\n", + " 'Earth': \"9.807 m/s²\", 'Mars': \"3.721 m/s²\"}\n", + "grav = pd.Series(grav_dict)\n", + "grav" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ab2e430c", + "metadata": { + "id": "ab2e430c", + "outputId": "b4e46dfa-d533-4d44-9675-99c03b9d70b1" + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
massgrav
Sun1.989 × 10^30 kg274 m/s²
Mercury3.285 × 10^23 kg3.7 m/s²
Venus4.867 × 10^24 kg8.87 m/s²
Earth5.972 × 10^24 kg9.807 m/s²
Mars6.39 × 10^23 kg3.721 m/s²
\n", + "
" + ], + "text/plain": [ + " mass grav\n", + "Sun 1.989 × 10^30 kg 274 m/s²\n", + "Mercury 3.285 × 10^23 kg 3.7 m/s²\n", + "Venus 4.867 × 10^24 kg 8.87 m/s²\n", + "Earth 5.972 × 10^24 kg 9.807 m/s²\n", + "Mars 6.39 × 10^23 kg 3.721 m/s²" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# lets use our mass dictionary from previous explanation\n", + "# Create a single Dataframe from both\n", + "objects = pd.DataFrame({'mass': mass,\n", + " 'grav': grav})\n", + "objects" + ] + }, + { + "cell_type": "markdown", + "id": "c254a1bb", + "metadata": { + "id": "c254a1bb" + }, + "source": [ + "We can acces each object with its index" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b1795baa", + "metadata": { + "id": "b1795baa", + "outputId": "d05fe0ff-8187-49a5-8293-64f7b7f2a8e1" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Index(['Sun', 'Mercury', 'Venus', 'Earth', 'Mars'], dtype='object')" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "objects.index" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d0d6c055", + "metadata": { + "id": "d0d6c055", + "outputId": "9b8569b3-314e-45a9-d83b-1b646476635f" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Index(['mass', 'grav'], dtype='object')" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# read the Dataframe columns\n", + "objects.columns" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4307a681", + "metadata": { + "id": "4307a681", + "outputId": "2a60fe87-0189-474c-e9cc-5cbd3f9490ef" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Sun 274 m/s²\n", + "Mercury 3.7 m/s²\n", + "Venus 8.87 m/s²\n", + "Earth 9.807 m/s²\n", + "Mars 3.721 m/s²\n", + "Name: grav, dtype: object" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Access one of the columns, similar to a dictionary\n", + "objects['grav']" + ] + }, + { + "cell_type": "markdown", + "id": "069f2198", + "metadata": { + "id": "069f2198" + }, + "source": [ + "Please notice that we are calling the Dataframe *column*" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "77b70c23", + "metadata": { + "id": "77b70c23", + "outputId": "6b02facb-f99a-4b1f-b7e0-36b19078c717" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Sun 1.989 × 10^30 kg\n", + "Mercury 3.285 × 10^23 kg\n", + "Venus 4.867 × 10^24 kg\n", + "Earth 5.972 × 10^24 kg\n", + "Mars 6.39 × 10^23 kg\n", + "Name: mass, dtype: object" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "objects['mass']" + ] + }, + { + "cell_type": "markdown", + "id": "174c7680", + "metadata": { + "id": "174c7680" + }, + "source": [ + "You can describe a Pandas Dataframe with `.describe()`" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "bf56f643", + "metadata": { + "id": "bf56f643", + "outputId": "16a5394f-7549-48df-fd01-610d1f620b16" + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
massgrav
count55
unique55
top1.989 × 10^30 kg274 m/s²
freq11
\n", + "
" + ], + "text/plain": [ + " mass grav\n", + "count 5 5\n", + "unique 5 5\n", + "top 1.989 × 10^30 kg 274 m/s²\n", + "freq 1 1" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "objects.describe()" + ] + }, + { + "cell_type": "markdown", + "id": "8c8654a9", + "metadata": { + "id": "8c8654a9" + }, + "source": [ + "### Optional Exercise\n", + "- Create a new dataframe from the following data\n", + "`data = {'ages': [23, 24, 25, 26, 27, 28], 'weight': [120, 130, 150, 160, 180, 190]}\n", + "- You should now have two data columns `ages` and `weight`. Print only the `ages` column.\n", + "- Select the row with age = 27 using `.loc[]`" + ] + }, + { + "cell_type": "markdown", + "id": "7036e515", + "metadata": { + "id": "7036e515" + }, + "source": [ + "Lets open our cereal csv and check some Pandas functions.\n", + "You can use `head()` to show the first 5 rows.\n", + "You can use `tail()` to show the last 5 rows." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "26224393", + "metadata": { + "id": "26224393", + "outputId": "017f72e9-a638-4610-b2b3-04151ea00c35" + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
mfrtypecaloriesproteinfatsodiumfibercarbosugarspotassvitaminsshelfweightcupsrating
name
100% BranNC704113010.05.062802531.00.3368.402973
100% Natural BranQC12035152.08.08135031.01.0033.983679
All-BranKC70412609.07.053202531.00.3359.425505
All-Bran with Extra FiberKC504014014.08.003302531.00.5093.704912
Almond DelightRC110222001.014.08-12531.00.7534.384843
\n", + "
" + ], + "text/plain": [ + " mfr type calories protein fat sodium fiber \\\n", + "name \n", + "100% Bran N C 70 4 1 130 10.0 \n", + "100% Natural Bran Q C 120 3 5 15 2.0 \n", + "All-Bran K C 70 4 1 260 9.0 \n", + "All-Bran with Extra Fiber K C 50 4 0 140 14.0 \n", + "Almond Delight R C 110 2 2 200 1.0 \n", + "\n", + " carbo sugars potass vitamins shelf weight \\\n", + "name \n", + "100% Bran 5.0 6 280 25 3 1.0 \n", + "100% Natural Bran 8.0 8 135 0 3 1.0 \n", + "All-Bran 7.0 5 320 25 3 1.0 \n", + "All-Bran with Extra Fiber 8.0 0 330 25 3 1.0 \n", + "Almond Delight 14.0 8 -1 25 3 1.0 \n", + "\n", + " cups rating \n", + "name \n", + "100% Bran 0.33 68.402973 \n", + "100% Natural Bran 1.00 33.983679 \n", + "All-Bran 0.33 59.425505 \n", + "All-Bran with Extra Fiber 0.50 93.704912 \n", + "Almond Delight 0.75 34.384843 " + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "cereal = pd.read_csv('cereal.csv', index_col='name')\n", + "cereal.head()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "df9b28ef", + "metadata": { + "id": "df9b28ef", + "outputId": "79832d06-35d8-4422-b3c3-9526a2c3989b" + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
mfrtypecaloriesproteinfatsodiumfibercarbosugarspotassvitaminsshelfweightcupsrating
name
TriplesGC110212500.021.03602531.00.7539.106174
TrixGC110111400.013.012252521.01.0027.753301
Wheat ChexRC100312303.017.031152511.00.6749.787445
WheatiesGC100312003.017.031102511.01.0051.592193
Wheaties Honey GoldGC110212001.016.08602511.00.7536.187559
\n", + "
" + ], + "text/plain": [ + " mfr type calories protein fat sodium fiber carbo \\\n", + "name \n", + "Triples G C 110 2 1 250 0.0 21.0 \n", + "Trix G C 110 1 1 140 0.0 13.0 \n", + "Wheat Chex R C 100 3 1 230 3.0 17.0 \n", + "Wheaties G C 100 3 1 200 3.0 17.0 \n", + "Wheaties Honey Gold G C 110 2 1 200 1.0 16.0 \n", + "\n", + " sugars potass vitamins shelf weight cups rating \n", + "name \n", + "Triples 3 60 25 3 1.0 0.75 39.106174 \n", + "Trix 12 25 25 2 1.0 1.00 27.753301 \n", + "Wheat Chex 3 115 25 1 1.0 0.67 49.787445 \n", + "Wheaties 3 110 25 1 1.0 1.00 51.592193 \n", + "Wheaties Honey Gold 8 60 25 1 1.0 0.75 36.187559 " + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "cereal.tail()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "750b4402", + "metadata": { + "id": "750b4402", + "outputId": "83d9485a-fb87-40e8-ffa7-04de11e354ee" + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
caloriesproteinfatsodiumfibercarbosugarspotassvitaminsshelfweightcupsrating
count77.00000077.00000077.00000077.00000077.00000077.00000077.00000077.00000077.00000077.00000077.00000077.00000077.000000
mean106.8831172.5454551.012987159.6753252.15194814.5974036.92207896.07792228.2467532.2077921.0296100.82103942.665705
std19.4841191.0947901.00647383.8322952.3833644.2789564.44488571.28681322.3425230.8325240.1504770.23271614.047289
min50.0000001.0000000.0000000.0000000.000000-1.000000-1.000000-1.0000000.0000001.0000000.5000000.25000018.042851
25%100.0000002.0000000.000000130.0000001.00000012.0000003.00000040.00000025.0000001.0000001.0000000.67000033.174094
50%110.0000003.0000001.000000180.0000002.00000014.0000007.00000090.00000025.0000002.0000001.0000000.75000040.400208
75%110.0000003.0000002.000000210.0000003.00000017.00000011.000000120.00000025.0000003.0000001.0000001.00000050.828392
max160.0000006.0000005.000000320.00000014.00000023.00000015.000000330.000000100.0000003.0000001.5000001.50000093.704912
\n", + "
" + ], + "text/plain": [ + " calories protein fat sodium fiber carbo \\\n", + "count 77.000000 77.000000 77.000000 77.000000 77.000000 77.000000 \n", + "mean 106.883117 2.545455 1.012987 159.675325 2.151948 14.597403 \n", + "std 19.484119 1.094790 1.006473 83.832295 2.383364 4.278956 \n", + "min 50.000000 1.000000 0.000000 0.000000 0.000000 -1.000000 \n", + "25% 100.000000 2.000000 0.000000 130.000000 1.000000 12.000000 \n", + "50% 110.000000 3.000000 1.000000 180.000000 2.000000 14.000000 \n", + "75% 110.000000 3.000000 2.000000 210.000000 3.000000 17.000000 \n", + "max 160.000000 6.000000 5.000000 320.000000 14.000000 23.000000 \n", + "\n", + " sugars potass vitamins shelf weight cups \\\n", + "count 77.000000 77.000000 77.000000 77.000000 77.000000 77.000000 \n", + "mean 6.922078 96.077922 28.246753 2.207792 1.029610 0.821039 \n", + "std 4.444885 71.286813 22.342523 0.832524 0.150477 0.232716 \n", + "min -1.000000 -1.000000 0.000000 1.000000 0.500000 0.250000 \n", + "25% 3.000000 40.000000 25.000000 1.000000 1.000000 0.670000 \n", + "50% 7.000000 90.000000 25.000000 2.000000 1.000000 0.750000 \n", + "75% 11.000000 120.000000 25.000000 3.000000 1.000000 1.000000 \n", + "max 15.000000 330.000000 100.000000 3.000000 1.500000 1.500000 \n", + "\n", + " rating \n", + "count 77.000000 \n", + "mean 42.665705 \n", + "std 14.047289 \n", + "min 18.042851 \n", + "25% 33.174094 \n", + "50% 40.400208 \n", + "75% 50.828392 \n", + "max 93.704912 " + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "cereal.describe()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "06fa90ec", + "metadata": { + "id": "06fa90ec", + "outputId": "7cfef184-bb94-40de-89ef-d96e15fcef38" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Index(['mfr', 'type', 'calories', 'protein', 'fat', 'sodium', 'fiber', 'carbo',\n", + " 'sugars', 'potass', 'vitamins', 'shelf', 'weight', 'cups', 'rating'],\n", + " dtype='object')" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "cereal.columns" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "740a8cbd", + "metadata": { + "id": "740a8cbd", + "outputId": "2270e9ab-a25c-4d5b-becb-d4115057efa1" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "array(['N', 'Q', 'K', 'R', 'G', 'P', 'A'], dtype=object)" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Returns an array of the unique manufacturers\n", + "cereal.mfr.unique()" + ] + }, + { + "cell_type": "markdown", + "id": "eefc4ac1", + "metadata": { + "id": "eefc4ac1" + }, + "source": [ + "You can get info about the dataset by using the `info()` function, giving you the number of rows and columns, rows with non null values, type of data in each column and memory use." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5d74d1e0", + "metadata": { + "id": "5d74d1e0", + "outputId": "68207070-1c47-451d-926c-23b763623d3b" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Index: 77 entries, 100% Bran to Wheaties Honey Gold\n", + "Data columns (total 15 columns):\n", + " # Column Non-Null Count Dtype \n", + "--- ------ -------------- ----- \n", + " 0 mfr 77 non-null object \n", + " 1 type 77 non-null object \n", + " 2 calories 77 non-null int64 \n", + " 3 protein 77 non-null int64 \n", + " 4 fat 77 non-null int64 \n", + " 5 sodium 77 non-null int64 \n", + " 6 fiber 77 non-null float64\n", + " 7 carbo 77 non-null float64\n", + " 8 sugars 77 non-null int64 \n", + " 9 potass 77 non-null int64 \n", + " 10 vitamins 77 non-null int64 \n", + " 11 shelf 77 non-null int64 \n", + " 12 weight 77 non-null float64\n", + " 13 cups 77 non-null float64\n", + " 14 rating 77 non-null float64\n", + "dtypes: float64(5), int64(8), object(2)\n", + "memory usage: 9.6+ KB\n" + ] + } + ], + "source": [ + "cereal.info()" + ] + }, + { + "cell_type": "markdown", + "id": "535968df", + "metadata": { + "id": "535968df" + }, + "source": [ + "You can also output the shape of the dataframe with `shape`" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "57b6c7eb", + "metadata": { + "id": "57b6c7eb", + "outputId": "fedbb9bf-1c32-4449-f1cc-cd2d70b0be56" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "(77, 15)" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#Should output 77 rows and 15 columns\n", + "cereal.shape" + ] + }, + { + "cell_type": "markdown", + "id": "b421a658", + "metadata": { + "id": "b421a658" + }, + "source": [ + "You can use `.loc` to access specific data.\n", + "Lets return the cereals which have a protein content higher that 4.0" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0fab8877", + "metadata": { + "id": "0fab8877", + "outputId": "8d10c014-1ad7-46a8-b336-39edbca595d8" + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
mfrtypecaloriesproteinfatsodiumfibercarbosugarspotassvitaminsshelfweightcupsrating
name
100% BranNC704113010.05.062802531.00.3368.402973
All-BranKC70412609.07.053202531.00.3359.425505
All-Bran with Extra FiberKC504014014.08.003302531.00.5093.704912
CheeriosGC110622902.017.011052511.01.2550.764999
LifeQC100421502.012.06952521.00.6745.328074
MaypoAH1004100.016.03952521.01.0054.850917
Muesli Raisins; Dates; & AlmondsRC15043953.016.0111702531.01.0037.136863
Muesli Raisins; Peaches; & PecansRC150431503.016.0111702531.01.0034.139765
Quaker Oat SquaresQC100411352.014.061102531.00.5049.511874
Quaker OatmealQH1005202.7-1.0-1110011.00.6750.828392
Special KKC110602301.016.03552511.01.0053.131324
\n", + "
" + ], + "text/plain": [ + " mfr type calories protein fat sodium \\\n", + "name \n", + "100% Bran N C 70 4 1 130 \n", + "All-Bran K C 70 4 1 260 \n", + "All-Bran with Extra Fiber K C 50 4 0 140 \n", + "Cheerios G C 110 6 2 290 \n", + "Life Q C 100 4 2 150 \n", + "Maypo A H 100 4 1 0 \n", + "Muesli Raisins; Dates; & Almonds R C 150 4 3 95 \n", + "Muesli Raisins; Peaches; & Pecans R C 150 4 3 150 \n", + "Quaker Oat Squares Q C 100 4 1 135 \n", + "Quaker Oatmeal Q H 100 5 2 0 \n", + "Special K K C 110 6 0 230 \n", + "\n", + " fiber carbo sugars potass vitamins \\\n", + "name \n", + "100% Bran 10.0 5.0 6 280 25 \n", + "All-Bran 9.0 7.0 5 320 25 \n", + "All-Bran with Extra Fiber 14.0 8.0 0 330 25 \n", + "Cheerios 2.0 17.0 1 105 25 \n", + "Life 2.0 12.0 6 95 25 \n", + "Maypo 0.0 16.0 3 95 25 \n", + "Muesli Raisins; Dates; & Almonds 3.0 16.0 11 170 25 \n", + "Muesli Raisins; Peaches; & Pecans 3.0 16.0 11 170 25 \n", + "Quaker Oat Squares 2.0 14.0 6 110 25 \n", + "Quaker Oatmeal 2.7 -1.0 -1 110 0 \n", + "Special K 1.0 16.0 3 55 25 \n", + "\n", + " shelf weight cups rating \n", + "name \n", + "100% Bran 3 1.0 0.33 68.402973 \n", + "All-Bran 3 1.0 0.33 59.425505 \n", + "All-Bran with Extra Fiber 3 1.0 0.50 93.704912 \n", + "Cheerios 1 1.0 1.25 50.764999 \n", + "Life 2 1.0 0.67 45.328074 \n", + "Maypo 2 1.0 1.00 54.850917 \n", + "Muesli Raisins; Dates; & Almonds 3 1.0 1.00 37.136863 \n", + "Muesli Raisins; Peaches; & Pecans 3 1.0 1.00 34.139765 \n", + "Quaker Oat Squares 3 1.0 0.50 49.511874 \n", + "Quaker Oatmeal 1 1.0 0.67 50.828392 \n", + "Special K 1 1.0 1.00 53.131324 " + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "cereal.loc[cereal['protein'] >= 4.0]" + ] + }, + { + "cell_type": "markdown", + "id": "e1f775dc", + "metadata": { + "id": "e1f775dc" + }, + "source": [ + "Return cereals with protein higher than 2 grams and sugar lower than 6 grams" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "99aa024a", + "metadata": { + "id": "99aa024a", + "outputId": "d412b553-ed5f-4b79-c939-5578975b2834" + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
mfrtypecaloriesproteinfatsodiumfibercarbosugarspotassvitaminsshelfweightcupsrating
name
100% BranNC704113010.05.062802531.000.3368.402973
All-BranKC70412609.07.053202531.000.3359.425505
All-Bran with Extra FiberKC504014014.08.003302531.000.5093.704912
Bran ChexRC90212004.015.061252511.000.6749.120253
Bran FlakesPC90302105.013.051902531.000.6753.313813
CheeriosGC110622902.017.011052511.001.2550.764999
Corn ChexRC110202800.022.03252511.001.0041.445019
Corn FlakesKC100202901.021.02352511.001.0045.863324
Cream of Wheat (Quick)NH10030801.021.00-1021.001.0064.533816
CrispixKC110202201.021.03302531.001.0046.895644
Double ChexRC100201901.018.05802531.000.7544.330856
Grape Nuts FlakesPC100311403.015.05852531.000.8852.076897
Grape-NutsPC110301703.017.03902531.000.2553.371007
Great Grains PecanPC12033753.013.041002531.000.3345.811716
Just Right Crunchy NuggetsKC110211701.017.066010031.001.0036.523683
KixGC110212600.021.03402521.001.5039.241114
LifeQC100421502.012.06952521.000.6745.328074
MaypoAH1004100.016.03952521.001.0054.850917
Multi-Grain CheeriosGC100212202.015.06902511.001.0040.105965
Nutri-grain WheatKC90301703.018.02902531.001.0059.642837
Product 19KC100303201.020.034510031.001.0041.503540
Puffed WheatQC502001.010.0050030.501.0063.005645
Quaker Oat SquaresQC100411352.014.061102531.000.5049.511874
Quaker OatmealQH1005202.7-1.0-1110011.000.6750.828392
Raisin SquaresKC902002.015.061102531.000.5055.333142
Rice KrispiesKC110202900.022.03352511.001.0040.560159
Shredded WheatNC802003.016.0095010.831.0068.235885
Shredded Wheat 'n'BranNC903004.019.00140011.000.6774.472949
Shredded Wheat spoon sizeNC903003.020.00120011.000.6772.801787
Special KKC110602301.016.03552511.001.0053.131324
Strawberry Fruit WheatsNC9020153.015.05902521.001.0059.363993
Total Corn FlakesGC110212000.021.033510031.001.0038.839746
Total Whole GrainGC100312003.016.0311010031.001.0046.658844
TriplesGC110212500.021.03602531.000.7539.106174
Wheat ChexRC100312303.017.031152511.000.6749.787445
WheatiesGC100312003.017.031102511.001.0051.592193
\n", + "
" + ], + "text/plain": [ + " mfr type calories protein fat sodium fiber \\\n", + "name \n", + "100% Bran N C 70 4 1 130 10.0 \n", + "All-Bran K C 70 4 1 260 9.0 \n", + "All-Bran with Extra Fiber K C 50 4 0 140 14.0 \n", + "Bran Chex R C 90 2 1 200 4.0 \n", + "Bran Flakes P C 90 3 0 210 5.0 \n", + "Cheerios G C 110 6 2 290 2.0 \n", + "Corn Chex R C 110 2 0 280 0.0 \n", + "Corn Flakes K C 100 2 0 290 1.0 \n", + "Cream of Wheat (Quick) N H 100 3 0 80 1.0 \n", + "Crispix K C 110 2 0 220 1.0 \n", + "Double Chex R C 100 2 0 190 1.0 \n", + "Grape Nuts Flakes P C 100 3 1 140 3.0 \n", + "Grape-Nuts P C 110 3 0 170 3.0 \n", + "Great Grains Pecan P C 120 3 3 75 3.0 \n", + "Just Right Crunchy Nuggets K C 110 2 1 170 1.0 \n", + "Kix G C 110 2 1 260 0.0 \n", + "Life Q C 100 4 2 150 2.0 \n", + "Maypo A H 100 4 1 0 0.0 \n", + "Multi-Grain Cheerios G C 100 2 1 220 2.0 \n", + "Nutri-grain Wheat K C 90 3 0 170 3.0 \n", + "Product 19 K C 100 3 0 320 1.0 \n", + "Puffed Wheat Q C 50 2 0 0 1.0 \n", + "Quaker Oat Squares Q C 100 4 1 135 2.0 \n", + "Quaker Oatmeal Q H 100 5 2 0 2.7 \n", + "Raisin Squares K C 90 2 0 0 2.0 \n", + "Rice Krispies K C 110 2 0 290 0.0 \n", + "Shredded Wheat N C 80 2 0 0 3.0 \n", + "Shredded Wheat 'n'Bran N C 90 3 0 0 4.0 \n", + "Shredded Wheat spoon size N C 90 3 0 0 3.0 \n", + "Special K K C 110 6 0 230 1.0 \n", + "Strawberry Fruit Wheats N C 90 2 0 15 3.0 \n", + "Total Corn Flakes G C 110 2 1 200 0.0 \n", + "Total Whole Grain G C 100 3 1 200 3.0 \n", + "Triples G C 110 2 1 250 0.0 \n", + "Wheat Chex R C 100 3 1 230 3.0 \n", + "Wheaties G C 100 3 1 200 3.0 \n", + "\n", + " carbo sugars potass vitamins shelf weight \\\n", + "name \n", + "100% Bran 5.0 6 280 25 3 1.00 \n", + "All-Bran 7.0 5 320 25 3 1.00 \n", + "All-Bran with Extra Fiber 8.0 0 330 25 3 1.00 \n", + "Bran Chex 15.0 6 125 25 1 1.00 \n", + "Bran Flakes 13.0 5 190 25 3 1.00 \n", + "Cheerios 17.0 1 105 25 1 1.00 \n", + "Corn Chex 22.0 3 25 25 1 1.00 \n", + "Corn Flakes 21.0 2 35 25 1 1.00 \n", + "Cream of Wheat (Quick) 21.0 0 -1 0 2 1.00 \n", + "Crispix 21.0 3 30 25 3 1.00 \n", + "Double Chex 18.0 5 80 25 3 1.00 \n", + "Grape Nuts Flakes 15.0 5 85 25 3 1.00 \n", + "Grape-Nuts 17.0 3 90 25 3 1.00 \n", + "Great Grains Pecan 13.0 4 100 25 3 1.00 \n", + "Just Right Crunchy Nuggets 17.0 6 60 100 3 1.00 \n", + "Kix 21.0 3 40 25 2 1.00 \n", + "Life 12.0 6 95 25 2 1.00 \n", + "Maypo 16.0 3 95 25 2 1.00 \n", + "Multi-Grain Cheerios 15.0 6 90 25 1 1.00 \n", + "Nutri-grain Wheat 18.0 2 90 25 3 1.00 \n", + "Product 19 20.0 3 45 100 3 1.00 \n", + "Puffed Wheat 10.0 0 50 0 3 0.50 \n", + "Quaker Oat Squares 14.0 6 110 25 3 1.00 \n", + "Quaker Oatmeal -1.0 -1 110 0 1 1.00 \n", + "Raisin Squares 15.0 6 110 25 3 1.00 \n", + "Rice Krispies 22.0 3 35 25 1 1.00 \n", + "Shredded Wheat 16.0 0 95 0 1 0.83 \n", + "Shredded Wheat 'n'Bran 19.0 0 140 0 1 1.00 \n", + "Shredded Wheat spoon size 20.0 0 120 0 1 1.00 \n", + "Special K 16.0 3 55 25 1 1.00 \n", + "Strawberry Fruit Wheats 15.0 5 90 25 2 1.00 \n", + "Total Corn Flakes 21.0 3 35 100 3 1.00 \n", + "Total Whole Grain 16.0 3 110 100 3 1.00 \n", + "Triples 21.0 3 60 25 3 1.00 \n", + "Wheat Chex 17.0 3 115 25 1 1.00 \n", + "Wheaties 17.0 3 110 25 1 1.00 \n", + "\n", + " cups rating \n", + "name \n", + "100% Bran 0.33 68.402973 \n", + "All-Bran 0.33 59.425505 \n", + "All-Bran with Extra Fiber 0.50 93.704912 \n", + "Bran Chex 0.67 49.120253 \n", + "Bran Flakes 0.67 53.313813 \n", + "Cheerios 1.25 50.764999 \n", + "Corn Chex 1.00 41.445019 \n", + "Corn Flakes 1.00 45.863324 \n", + "Cream of Wheat (Quick) 1.00 64.533816 \n", + "Crispix 1.00 46.895644 \n", + "Double Chex 0.75 44.330856 \n", + "Grape Nuts Flakes 0.88 52.076897 \n", + "Grape-Nuts 0.25 53.371007 \n", + "Great Grains Pecan 0.33 45.811716 \n", + "Just Right Crunchy Nuggets 1.00 36.523683 \n", + "Kix 1.50 39.241114 \n", + "Life 0.67 45.328074 \n", + "Maypo 1.00 54.850917 \n", + "Multi-Grain Cheerios 1.00 40.105965 \n", + "Nutri-grain Wheat 1.00 59.642837 \n", + "Product 19 1.00 41.503540 \n", + "Puffed Wheat 1.00 63.005645 \n", + "Quaker Oat Squares 0.50 49.511874 \n", + "Quaker Oatmeal 0.67 50.828392 \n", + "Raisin Squares 0.50 55.333142 \n", + "Rice Krispies 1.00 40.560159 \n", + "Shredded Wheat 1.00 68.235885 \n", + "Shredded Wheat 'n'Bran 0.67 74.472949 \n", + "Shredded Wheat spoon size 0.67 72.801787 \n", + "Special K 1.00 53.131324 \n", + "Strawberry Fruit Wheats 1.00 59.363993 \n", + "Total Corn Flakes 1.00 38.839746 \n", + "Total Whole Grain 1.00 46.658844 \n", + "Triples 0.75 39.106174 \n", + "Wheat Chex 0.67 49.787445 \n", + "Wheaties 1.00 51.592193 " + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "cereal.loc[(cereal['protein'] >= 2) & (cereal['sugars'] <= 6)]" + ] + }, + { + "cell_type": "markdown", + "id": "195fb654", + "metadata": { + "id": "195fb654" + }, + "source": [ + "### Optional Exercise 1\n", + "- Can you find the average sugar content of the cereals which list the portion `cups` size as 1.0?\n", + "- Can you find the highest and lowest calorie content of the previous selection?\n", + "\n", + "### Optional Exercise 2\n", + "- How many cereals by manufacturer `G` have a higher calorie content than 100?" + ] + }, + { + "cell_type": "markdown", + "id": "f6b1e95f", + "metadata": { + "id": "f6b1e95f" + }, + "source": [ + "### Handling duplicates\n", + "Next we will be using the `IMDB-Movie-Data.csv` dataset, it contains a list of movies, its genre, a brief description, director, actors, year, runtime, rating, votes, revenue and metascore.\n", + "\n", + "This dataset does not have duplicates initially, but we can artificially create duplicates by appending the dataset to itself." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ecabb8db", + "metadata": { + "id": "ecabb8db", + "outputId": "4455b065-860f-48d4-c82a-46dbc9c17516" + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
RankGenreDescriptionDirectorActorsYearRuntime (Minutes)RatingVotesRevenue (Millions)Metascore
Title
Guardians of the Galaxy1Action,Adventure,Sci-FiA group of intergalactic criminals are forced ...James GunnChris Pratt, Vin Diesel, Bradley Cooper, Zoe S...20141218.1757074333.1376.0
Prometheus2Adventure,Mystery,Sci-FiFollowing clues to the origin of mankind, a te...Ridley ScottNoomi Rapace, Logan Marshall-Green, Michael Fa...20121247.0485820126.4665.0
Split3Horror,ThrillerThree girls are kidnapped by a man with a diag...M. Night ShyamalanJames McAvoy, Anya Taylor-Joy, Haley Lu Richar...20161177.3157606138.1262.0
Sing4Animation,Comedy,FamilyIn a city of humanoid animals, a hustling thea...Christophe LourdeletMatthew McConaughey,Reese Witherspoon, Seth Ma...20161087.260545270.3259.0
Suicide Squad5Action,Adventure,FantasyA secret government agency recruits some of th...David AyerWill Smith, Jared Leto, Margot Robbie, Viola D...20161236.2393727325.0240.0
\n", + "
" + ], + "text/plain": [ + " Rank Genre \\\n", + "Title \n", + "Guardians of the Galaxy 1 Action,Adventure,Sci-Fi \n", + "Prometheus 2 Adventure,Mystery,Sci-Fi \n", + "Split 3 Horror,Thriller \n", + "Sing 4 Animation,Comedy,Family \n", + "Suicide Squad 5 Action,Adventure,Fantasy \n", + "\n", + " Description \\\n", + "Title \n", + "Guardians of the Galaxy A group of intergalactic criminals are forced ... \n", + "Prometheus Following clues to the origin of mankind, a te... \n", + "Split Three girls are kidnapped by a man with a diag... \n", + "Sing In a city of humanoid animals, a hustling thea... \n", + "Suicide Squad A secret government agency recruits some of th... \n", + "\n", + " Director \\\n", + "Title \n", + "Guardians of the Galaxy James Gunn \n", + "Prometheus Ridley Scott \n", + "Split M. Night Shyamalan \n", + "Sing Christophe Lourdelet \n", + "Suicide Squad David Ayer \n", + "\n", + " Actors \\\n", + "Title \n", + "Guardians of the Galaxy Chris Pratt, Vin Diesel, Bradley Cooper, Zoe S... \n", + "Prometheus Noomi Rapace, Logan Marshall-Green, Michael Fa... \n", + "Split James McAvoy, Anya Taylor-Joy, Haley Lu Richar... \n", + "Sing Matthew McConaughey,Reese Witherspoon, Seth Ma... \n", + "Suicide Squad Will Smith, Jared Leto, Margot Robbie, Viola D... \n", + "\n", + " Year Runtime (Minutes) Rating Votes \\\n", + "Title \n", + "Guardians of the Galaxy 2014 121 8.1 757074 \n", + "Prometheus 2012 124 7.0 485820 \n", + "Split 2016 117 7.3 157606 \n", + "Sing 2016 108 7.2 60545 \n", + "Suicide Squad 2016 123 6.2 393727 \n", + "\n", + " Revenue (Millions) Metascore \n", + "Title \n", + "Guardians of the Galaxy 333.13 76.0 \n", + "Prometheus 126.46 65.0 \n", + "Split 138.12 62.0 \n", + "Sing 270.32 59.0 \n", + "Suicide Squad 325.02 40.0 " + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "movies = pd.read_csv(\"IMDB-Movie-Data.csv\", index_col='Title')\n", + "movies.head()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "eb1250cd", + "metadata": { + "id": "eb1250cd", + "outputId": "a09079d5-15b3-4648-c468-d2d392b1e823" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "(1000, 11)" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "movies.shape" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b64d504d", + "metadata": { + "id": "b64d504d", + "outputId": "37ab1adb-2469-4d61-8480-26223490eb78" + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
RankGenreDescriptionDirectorActorsYearRuntime (Minutes)RatingVotesRevenue (Millions)Metascore
Title
Guardians of the Galaxy1Action,Adventure,Sci-FiA group of intergalactic criminals are forced ...James GunnChris Pratt, Vin Diesel, Bradley Cooper, Zoe S...20141218.1757074333.1376.0
Prometheus2Adventure,Mystery,Sci-FiFollowing clues to the origin of mankind, a te...Ridley ScottNoomi Rapace, Logan Marshall-Green, Michael Fa...20121247.0485820126.4665.0
Split3Horror,ThrillerThree girls are kidnapped by a man with a diag...M. Night ShyamalanJames McAvoy, Anya Taylor-Joy, Haley Lu Richar...20161177.3157606138.1262.0
Sing4Animation,Comedy,FamilyIn a city of humanoid animals, a hustling thea...Christophe LourdeletMatthew McConaughey,Reese Witherspoon, Seth Ma...20161087.260545270.3259.0
Suicide Squad5Action,Adventure,FantasyA secret government agency recruits some of th...David AyerWill Smith, Jared Leto, Margot Robbie, Viola D...20161236.2393727325.0240.0
\n", + "
" + ], + "text/plain": [ + " Rank Genre \\\n", + "Title \n", + "Guardians of the Galaxy 1 Action,Adventure,Sci-Fi \n", + "Prometheus 2 Adventure,Mystery,Sci-Fi \n", + "Split 3 Horror,Thriller \n", + "Sing 4 Animation,Comedy,Family \n", + "Suicide Squad 5 Action,Adventure,Fantasy \n", + "\n", + " Description \\\n", + "Title \n", + "Guardians of the Galaxy A group of intergalactic criminals are forced ... \n", + "Prometheus Following clues to the origin of mankind, a te... \n", + "Split Three girls are kidnapped by a man with a diag... \n", + "Sing In a city of humanoid animals, a hustling thea... \n", + "Suicide Squad A secret government agency recruits some of th... \n", + "\n", + " Director \\\n", + "Title \n", + "Guardians of the Galaxy James Gunn \n", + "Prometheus Ridley Scott \n", + "Split M. Night Shyamalan \n", + "Sing Christophe Lourdelet \n", + "Suicide Squad David Ayer \n", + "\n", + " Actors \\\n", + "Title \n", + "Guardians of the Galaxy Chris Pratt, Vin Diesel, Bradley Cooper, Zoe S... \n", + "Prometheus Noomi Rapace, Logan Marshall-Green, Michael Fa... \n", + "Split James McAvoy, Anya Taylor-Joy, Haley Lu Richar... \n", + "Sing Matthew McConaughey,Reese Witherspoon, Seth Ma... \n", + "Suicide Squad Will Smith, Jared Leto, Margot Robbie, Viola D... \n", + "\n", + " Year Runtime (Minutes) Rating Votes \\\n", + "Title \n", + "Guardians of the Galaxy 2014 121 8.1 757074 \n", + "Prometheus 2012 124 7.0 485820 \n", + "Split 2016 117 7.3 157606 \n", + "Sing 2016 108 7.2 60545 \n", + "Suicide Squad 2016 123 6.2 393727 \n", + "\n", + " Revenue (Millions) Metascore \n", + "Title \n", + "Guardians of the Galaxy 333.13 76.0 \n", + "Prometheus 126.46 65.0 \n", + "Split 138.12 62.0 \n", + "Sing 270.32 59.0 \n", + "Suicide Squad 325.02 40.0 " + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# now lets duplicate the data\n", + "# concatenate the dataframe to itself\n", + "dup_movies = pd.concat([movies, movies])\n", + "dup_movies.head()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0bb68f1c", + "metadata": { + "id": "0bb68f1c", + "outputId": "ee52dec4-5f9a-42f0-e124-fc110fe72e7b" + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
RankGenreDescriptionDirectorActorsYearRuntime (Minutes)RatingVotesRevenue (Millions)Metascore
Title
Secret in Their Eyes996Crime,Drama,MysteryA tight-knit team of rising investigators, alo...Billy RayChiwetel Ejiofor, Nicole Kidman, Julia Roberts...20151116.227585NaN45.0
Hostel: Part II997HorrorThree American college students studying abroa...Eli RothLauren German, Heather Matarazzo, Bijou Philli...2007945.57315217.5446.0
Step Up 2: The Streets998Drama,Music,RomanceRomantic sparks occur between two dance studen...Jon M. ChuRobert Hoffman, Briana Evigan, Cassie Ventura,...2008986.27069958.0150.0
Search Party999Adventure,ComedyA pair of friends embark on a mission to reuni...Scot ArmstrongAdam Pally, T.J. Miller, Thomas Middleditch,Sh...2014935.64881NaN22.0
Nine Lives1000Comedy,Family,FantasyA stuffy businessman finds himself trapped ins...Barry SonnenfeldKevin Spacey, Jennifer Garner, Robbie Amell,Ch...2016875.31243519.6411.0
\n", + "
" + ], + "text/plain": [ + " Rank Genre \\\n", + "Title \n", + "Secret in Their Eyes 996 Crime,Drama,Mystery \n", + "Hostel: Part II 997 Horror \n", + "Step Up 2: The Streets 998 Drama,Music,Romance \n", + "Search Party 999 Adventure,Comedy \n", + "Nine Lives 1000 Comedy,Family,Fantasy \n", + "\n", + " Description \\\n", + "Title \n", + "Secret in Their Eyes A tight-knit team of rising investigators, alo... \n", + "Hostel: Part II Three American college students studying abroa... \n", + "Step Up 2: The Streets Romantic sparks occur between two dance studen... \n", + "Search Party A pair of friends embark on a mission to reuni... \n", + "Nine Lives A stuffy businessman finds himself trapped ins... \n", + "\n", + " Director \\\n", + "Title \n", + "Secret in Their Eyes Billy Ray \n", + "Hostel: Part II Eli Roth \n", + "Step Up 2: The Streets Jon M. Chu \n", + "Search Party Scot Armstrong \n", + "Nine Lives Barry Sonnenfeld \n", + "\n", + " Actors \\\n", + "Title \n", + "Secret in Their Eyes Chiwetel Ejiofor, Nicole Kidman, Julia Roberts... \n", + "Hostel: Part II Lauren German, Heather Matarazzo, Bijou Philli... \n", + "Step Up 2: The Streets Robert Hoffman, Briana Evigan, Cassie Ventura,... \n", + "Search Party Adam Pally, T.J. Miller, Thomas Middleditch,Sh... \n", + "Nine Lives Kevin Spacey, Jennifer Garner, Robbie Amell,Ch... \n", + "\n", + " Year Runtime (Minutes) Rating Votes \\\n", + "Title \n", + "Secret in Their Eyes 2015 111 6.2 27585 \n", + "Hostel: Part II 2007 94 5.5 73152 \n", + "Step Up 2: The Streets 2008 98 6.2 70699 \n", + "Search Party 2014 93 5.6 4881 \n", + "Nine Lives 2016 87 5.3 12435 \n", + "\n", + " Revenue (Millions) Metascore \n", + "Title \n", + "Secret in Their Eyes NaN 45.0 \n", + "Hostel: Part II 17.54 46.0 \n", + "Step Up 2: The Streets 58.01 50.0 \n", + "Search Party NaN 22.0 \n", + "Nine Lives 19.64 11.0 " + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dup_movies.tail()" + ] + }, + { + "cell_type": "markdown", + "id": "d8505890", + "metadata": { + "id": "d8505890" + }, + "source": [ + "We now have 2000 movies, 1000 of which are duplicates" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "03d63528", + "metadata": { + "id": "03d63528", + "outputId": "22b729c7-1f68-476e-95b1-ff60f7bea630" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "(2000, 11)" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dup_movies.shape" + ] + }, + { + "cell_type": "markdown", + "id": "a4417a5f", + "metadata": { + "id": "a4417a5f" + }, + "source": [ + "We can use the `drop_duplicates(inplace=True)` method to delete the same Dataframe we are working with, this way we don't have to store the DF to a variable" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "36ef025f", + "metadata": { + "id": "36ef025f" + }, + "outputs": [], + "source": [ + "dup_movies.drop_duplicates(inplace=True)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "bd2c8feb", + "metadata": { + "id": "bd2c8feb", + "outputId": "b4ac5165-6a79-414f-ed29-5656a0f964ac" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "(1000, 11)" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dup_movies.shape" + ] + }, + { + "cell_type": "markdown", + "id": "47167483", + "metadata": { + "id": "47167483" + }, + "source": [ + "The `drop_duplicates()` also has the `keep` argument, which can do the following:\n", + "- `first` (default) drop duplicates except for the first occurrence\n", + "- `last` drop duplicates except for the last occurrence\n", + "- `False` drop all duplicates/" + ] + }, + { + "cell_type": "markdown", + "id": "9fd0f9d4", + "metadata": { + "id": "9fd0f9d4" + }, + "source": [ + "### Cleaning columns\n", + "We can change the name of our columns to make them easier to work with, remove symbols, spaces, typos.\n", + "\n", + "Lets print the columns of our original movie dataset." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1e0be569", + "metadata": { + "id": "1e0be569", + "outputId": "3a884657-1c27-4bb4-822a-a9d17d2c668b" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Index(['Rank', 'Genre', 'Description', 'Director', 'Actors', 'Year',\n", + " 'Runtime (Minutes)', 'Rating', 'Votes', 'Revenue (Millions)',\n", + " 'Metascore'],\n", + " dtype='object')" + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "movies.columns" + ] + }, + { + "cell_type": "markdown", + "id": "4b5cc133", + "metadata": { + "id": "4b5cc133" + }, + "source": [ + "We can rename columns using a dictionary and the `rename()` method. New names are passed as a dictionary." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6581b693", + "metadata": { + "id": "6581b693", + "outputId": "d9a6b06e-5539-4668-fd67-d3aa67f0def8" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Index(['Rank', 'Genre', 'Description', 'Director', 'Actors', 'Year', 'Runtime',\n", + " 'Rating', 'Votes', 'Revenue', 'Metascore'],\n", + " dtype='object')" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "movies.rename(columns={'Runtime (Minutes)' : 'Runtime',\n", + " 'Revenue (Millions)' : 'Revenue'}, inplace=True)\n", + "movies.columns" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5872d2e7", + "metadata": { + "id": "5872d2e7", + "outputId": "6a49eb0e-b2ed-4a29-8599-6d433958d396" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Title\n", + "Guardians of the Galaxy 333.13\n", + "Prometheus 126.46\n", + "Split 138.12\n", + "Sing 270.32\n", + "Suicide Squad 325.02\n", + " ... \n", + "Secret in Their Eyes NaN\n", + "Hostel: Part II 17.54\n", + "Step Up 2: The Streets 58.01\n", + "Search Party NaN\n", + "Nine Lives 19.64\n", + "Name: Revenue, Length: 1000, dtype: float64" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "movies.Revenue" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7e82f524", + "metadata": { + "id": "7e82f524", + "outputId": "0e3585d1-f4a3-482d-a766-27759c7de697" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Title\n", + "Guardians of the Galaxy 121\n", + "Prometheus 124\n", + "Split 117\n", + "Sing 108\n", + "Suicide Squad 123\n", + " ... \n", + "Secret in Their Eyes 111\n", + "Hostel: Part II 94\n", + "Step Up 2: The Streets 98\n", + "Search Party 93\n", + "Nine Lives 87\n", + "Name: Runtime, Length: 1000, dtype: int64" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "movies.Runtime" + ] + }, + { + "cell_type": "markdown", + "id": "836843ab", + "metadata": { + "id": "836843ab" + }, + "source": [ + "We can also rename by using the `columns` property and passing a list of names. Lets have all of our names be lowercase." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b57e9e22", + "metadata": { + "id": "b57e9e22", + "outputId": "94029dc2-b4ae-4b89-9f2e-0b4354d520a3" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Index(['rank', 'genre', 'description', 'director', 'actors', 'year', 'runtime',\n", + " 'rating', 'votes', 'revenue_millions', 'metascore'],\n", + " dtype='object')" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "movies.columns = ['rank', 'genre', 'description', 'director', 'actors', 'year', 'runtime', 'rating', 'votes', 'revenue_millions', 'metascore']\n", + "movies.columns" + ] + }, + { + "cell_type": "markdown", + "id": "6acbf0e7", + "metadata": { + "id": "6acbf0e7" + }, + "source": [ + "We can also simplify renaning using list comprehension." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "76a8141b", + "metadata": { + "id": "76a8141b", + "outputId": "af4a9230-b74f-4ebc-bf6e-4a18e7a41727" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Index(['RANK', 'GENRE', 'DESCRIPTION', 'DIRECTOR', 'ACTORS', 'YEAR', 'RUNTIME',\n", + " 'RATING', 'VOTES', 'REVENUE_MILLIONS', 'METASCORE'],\n", + " dtype='object')" + ] + }, + "execution_count": 39, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "movies.columns = [col.upper() for col in movies]\n", + "movies.columns" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6340b0ed", + "metadata": { + "id": "6340b0ed", + "outputId": "c9f25e88-ee96-4596-8576-33b40c422f88" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Index(['rank', 'genre', 'description', 'director', 'actors', 'year', 'runtime',\n", + " 'rating', 'votes', 'revenue_millions', 'metascore'],\n", + " dtype='object')" + ] + }, + "execution_count": 40, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "movies.columns = [col.lower() for col in movies]\n", + "movies.columns" + ] + }, + { + "cell_type": "markdown", + "id": "5982d6a0", + "metadata": { + "id": "5982d6a0" + }, + "source": [ + "### Working with missing values\n", + "When working with datasets, we are almost guaranteed to find missing data or null values. We can sometimes find `None` of Numpy's `np.nan`. We have two options:\n", + "- Get rid of rows or columns with null values.\n", + "- Replace null values with something else.\n", + "First, lets check if our movie columns contains null values.\n", + "\n", + "`isnull()` will return if each cell in the dataset has a null value, evaluating to `True` or `False`.\n", + "\n", + "We can then count the number of nulls in each column using the `sum()` method." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "65e419d9", + "metadata": { + "id": "65e419d9", + "outputId": "a7a863e4-4e36-46c0-bbbc-0f49bcfb4905" + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
rankgenredescriptiondirectoractorsyearruntimeratingvotesrevenue_millionsmetascore
Title
Guardians of the GalaxyFalseFalseFalseFalseFalseFalseFalseFalseFalseFalseFalse
PrometheusFalseFalseFalseFalseFalseFalseFalseFalseFalseFalseFalse
SplitFalseFalseFalseFalseFalseFalseFalseFalseFalseFalseFalse
SingFalseFalseFalseFalseFalseFalseFalseFalseFalseFalseFalse
Suicide SquadFalseFalseFalseFalseFalseFalseFalseFalseFalseFalseFalse
....................................
Secret in Their EyesFalseFalseFalseFalseFalseFalseFalseFalseFalseTrueFalse
Hostel: Part IIFalseFalseFalseFalseFalseFalseFalseFalseFalseFalseFalse
Step Up 2: The StreetsFalseFalseFalseFalseFalseFalseFalseFalseFalseFalseFalse
Search PartyFalseFalseFalseFalseFalseFalseFalseFalseFalseTrueFalse
Nine LivesFalseFalseFalseFalseFalseFalseFalseFalseFalseFalseFalse
\n", + "

1000 rows × 11 columns

\n", + "
" + ], + "text/plain": [ + " rank genre description director actors year \\\n", + "Title \n", + "Guardians of the Galaxy False False False False False False \n", + "Prometheus False False False False False False \n", + "Split False False False False False False \n", + "Sing False False False False False False \n", + "Suicide Squad False False False False False False \n", + "... ... ... ... ... ... ... \n", + "Secret in Their Eyes False False False False False False \n", + "Hostel: Part II False False False False False False \n", + "Step Up 2: The Streets False False False False False False \n", + "Search Party False False False False False False \n", + "Nine Lives False False False False False False \n", + "\n", + " runtime rating votes revenue_millions metascore \n", + "Title \n", + "Guardians of the Galaxy False False False False False \n", + "Prometheus False False False False False \n", + "Split False False False False False \n", + "Sing False False False False False \n", + "Suicide Squad False False False False False \n", + "... ... ... ... ... ... \n", + "Secret in Their Eyes False False False True False \n", + "Hostel: Part II False False False False False \n", + "Step Up 2: The Streets False False False False False \n", + "Search Party False False False True False \n", + "Nine Lives False False False False False \n", + "\n", + "[1000 rows x 11 columns]" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "movies.isnull()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b27f2f44", + "metadata": { + "id": "b27f2f44", + "outputId": "8e9264b2-2059-46c5-ecfd-987443c4e91e" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "rank 0\n", + "genre 0\n", + "description 0\n", + "director 0\n", + "actors 0\n", + "year 0\n", + "runtime 0\n", + "rating 0\n", + "votes 0\n", + "revenue_millions 128\n", + "metascore 64\n", + "dtype: int64" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "movies.isnull().sum()" + ] + }, + { + "cell_type": "markdown", + "id": "09f00b59", + "metadata": { + "id": "09f00b59" + }, + "source": [ + "We can see that our `revenue_millions` column and `metascore` column have null values. To remove a row containing a null value you can use `.dropna()`. To remove a column containing a null value you can change the axis by running `.dropna(axis=1)`. Remember the first axis we get on the shape of a DataFrame is rows, and the second one is columns, this is where the axis 0 = rows, and axis 1 = columns comes from.\n", + "\n", + "Lets see a dropping example with a temporary DF." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b240a7b5", + "metadata": { + "id": "b240a7b5", + "outputId": "f0cd1a09-1ed0-4d9b-df2a-949f76dfb3b7" + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
rankgenredescriptiondirectoractorsyearruntimeratingvotesrevenue_millionsmetascore
Title
Guardians of the Galaxy1Action,Adventure,Sci-FiA group of intergalactic criminals are forced ...James GunnChris Pratt, Vin Diesel, Bradley Cooper, Zoe S...20141218.1757074333.1376.0
Prometheus2Adventure,Mystery,Sci-FiFollowing clues to the origin of mankind, a te...Ridley ScottNoomi Rapace, Logan Marshall-Green, Michael Fa...20121247.0485820126.4665.0
Split3Horror,ThrillerThree girls are kidnapped by a man with a diag...M. Night ShyamalanJames McAvoy, Anya Taylor-Joy, Haley Lu Richar...20161177.3157606138.1262.0
Sing4Animation,Comedy,FamilyIn a city of humanoid animals, a hustling thea...Christophe LourdeletMatthew McConaughey,Reese Witherspoon, Seth Ma...20161087.260545270.3259.0
Suicide Squad5Action,Adventure,FantasyA secret government agency recruits some of th...David AyerWill Smith, Jared Leto, Margot Robbie, Viola D...20161236.2393727325.0240.0
\n", + "
" + ], + "text/plain": [ + " rank genre \\\n", + "Title \n", + "Guardians of the Galaxy 1 Action,Adventure,Sci-Fi \n", + "Prometheus 2 Adventure,Mystery,Sci-Fi \n", + "Split 3 Horror,Thriller \n", + "Sing 4 Animation,Comedy,Family \n", + "Suicide Squad 5 Action,Adventure,Fantasy \n", + "\n", + " description \\\n", + "Title \n", + "Guardians of the Galaxy A group of intergalactic criminals are forced ... \n", + "Prometheus Following clues to the origin of mankind, a te... \n", + "Split Three girls are kidnapped by a man with a diag... \n", + "Sing In a city of humanoid animals, a hustling thea... \n", + "Suicide Squad A secret government agency recruits some of th... \n", + "\n", + " director \\\n", + "Title \n", + "Guardians of the Galaxy James Gunn \n", + "Prometheus Ridley Scott \n", + "Split M. Night Shyamalan \n", + "Sing Christophe Lourdelet \n", + "Suicide Squad David Ayer \n", + "\n", + " actors \\\n", + "Title \n", + "Guardians of the Galaxy Chris Pratt, Vin Diesel, Bradley Cooper, Zoe S... \n", + "Prometheus Noomi Rapace, Logan Marshall-Green, Michael Fa... \n", + "Split James McAvoy, Anya Taylor-Joy, Haley Lu Richar... \n", + "Sing Matthew McConaughey,Reese Witherspoon, Seth Ma... \n", + "Suicide Squad Will Smith, Jared Leto, Margot Robbie, Viola D... \n", + "\n", + " year runtime rating votes revenue_millions \\\n", + "Title \n", + "Guardians of the Galaxy 2014 121 8.1 757074 333.13 \n", + "Prometheus 2012 124 7.0 485820 126.46 \n", + "Split 2016 117 7.3 157606 138.12 \n", + "Sing 2016 108 7.2 60545 270.32 \n", + "Suicide Squad 2016 123 6.2 393727 325.02 \n", + "\n", + " metascore \n", + "Title \n", + "Guardians of the Galaxy 76.0 \n", + "Prometheus 65.0 \n", + "Split 62.0 \n", + "Sing 59.0 \n", + "Suicide Squad 40.0 " + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Copy the original Dataframe into a new variable\n", + "drop_movies = movies.copy()\n", + "drop_movies.head()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7d79d9f6", + "metadata": { + "id": "7d79d9f6", + "outputId": "cfe4fdb3-5a82-4eff-abe3-677ff7dfef60" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "(1000, 11)" + ] + }, + "execution_count": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "drop_movies.shape" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6c7dccc2", + "metadata": { + "id": "6c7dccc2", + "outputId": "074783cd-4860-4372-b031-ff27ff60819d" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "rank 0\n", + "genre 0\n", + "description 0\n", + "director 0\n", + "actors 0\n", + "year 0\n", + "runtime 0\n", + "rating 0\n", + "votes 0\n", + "revenue_millions 128\n", + "metascore 64\n", + "dtype: int64" + ] + }, + "execution_count": 45, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "drop_movies.isnull().sum()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cb26c582", + "metadata": { + "id": "cb26c582" + }, + "outputs": [], + "source": [ + "drop_movies.dropna(inplace=True)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "94e31c4f", + "metadata": { + "id": "94e31c4f", + "outputId": "37c152f5-6ee4-4e6c-8c6e-41e8723a3c42" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "(838, 11)" + ] + }, + "execution_count": 47, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "drop_movies.shape" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6ba2b90d", + "metadata": { + "id": "6ba2b90d", + "outputId": "20bdb07d-d6c0-484e-c242-e0f88bbfc640" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "rank 0\n", + "genre 0\n", + "description 0\n", + "director 0\n", + "actors 0\n", + "year 0\n", + "runtime 0\n", + "rating 0\n", + "votes 0\n", + "revenue_millions 0\n", + "metascore 0\n", + "dtype: int64" + ] + }, + "execution_count": 48, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "drop_movies.isnull().sum()" + ] + }, + { + "cell_type": "markdown", + "id": "a05a2a99", + "metadata": { + "id": "a05a2a99" + }, + "source": [ + "It seems like a waste to drop all of those rows that were missing some data. Lets replace the missing values with something else in our original dataframe `movies`" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d861be5b", + "metadata": { + "id": "d861be5b", + "outputId": "910f505b-2365-4434-fe67-e933cfb3dd1a" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "rank 0\n", + "genre 0\n", + "description 0\n", + "director 0\n", + "actors 0\n", + "year 0\n", + "runtime 0\n", + "rating 0\n", + "votes 0\n", + "revenue_millions 128\n", + "metascore 64\n", + "dtype: int64" + ] + }, + "execution_count": 49, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "movies.isnull().sum()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d3b492aa", + "metadata": { + "id": "d3b492aa" + }, + "outputs": [], + "source": [ + "# Select the revenue column\n", + "revenue = movies['revenue_millions']" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3d17b033", + "metadata": { + "id": "3d17b033", + "outputId": "54c1dcb3-64db-4b04-f67a-489e88bcfab5" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Title\n", + "Guardians of the Galaxy 333.13\n", + "Prometheus 126.46\n", + "Split 138.12\n", + "Sing 270.32\n", + "Suicide Squad 325.02\n", + " ... \n", + "Secret in Their Eyes NaN\n", + "Hostel: Part II 17.54\n", + "Step Up 2: The Streets 58.01\n", + "Search Party NaN\n", + "Nine Lives 19.64\n", + "Name: revenue_millions, Length: 1000, dtype: float64" + ] + }, + "execution_count": 51, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Series type, not dataframe\n", + "revenue" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ccab64b0", + "metadata": { + "id": "ccab64b0", + "outputId": "2619a941-dff7-4754-8273-2e350e8bdfab" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "82.95637614678898" + ] + }, + "execution_count": 52, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Calculate revenue mean\n", + "revenue_mean = revenue.mean()\n", + "revenue_mean" + ] + }, + { + "cell_type": "markdown", + "id": "d52025de", + "metadata": { + "id": "d52025de" + }, + "source": [ + "Lets fill up our missing values with our new `revenue_mean` value using the `.fillna()` method. Remember to use the `inplace=True` to affect the original **Dataframe**." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "247fc55c", + "metadata": { + "id": "247fc55c" + }, + "outputs": [], + "source": [ + "revenue.fillna(revenue_mean, inplace=True)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "bb5e376d", + "metadata": { + "id": "bb5e376d", + "outputId": "e361858d-5b21-411f-e53f-926b608b5573" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "rank 0\n", + "genre 0\n", + "description 0\n", + "director 0\n", + "actors 0\n", + "year 0\n", + "runtime 0\n", + "rating 0\n", + "votes 0\n", + "revenue_millions 0\n", + "metascore 64\n", + "dtype: int64" + ] + }, + "execution_count": 54, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "movies.isnull().sum()" + ] + }, + { + "cell_type": "markdown", + "id": "fc4c5ffc", + "metadata": { + "id": "fc4c5ffc" + }, + "source": [ + "### Optional exercise\n", + "- Fill out the metascore missing values with the mean of metascores.\n", + "- Verify that the `movies` DF has no null values." + ] + }, + { + "cell_type": "markdown", + "id": "8ad480b5", + "metadata": { + "id": "8ad480b5" + }, + "source": [ + "### Understanding your variables\n", + "Remember you can get a summary of your data using the `describe()` method. Lets describe `movies`" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ec6c7937", + "metadata": { + "id": "ec6c7937", + "outputId": "41223061-330e-46f8-ed82-1fbb450b99f8" + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
rankyearruntimeratingvotesrevenue_millionsmetascore
count1000.0000001000.0000001000.0000001000.0000001.000000e+031000.000000936.000000
mean500.5000002012.783000113.1720006.7232001.698083e+0582.95637658.985043
std288.8194363.20596218.8109080.9454291.887626e+0596.41204317.194757
min1.0000002006.00000066.0000001.9000006.100000e+010.00000011.000000
25%250.7500002010.000000100.0000006.2000003.630900e+0417.44250047.000000
50%500.5000002014.000000111.0000006.8000001.107990e+0560.37500059.500000
75%750.2500002016.000000123.0000007.4000002.399098e+0599.17750072.000000
max1000.0000002016.000000191.0000009.0000001.791916e+06936.630000100.000000
\n", + "
" + ], + "text/plain": [ + " rank year runtime rating votes \\\n", + "count 1000.000000 1000.000000 1000.000000 1000.000000 1.000000e+03 \n", + "mean 500.500000 2012.783000 113.172000 6.723200 1.698083e+05 \n", + "std 288.819436 3.205962 18.810908 0.945429 1.887626e+05 \n", + "min 1.000000 2006.000000 66.000000 1.900000 6.100000e+01 \n", + "25% 250.750000 2010.000000 100.000000 6.200000 3.630900e+04 \n", + "50% 500.500000 2014.000000 111.000000 6.800000 1.107990e+05 \n", + "75% 750.250000 2016.000000 123.000000 7.400000 2.399098e+05 \n", + "max 1000.000000 2016.000000 191.000000 9.000000 1.791916e+06 \n", + "\n", + " revenue_millions metascore \n", + "count 1000.000000 936.000000 \n", + "mean 82.956376 58.985043 \n", + "std 96.412043 17.194757 \n", + "min 0.000000 11.000000 \n", + "25% 17.442500 47.000000 \n", + "50% 60.375000 59.500000 \n", + "75% 99.177500 72.000000 \n", + "max 936.630000 100.000000 " + ] + }, + "execution_count": 55, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "movies.describe()" + ] + }, + { + "cell_type": "markdown", + "id": "5ed8e015", + "metadata": { + "id": "5ed8e015" + }, + "source": [ + "To describe a single column you can select the column and run the method. You can also describe string columns, like `genre`" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "aa4e7536", + "metadata": { + "id": "aa4e7536", + "outputId": "51cbb9e8-2dc3-4b6d-bc0b-47d9574ad77c" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "count 1000\n", + "unique 207\n", + "top Action,Adventure,Sci-Fi\n", + "freq 50\n", + "Name: genre, dtype: object" + ] + }, + "execution_count": 56, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "movies.genre.describe()" + ] + }, + { + "cell_type": "markdown", + "id": "53058870", + "metadata": { + "id": "53058870" + }, + "source": [ + "To count frequency of data you can use `value_counts()`" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2771362b", + "metadata": { + "id": "2771362b", + "outputId": "40185a96-ceeb-4e61-bf89-7c6105e8766e" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Action,Adventure,Sci-Fi 50\n", + "Drama 48\n", + "Comedy,Drama,Romance 35\n", + "Comedy 32\n", + "Drama,Romance 31\n", + "Animation,Adventure,Comedy 27\n", + "Action,Adventure,Fantasy 27\n", + "Comedy,Drama 27\n", + "Comedy,Romance 26\n", + "Crime,Drama,Thriller 24\n", + "Name: genre, dtype: int64" + ] + }, + "execution_count": 57, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Show top 10 genres\n", + "movies.genre.value_counts().head(10)" + ] + }, + { + "cell_type": "markdown", + "id": "64c6db78", + "metadata": { + "id": "64c6db78" + }, + "source": [ + "We can further try to describe relationship in our variables by using the `.corr()` method, this will show the relationship between two variables (bivariate relationship). Positive numbers indicate a positive correlation, one goes up when the other variable goes up. Negative numbers indicate an inverse correlation - one goes up the other goes down. 1.0 indicates a perfect correlation." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "34eb6dda", + "metadata": { + "id": "34eb6dda", + "outputId": "941dbff7-8517-4ee0-e1b1-d8106ca16cd1" + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
rankyearruntimeratingvotesrevenue_millionsmetascore
rank1.000000-0.261605-0.221739-0.219555-0.283876-0.252996-0.191869
year-0.2616051.000000-0.164900-0.211219-0.411904-0.117562-0.079305
runtime-0.221739-0.1649001.0000000.3922140.4070620.2478340.211978
rating-0.219555-0.2112190.3922141.0000000.5115370.1895270.631897
votes-0.283876-0.4119040.4070620.5115371.0000000.6079410.325684
revenue_millions-0.252996-0.1175620.2478340.1895270.6079411.0000000.133328
metascore-0.191869-0.0793050.2119780.6318970.3256840.1333281.000000
\n", + "
" + ], + "text/plain": [ + " rank year runtime rating votes \\\n", + "rank 1.000000 -0.261605 -0.221739 -0.219555 -0.283876 \n", + "year -0.261605 1.000000 -0.164900 -0.211219 -0.411904 \n", + "runtime -0.221739 -0.164900 1.000000 0.392214 0.407062 \n", + "rating -0.219555 -0.211219 0.392214 1.000000 0.511537 \n", + "votes -0.283876 -0.411904 0.407062 0.511537 1.000000 \n", + "revenue_millions -0.252996 -0.117562 0.247834 0.189527 0.607941 \n", + "metascore -0.191869 -0.079305 0.211978 0.631897 0.325684 \n", + "\n", + " revenue_millions metascore \n", + "rank -0.252996 -0.191869 \n", + "year -0.117562 -0.079305 \n", + "runtime 0.247834 0.211978 \n", + "rating 0.189527 0.631897 \n", + "votes 0.607941 0.325684 \n", + "revenue_millions 1.000000 0.133328 \n", + "metascore 0.133328 1.000000 " + ] + }, + "execution_count": 58, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "movies.corr()" + ] + }, + { + "cell_type": "markdown", + "id": "3727321c", + "metadata": { + "id": "3727321c" + }, + "source": [ + "### Manipulating Dataframes\n", + "On the next section we will further explore how to explore Dataframe and Series:\n", + "- Selecting columns\n", + "- Selecting rows\n", + "- Conditional selections\n", + "\n", + "By now we now how to select columns using square brackets" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "75af9253", + "metadata": { + "id": "75af9253", + "outputId": "ff82ca70-09d6-4575-c01c-b854e59ebf0b" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Title\n", + "Guardians of the Galaxy Action,Adventure,Sci-Fi\n", + "Prometheus Adventure,Mystery,Sci-Fi\n", + "Split Horror,Thriller\n", + "Sing Animation,Comedy,Family\n", + "Suicide Squad Action,Adventure,Fantasy\n", + " ... \n", + "Secret in Their Eyes Crime,Drama,Mystery\n", + "Hostel: Part II Horror\n", + "Step Up 2: The Streets Drama,Music,Romance\n", + "Search Party Adventure,Comedy\n", + "Nine Lives Comedy,Family,Fantasy\n", + "Name: genre, Length: 1000, dtype: object" + ] + }, + "execution_count": 60, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "genre_col = movies['genre']\n", + "genre_col" + ] + }, + { + "cell_type": "markdown", + "id": "e82ca17e", + "metadata": { + "id": "e82ca17e" + }, + "source": [ + "The following code returns a Series. To extract a column as a DataFrame you need to pass a list of column names, in our case a single column" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "05efd7b4", + "metadata": { + "id": "05efd7b4", + "outputId": "8c109d8d-a106-469b-8437-cf8f9beaf657" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "pandas.core.series.Series" + ] + }, + "execution_count": 61, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(genre_col)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0dc1ccc2", + "metadata": { + "id": "0dc1ccc2", + "outputId": "0466b335-29c5-47bd-b8c9-397c0b2e671a" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "pandas.core.frame.DataFrame" + ] + }, + "execution_count": 62, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "genre_col = movies[['genre']]\n", + "type(genre_col)" + ] + }, + { + "cell_type": "markdown", + "id": "ac778194", + "metadata": { + "id": "ac778194" + }, + "source": [ + "Since its just a list, adding another column name is easy" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ae181223", + "metadata": { + "id": "ae181223", + "outputId": "56ba0f4d-9747-425e-8a7c-001d11103d51" + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
genrerating
Title
Guardians of the GalaxyAction,Adventure,Sci-Fi8.1
PrometheusAdventure,Mystery,Sci-Fi7.0
SplitHorror,Thriller7.3
SingAnimation,Comedy,Family7.2
Suicide SquadAction,Adventure,Fantasy6.2
\n", + "
" + ], + "text/plain": [ + " genre rating\n", + "Title \n", + "Guardians of the Galaxy Action,Adventure,Sci-Fi 8.1\n", + "Prometheus Adventure,Mystery,Sci-Fi 7.0\n", + "Split Horror,Thriller 7.3\n", + "Sing Animation,Comedy,Family 7.2\n", + "Suicide Squad Action,Adventure,Fantasy 6.2" + ] + }, + "execution_count": 63, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "subset = movies[['genre', 'rating']]\n", + "subset.head()" + ] + }, + { + "cell_type": "markdown", + "id": "4df4fed1", + "metadata": { + "id": "4df4fed1" + }, + "source": [ + "#### Data by rows\n", + "For rows we have two options:\n", + "- `.loc` - locates by name\n", + "- `.iloc` - locates by numerical index\n", + "\n", + "We are still indexed by movie Title so to use `.loc` we give the Title of the movie" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "13437385", + "metadata": { + "id": "13437385", + "outputId": "88619fe4-c745-48a2-c5e5-21c223255fa2" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "rank 2\n", + "genre Adventure,Mystery,Sci-Fi\n", + "description Following clues to the origin of mankind, a te...\n", + "director Ridley Scott\n", + "actors Noomi Rapace, Logan Marshall-Green, Michael Fa...\n", + "year 2012\n", + "runtime 124\n", + "rating 7.0\n", + "votes 485820\n", + "revenue_millions 126.46\n", + "metascore 65.0\n", + "Name: Prometheus, dtype: object" + ] + }, + "execution_count": 64, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "prom = movies.loc['Prometheus']\n", + "prom" + ] + }, + { + "cell_type": "markdown", + "id": "a5ff7a39", + "metadata": { + "id": "a5ff7a39" + }, + "source": [ + "With `.iloc` we would give the numerical index of the movie" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "17ec4b78", + "metadata": { + "id": "17ec4b78", + "outputId": "a1512ac6-e8d4-43cd-c5f1-c3e4895b2140" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "rank 2\n", + "genre Adventure,Mystery,Sci-Fi\n", + "description Following clues to the origin of mankind, a te...\n", + "director Ridley Scott\n", + "actors Noomi Rapace, Logan Marshall-Green, Michael Fa...\n", + "year 2012\n", + "runtime 124\n", + "rating 7.0\n", + "votes 485820\n", + "revenue_millions 126.46\n", + "metascore 65.0\n", + "Name: Prometheus, dtype: object" + ] + }, + "execution_count": 66, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "movies.iloc[1]" + ] + }, + { + "cell_type": "markdown", + "id": "5bfb3ae5", + "metadata": { + "id": "5bfb3ae5" + }, + "source": [ + "These methods can be thought of as similar to `list` slicing. Lets select multiple rows." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c8c5be74", + "metadata": { + "id": "c8c5be74", + "outputId": "00957497-2c53-450a-c8b0-b48faa0c7e83" + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
rankgenredescriptiondirectoractorsyearruntimeratingvotesrevenue_millionsmetascore
Title
Prometheus2Adventure,Mystery,Sci-FiFollowing clues to the origin of mankind, a te...Ridley ScottNoomi Rapace, Logan Marshall-Green, Michael Fa...20121247.0485820126.4665.0
Split3Horror,ThrillerThree girls are kidnapped by a man with a diag...M. Night ShyamalanJames McAvoy, Anya Taylor-Joy, Haley Lu Richar...20161177.3157606138.1262.0
Sing4Animation,Comedy,FamilyIn a city of humanoid animals, a hustling thea...Christophe LourdeletMatthew McConaughey,Reese Witherspoon, Seth Ma...20161087.260545270.3259.0
\n", + "
" + ], + "text/plain": [ + " rank genre \\\n", + "Title \n", + "Prometheus 2 Adventure,Mystery,Sci-Fi \n", + "Split 3 Horror,Thriller \n", + "Sing 4 Animation,Comedy,Family \n", + "\n", + " description \\\n", + "Title \n", + "Prometheus Following clues to the origin of mankind, a te... \n", + "Split Three girls are kidnapped by a man with a diag... \n", + "Sing In a city of humanoid animals, a hustling thea... \n", + "\n", + " director \\\n", + "Title \n", + "Prometheus Ridley Scott \n", + "Split M. Night Shyamalan \n", + "Sing Christophe Lourdelet \n", + "\n", + " actors year runtime \\\n", + "Title \n", + "Prometheus Noomi Rapace, Logan Marshall-Green, Michael Fa... 2012 124 \n", + "Split James McAvoy, Anya Taylor-Joy, Haley Lu Richar... 2016 117 \n", + "Sing Matthew McConaughey,Reese Witherspoon, Seth Ma... 2016 108 \n", + "\n", + " rating votes revenue_millions metascore \n", + "Title \n", + "Prometheus 7.0 485820 126.46 65.0 \n", + "Split 7.3 157606 138.12 62.0 \n", + "Sing 7.2 60545 270.32 59.0 " + ] + }, + "execution_count": 68, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "subset = movies.loc['Prometheus':'Sing']\n", + "subset" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "acbe3684", + "metadata": { + "id": "acbe3684", + "outputId": "2c020320-f369-4b24-a9b9-f4453777819b" + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
rankgenredescriptiondirectoractorsyearruntimeratingvotesrevenue_millionsmetascore
Title
Prometheus2Adventure,Mystery,Sci-FiFollowing clues to the origin of mankind, a te...Ridley ScottNoomi Rapace, Logan Marshall-Green, Michael Fa...20121247.0485820126.4665.0
Split3Horror,ThrillerThree girls are kidnapped by a man with a diag...M. Night ShyamalanJames McAvoy, Anya Taylor-Joy, Haley Lu Richar...20161177.3157606138.1262.0
Sing4Animation,Comedy,FamilyIn a city of humanoid animals, a hustling thea...Christophe LourdeletMatthew McConaughey,Reese Witherspoon, Seth Ma...20161087.260545270.3259.0
\n", + "
" + ], + "text/plain": [ + " rank genre \\\n", + "Title \n", + "Prometheus 2 Adventure,Mystery,Sci-Fi \n", + "Split 3 Horror,Thriller \n", + "Sing 4 Animation,Comedy,Family \n", + "\n", + " description \\\n", + "Title \n", + "Prometheus Following clues to the origin of mankind, a te... \n", + "Split Three girls are kidnapped by a man with a diag... \n", + "Sing In a city of humanoid animals, a hustling thea... \n", + "\n", + " director \\\n", + "Title \n", + "Prometheus Ridley Scott \n", + "Split M. Night Shyamalan \n", + "Sing Christophe Lourdelet \n", + "\n", + " actors year runtime \\\n", + "Title \n", + "Prometheus Noomi Rapace, Logan Marshall-Green, Michael Fa... 2012 124 \n", + "Split James McAvoy, Anya Taylor-Joy, Haley Lu Richar... 2016 117 \n", + "Sing Matthew McConaughey,Reese Witherspoon, Seth Ma... 2016 108 \n", + "\n", + " rating votes revenue_millions metascore \n", + "Title \n", + "Prometheus 7.0 485820 126.46 65.0 \n", + "Split 7.3 157606 138.12 62.0 \n", + "Sing 7.2 60545 270.32 59.0 " + ] + }, + "execution_count": 69, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Or using iloc\n", + "movies.iloc[1:4]" + ] + }, + { + "cell_type": "markdown", + "id": "b46f377b", + "metadata": { + "id": "b46f377b" + }, + "source": [ + "#### Conditional selection\n", + "What if we want to make a conditional selection? Lets say we want to filter our movies to show only films directed by Ridley Scott or films with a rating greater than or equal to 8.0.\n", + "\n", + "Here is an example of those conditions" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "39baaae8", + "metadata": { + "id": "39baaae8", + "outputId": "fb6300a0-8cb1-42a1-df22-ffcbfec695dd" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Title\n", + "Guardians of the Galaxy False\n", + "Prometheus True\n", + "Split False\n", + "Sing False\n", + "Suicide Squad False\n", + "Name: director, dtype: bool" + ] + }, + "execution_count": 72, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "condition = (movies['director'] == 'Ridley Scott')\n", + "condition.head()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a335c747", + "metadata": { + "id": "a335c747", + "outputId": "fb9fc2c3-b290-4d76-cfce-32f835c2e886" + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
rankgenredescriptiondirectoractorsyearruntimeratingvotesrevenue_millionsmetascore
Title
Prometheus2Adventure,Mystery,Sci-FiFollowing clues to the origin of mankind, a te...Ridley ScottNoomi Rapace, Logan Marshall-Green, Michael Fa...20121247.0485820126.4665.0
The Martian103Adventure,Drama,Sci-FiAn astronaut becomes stranded on Mars after hi...Ridley ScottMatt Damon, Jessica Chastain, Kristen Wiig, Ka...20151448.0556097228.4380.0
Robin Hood388Action,Adventure,DramaIn 12th century England, Robin and his band of...Ridley ScottRussell Crowe, Cate Blanchett, Matthew Macfady...20101406.7221117105.2253.0
American Gangster471Biography,Crime,DramaIn 1970s America, a detective works to bring d...Ridley ScottDenzel Washington, Russell Crowe, Chiwetel Eji...20071577.8337835130.1376.0
Exodus: Gods and Kings517Action,Adventure,DramaThe defiant leader Moses rises up against the ...Ridley ScottChristian Bale, Joel Edgerton, Ben Kingsley, S...20141506.013729965.0152.0
The Counselor522Crime,Drama,ThrillerA lawyer finds himself in over his head when h...Ridley ScottMichael Fassbender, Penélope Cruz, Cameron Dia...20131175.38492716.9748.0
A Good Year531Comedy,Drama,RomanceA British investment broker inherits his uncle...Ridley ScottRussell Crowe, Abbie Cornish, Albert Finney, M...20061176.9746747.4647.0
Body of Lies738Action,Drama,RomanceA CIA agent on the ground in Jordan hunts down...Ridley ScottLeonardo DiCaprio, Russell Crowe, Mark Strong,...20081287.118230539.3857.0
\n", + "
" + ], + "text/plain": [ + " rank genre \\\n", + "Title \n", + "Prometheus 2 Adventure,Mystery,Sci-Fi \n", + "The Martian 103 Adventure,Drama,Sci-Fi \n", + "Robin Hood 388 Action,Adventure,Drama \n", + "American Gangster 471 Biography,Crime,Drama \n", + "Exodus: Gods and Kings 517 Action,Adventure,Drama \n", + "The Counselor 522 Crime,Drama,Thriller \n", + "A Good Year 531 Comedy,Drama,Romance \n", + "Body of Lies 738 Action,Drama,Romance \n", + "\n", + " description \\\n", + "Title \n", + "Prometheus Following clues to the origin of mankind, a te... \n", + "The Martian An astronaut becomes stranded on Mars after hi... \n", + "Robin Hood In 12th century England, Robin and his band of... \n", + "American Gangster In 1970s America, a detective works to bring d... \n", + "Exodus: Gods and Kings The defiant leader Moses rises up against the ... \n", + "The Counselor A lawyer finds himself in over his head when h... \n", + "A Good Year A British investment broker inherits his uncle... \n", + "Body of Lies A CIA agent on the ground in Jordan hunts down... \n", + "\n", + " director \\\n", + "Title \n", + "Prometheus Ridley Scott \n", + "The Martian Ridley Scott \n", + "Robin Hood Ridley Scott \n", + "American Gangster Ridley Scott \n", + "Exodus: Gods and Kings Ridley Scott \n", + "The Counselor Ridley Scott \n", + "A Good Year Ridley Scott \n", + "Body of Lies Ridley Scott \n", + "\n", + " actors \\\n", + "Title \n", + "Prometheus Noomi Rapace, Logan Marshall-Green, Michael Fa... \n", + "The Martian Matt Damon, Jessica Chastain, Kristen Wiig, Ka... \n", + "Robin Hood Russell Crowe, Cate Blanchett, Matthew Macfady... \n", + "American Gangster Denzel Washington, Russell Crowe, Chiwetel Eji... \n", + "Exodus: Gods and Kings Christian Bale, Joel Edgerton, Ben Kingsley, S... \n", + "The Counselor Michael Fassbender, Penélope Cruz, Cameron Dia... \n", + "A Good Year Russell Crowe, Abbie Cornish, Albert Finney, M... \n", + "Body of Lies Leonardo DiCaprio, Russell Crowe, Mark Strong,... \n", + "\n", + " year runtime rating votes revenue_millions \\\n", + "Title \n", + "Prometheus 2012 124 7.0 485820 126.46 \n", + "The Martian 2015 144 8.0 556097 228.43 \n", + "Robin Hood 2010 140 6.7 221117 105.22 \n", + "American Gangster 2007 157 7.8 337835 130.13 \n", + "Exodus: Gods and Kings 2014 150 6.0 137299 65.01 \n", + "The Counselor 2013 117 5.3 84927 16.97 \n", + "A Good Year 2006 117 6.9 74674 7.46 \n", + "Body of Lies 2008 128 7.1 182305 39.38 \n", + "\n", + " metascore \n", + "Title \n", + "Prometheus 65.0 \n", + "The Martian 80.0 \n", + "Robin Hood 53.0 \n", + "American Gangster 76.0 \n", + "Exodus: Gods and Kings 52.0 \n", + "The Counselor 48.0 \n", + "A Good Year 47.0 \n", + "Body of Lies 57.0 " + ] + }, + "execution_count": 75, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Filter out movies that dont fulfill the condition\n", + "# Show only movies by this director\n", + "# Select movies df where movies director equals Ridley Scott\n", + "movies[movies['director'] == 'Ridley Scott']" + ] + }, + { + "cell_type": "markdown", + "id": "5f356256", + "metadata": { + "id": "5f356256" + }, + "source": [ + "We can create more conditions by using the logical operators `|` \"or\" and `&` \"and\".\n", + "\n", + "Lets filter to show movies only by Christopher Nolan or Ridley Scott" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "886a31c6", + "metadata": { + "id": "886a31c6", + "outputId": "af9f2318-744b-490a-a36f-e0a4d63f733d" + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
rankgenredescriptiondirectoractorsyearruntimeratingvotesrevenue_millionsmetascore
Title
Prometheus2Adventure,Mystery,Sci-FiFollowing clues to the origin of mankind, a te...Ridley ScottNoomi Rapace, Logan Marshall-Green, Michael Fa...20121247.0485820126.4665.0
Interstellar37Adventure,Drama,Sci-FiA team of explorers travel through a wormhole ...Christopher NolanMatthew McConaughey, Anne Hathaway, Jessica Ch...20141698.61047747187.9974.0
The Dark Knight55Action,Crime,DramaWhen the menace known as the Joker wreaks havo...Christopher NolanChristian Bale, Heath Ledger, Aaron Eckhart,Mi...20081529.01791916533.3282.0
The Prestige65Drama,Mystery,Sci-FiTwo stage magicians engage in competitive one-...Christopher NolanChristian Bale, Hugh Jackman, Scarlett Johanss...20061308.591315253.0866.0
Inception81Action,Adventure,Sci-FiA thief, who steals corporate secrets through ...Christopher NolanLeonardo DiCaprio, Joseph Gordon-Levitt, Ellen...20101488.81583625292.5774.0
The Martian103Adventure,Drama,Sci-FiAn astronaut becomes stranded on Mars after hi...Ridley ScottMatt Damon, Jessica Chastain, Kristen Wiig, Ka...20151448.0556097228.4380.0
The Dark Knight Rises125Action,ThrillerEight years after the Joker's reign of anarchy...Christopher NolanChristian Bale, Tom Hardy, Anne Hathaway,Gary ...20121648.51222645448.1378.0
Robin Hood388Action,Adventure,DramaIn 12th century England, Robin and his band of...Ridley ScottRussell Crowe, Cate Blanchett, Matthew Macfady...20101406.7221117105.2253.0
American Gangster471Biography,Crime,DramaIn 1970s America, a detective works to bring d...Ridley ScottDenzel Washington, Russell Crowe, Chiwetel Eji...20071577.8337835130.1376.0
Exodus: Gods and Kings517Action,Adventure,DramaThe defiant leader Moses rises up against the ...Ridley ScottChristian Bale, Joel Edgerton, Ben Kingsley, S...20141506.013729965.0152.0
The Counselor522Crime,Drama,ThrillerA lawyer finds himself in over his head when h...Ridley ScottMichael Fassbender, Penélope Cruz, Cameron Dia...20131175.38492716.9748.0
A Good Year531Comedy,Drama,RomanceA British investment broker inherits his uncle...Ridley ScottRussell Crowe, Abbie Cornish, Albert Finney, M...20061176.9746747.4647.0
Body of Lies738Action,Drama,RomanceA CIA agent on the ground in Jordan hunts down...Ridley ScottLeonardo DiCaprio, Russell Crowe, Mark Strong,...20081287.118230539.3857.0
\n", + "
" + ], + "text/plain": [ + " rank genre \\\n", + "Title \n", + "Prometheus 2 Adventure,Mystery,Sci-Fi \n", + "Interstellar 37 Adventure,Drama,Sci-Fi \n", + "The Dark Knight 55 Action,Crime,Drama \n", + "The Prestige 65 Drama,Mystery,Sci-Fi \n", + "Inception 81 Action,Adventure,Sci-Fi \n", + "The Martian 103 Adventure,Drama,Sci-Fi \n", + "The Dark Knight Rises 125 Action,Thriller \n", + "Robin Hood 388 Action,Adventure,Drama \n", + "American Gangster 471 Biography,Crime,Drama \n", + "Exodus: Gods and Kings 517 Action,Adventure,Drama \n", + "The Counselor 522 Crime,Drama,Thriller \n", + "A Good Year 531 Comedy,Drama,Romance \n", + "Body of Lies 738 Action,Drama,Romance \n", + "\n", + " description \\\n", + "Title \n", + "Prometheus Following clues to the origin of mankind, a te... \n", + "Interstellar A team of explorers travel through a wormhole ... \n", + "The Dark Knight When the menace known as the Joker wreaks havo... \n", + "The Prestige Two stage magicians engage in competitive one-... \n", + "Inception A thief, who steals corporate secrets through ... \n", + "The Martian An astronaut becomes stranded on Mars after hi... \n", + "The Dark Knight Rises Eight years after the Joker's reign of anarchy... \n", + "Robin Hood In 12th century England, Robin and his band of... \n", + "American Gangster In 1970s America, a detective works to bring d... \n", + "Exodus: Gods and Kings The defiant leader Moses rises up against the ... \n", + "The Counselor A lawyer finds himself in over his head when h... \n", + "A Good Year A British investment broker inherits his uncle... \n", + "Body of Lies A CIA agent on the ground in Jordan hunts down... \n", + "\n", + " director \\\n", + "Title \n", + "Prometheus Ridley Scott \n", + "Interstellar Christopher Nolan \n", + "The Dark Knight Christopher Nolan \n", + "The Prestige Christopher Nolan \n", + "Inception Christopher Nolan \n", + "The Martian Ridley Scott \n", + "The Dark Knight Rises Christopher Nolan \n", + "Robin Hood Ridley Scott \n", + "American Gangster Ridley Scott \n", + "Exodus: Gods and Kings Ridley Scott \n", + "The Counselor Ridley Scott \n", + "A Good Year Ridley Scott \n", + "Body of Lies Ridley Scott \n", + "\n", + " actors \\\n", + "Title \n", + "Prometheus Noomi Rapace, Logan Marshall-Green, Michael Fa... \n", + "Interstellar Matthew McConaughey, Anne Hathaway, Jessica Ch... \n", + "The Dark Knight Christian Bale, Heath Ledger, Aaron Eckhart,Mi... \n", + "The Prestige Christian Bale, Hugh Jackman, Scarlett Johanss... \n", + "Inception Leonardo DiCaprio, Joseph Gordon-Levitt, Ellen... \n", + "The Martian Matt Damon, Jessica Chastain, Kristen Wiig, Ka... \n", + "The Dark Knight Rises Christian Bale, Tom Hardy, Anne Hathaway,Gary ... \n", + "Robin Hood Russell Crowe, Cate Blanchett, Matthew Macfady... \n", + "American Gangster Denzel Washington, Russell Crowe, Chiwetel Eji... \n", + "Exodus: Gods and Kings Christian Bale, Joel Edgerton, Ben Kingsley, S... \n", + "The Counselor Michael Fassbender, Penélope Cruz, Cameron Dia... \n", + "A Good Year Russell Crowe, Abbie Cornish, Albert Finney, M... \n", + "Body of Lies Leonardo DiCaprio, Russell Crowe, Mark Strong,... \n", + "\n", + " year runtime rating votes revenue_millions \\\n", + "Title \n", + "Prometheus 2012 124 7.0 485820 126.46 \n", + "Interstellar 2014 169 8.6 1047747 187.99 \n", + "The Dark Knight 2008 152 9.0 1791916 533.32 \n", + "The Prestige 2006 130 8.5 913152 53.08 \n", + "Inception 2010 148 8.8 1583625 292.57 \n", + "The Martian 2015 144 8.0 556097 228.43 \n", + "The Dark Knight Rises 2012 164 8.5 1222645 448.13 \n", + "Robin Hood 2010 140 6.7 221117 105.22 \n", + "American Gangster 2007 157 7.8 337835 130.13 \n", + "Exodus: Gods and Kings 2014 150 6.0 137299 65.01 \n", + "The Counselor 2013 117 5.3 84927 16.97 \n", + "A Good Year 2006 117 6.9 74674 7.46 \n", + "Body of Lies 2008 128 7.1 182305 39.38 \n", + "\n", + " metascore \n", + "Title \n", + "Prometheus 65.0 \n", + "Interstellar 74.0 \n", + "The Dark Knight 82.0 \n", + "The Prestige 66.0 \n", + "Inception 74.0 \n", + "The Martian 80.0 \n", + "The Dark Knight Rises 78.0 \n", + "Robin Hood 53.0 \n", + "American Gangster 76.0 \n", + "Exodus: Gods and Kings 52.0 \n", + "The Counselor 48.0 \n", + "A Good Year 47.0 \n", + "Body of Lies 57.0 " + ] + }, + "execution_count": 76, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "movies[(movies['director'] == 'Christopher Nolan') | (movies['director'] == 'Ridley Scott')]" + ] + }, + { + "cell_type": "markdown", + "id": "c6ee1c55", + "metadata": { + "id": "c6ee1c55" + }, + "source": [ + "We could also use the `isin()` method to make this more concise" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ec9c5e7e", + "metadata": { + "id": "ec9c5e7e", + "outputId": "3a1ff1bb-fe8c-4304-f25e-6cddf3ad1207" + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
rankgenredescriptiondirectoractorsyearruntimeratingvotesrevenue_millionsmetascore
Title
Prometheus2Adventure,Mystery,Sci-FiFollowing clues to the origin of mankind, a te...Ridley ScottNoomi Rapace, Logan Marshall-Green, Michael Fa...20121247.0485820126.4665.0
Interstellar37Adventure,Drama,Sci-FiA team of explorers travel through a wormhole ...Christopher NolanMatthew McConaughey, Anne Hathaway, Jessica Ch...20141698.61047747187.9974.0
The Dark Knight55Action,Crime,DramaWhen the menace known as the Joker wreaks havo...Christopher NolanChristian Bale, Heath Ledger, Aaron Eckhart,Mi...20081529.01791916533.3282.0
The Prestige65Drama,Mystery,Sci-FiTwo stage magicians engage in competitive one-...Christopher NolanChristian Bale, Hugh Jackman, Scarlett Johanss...20061308.591315253.0866.0
Inception81Action,Adventure,Sci-FiA thief, who steals corporate secrets through ...Christopher NolanLeonardo DiCaprio, Joseph Gordon-Levitt, Ellen...20101488.81583625292.5774.0
The Martian103Adventure,Drama,Sci-FiAn astronaut becomes stranded on Mars after hi...Ridley ScottMatt Damon, Jessica Chastain, Kristen Wiig, Ka...20151448.0556097228.4380.0
The Dark Knight Rises125Action,ThrillerEight years after the Joker's reign of anarchy...Christopher NolanChristian Bale, Tom Hardy, Anne Hathaway,Gary ...20121648.51222645448.1378.0
Robin Hood388Action,Adventure,DramaIn 12th century England, Robin and his band of...Ridley ScottRussell Crowe, Cate Blanchett, Matthew Macfady...20101406.7221117105.2253.0
American Gangster471Biography,Crime,DramaIn 1970s America, a detective works to bring d...Ridley ScottDenzel Washington, Russell Crowe, Chiwetel Eji...20071577.8337835130.1376.0
Exodus: Gods and Kings517Action,Adventure,DramaThe defiant leader Moses rises up against the ...Ridley ScottChristian Bale, Joel Edgerton, Ben Kingsley, S...20141506.013729965.0152.0
The Counselor522Crime,Drama,ThrillerA lawyer finds himself in over his head when h...Ridley ScottMichael Fassbender, Penélope Cruz, Cameron Dia...20131175.38492716.9748.0
A Good Year531Comedy,Drama,RomanceA British investment broker inherits his uncle...Ridley ScottRussell Crowe, Abbie Cornish, Albert Finney, M...20061176.9746747.4647.0
Body of Lies738Action,Drama,RomanceA CIA agent on the ground in Jordan hunts down...Ridley ScottLeonardo DiCaprio, Russell Crowe, Mark Strong,...20081287.118230539.3857.0
\n", + "
" + ], + "text/plain": [ + " rank genre \\\n", + "Title \n", + "Prometheus 2 Adventure,Mystery,Sci-Fi \n", + "Interstellar 37 Adventure,Drama,Sci-Fi \n", + "The Dark Knight 55 Action,Crime,Drama \n", + "The Prestige 65 Drama,Mystery,Sci-Fi \n", + "Inception 81 Action,Adventure,Sci-Fi \n", + "The Martian 103 Adventure,Drama,Sci-Fi \n", + "The Dark Knight Rises 125 Action,Thriller \n", + "Robin Hood 388 Action,Adventure,Drama \n", + "American Gangster 471 Biography,Crime,Drama \n", + "Exodus: Gods and Kings 517 Action,Adventure,Drama \n", + "The Counselor 522 Crime,Drama,Thriller \n", + "A Good Year 531 Comedy,Drama,Romance \n", + "Body of Lies 738 Action,Drama,Romance \n", + "\n", + " description \\\n", + "Title \n", + "Prometheus Following clues to the origin of mankind, a te... \n", + "Interstellar A team of explorers travel through a wormhole ... \n", + "The Dark Knight When the menace known as the Joker wreaks havo... \n", + "The Prestige Two stage magicians engage in competitive one-... \n", + "Inception A thief, who steals corporate secrets through ... \n", + "The Martian An astronaut becomes stranded on Mars after hi... \n", + "The Dark Knight Rises Eight years after the Joker's reign of anarchy... \n", + "Robin Hood In 12th century England, Robin and his band of... \n", + "American Gangster In 1970s America, a detective works to bring d... \n", + "Exodus: Gods and Kings The defiant leader Moses rises up against the ... \n", + "The Counselor A lawyer finds himself in over his head when h... \n", + "A Good Year A British investment broker inherits his uncle... \n", + "Body of Lies A CIA agent on the ground in Jordan hunts down... \n", + "\n", + " director \\\n", + "Title \n", + "Prometheus Ridley Scott \n", + "Interstellar Christopher Nolan \n", + "The Dark Knight Christopher Nolan \n", + "The Prestige Christopher Nolan \n", + "Inception Christopher Nolan \n", + "The Martian Ridley Scott \n", + "The Dark Knight Rises Christopher Nolan \n", + "Robin Hood Ridley Scott \n", + "American Gangster Ridley Scott \n", + "Exodus: Gods and Kings Ridley Scott \n", + "The Counselor Ridley Scott \n", + "A Good Year Ridley Scott \n", + "Body of Lies Ridley Scott \n", + "\n", + " actors \\\n", + "Title \n", + "Prometheus Noomi Rapace, Logan Marshall-Green, Michael Fa... \n", + "Interstellar Matthew McConaughey, Anne Hathaway, Jessica Ch... \n", + "The Dark Knight Christian Bale, Heath Ledger, Aaron Eckhart,Mi... \n", + "The Prestige Christian Bale, Hugh Jackman, Scarlett Johanss... \n", + "Inception Leonardo DiCaprio, Joseph Gordon-Levitt, Ellen... \n", + "The Martian Matt Damon, Jessica Chastain, Kristen Wiig, Ka... \n", + "The Dark Knight Rises Christian Bale, Tom Hardy, Anne Hathaway,Gary ... \n", + "Robin Hood Russell Crowe, Cate Blanchett, Matthew Macfady... \n", + "American Gangster Denzel Washington, Russell Crowe, Chiwetel Eji... \n", + "Exodus: Gods and Kings Christian Bale, Joel Edgerton, Ben Kingsley, S... \n", + "The Counselor Michael Fassbender, Penélope Cruz, Cameron Dia... \n", + "A Good Year Russell Crowe, Abbie Cornish, Albert Finney, M... \n", + "Body of Lies Leonardo DiCaprio, Russell Crowe, Mark Strong,... \n", + "\n", + " year runtime rating votes revenue_millions \\\n", + "Title \n", + "Prometheus 2012 124 7.0 485820 126.46 \n", + "Interstellar 2014 169 8.6 1047747 187.99 \n", + "The Dark Knight 2008 152 9.0 1791916 533.32 \n", + "The Prestige 2006 130 8.5 913152 53.08 \n", + "Inception 2010 148 8.8 1583625 292.57 \n", + "The Martian 2015 144 8.0 556097 228.43 \n", + "The Dark Knight Rises 2012 164 8.5 1222645 448.13 \n", + "Robin Hood 2010 140 6.7 221117 105.22 \n", + "American Gangster 2007 157 7.8 337835 130.13 \n", + "Exodus: Gods and Kings 2014 150 6.0 137299 65.01 \n", + "The Counselor 2013 117 5.3 84927 16.97 \n", + "A Good Year 2006 117 6.9 74674 7.46 \n", + "Body of Lies 2008 128 7.1 182305 39.38 \n", + "\n", + " metascore \n", + "Title \n", + "Prometheus 65.0 \n", + "Interstellar 74.0 \n", + "The Dark Knight 82.0 \n", + "The Prestige 66.0 \n", + "Inception 74.0 \n", + "The Martian 80.0 \n", + "The Dark Knight Rises 78.0 \n", + "Robin Hood 53.0 \n", + "American Gangster 76.0 \n", + "Exodus: Gods and Kings 52.0 \n", + "The Counselor 48.0 \n", + "A Good Year 47.0 \n", + "Body of Lies 57.0 " + ] + }, + "execution_count": 78, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "movies[movies['director'].isin(['Christopher Nolan', 'Ridley Scott'])]" + ] + }, + { + "cell_type": "markdown", + "id": "ebf30bb6", + "metadata": { + "id": "ebf30bb6" + }, + "source": [ + "Can you get all movies with the following conditions?\n", + "- Released between 2005 and 2010\n", + "- Have a rating above 8.0\n", + "- Made below the 25th percentile in revenue" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1d263a03", + "metadata": { + "id": "1d263a03", + "outputId": "ee0c7709-cef6-4d7a-8788-be5781bbe70d" + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
rankgenredescriptiondirectoractorsyearruntimeratingvotesrevenue_millionsmetascore
Title
3 Idiots431Comedy,DramaTwo friends are searching for their long lost ...Rajkumar HiraniAamir Khan, Madhavan, Mona Singh, Sharman Joshi20091708.42387896.5267.0
The Lives of Others477Drama,ThrillerIn 1984 East Berlin, an agent of the secret po...Florian Henckel von DonnersmarckUlrich Mühe, Martina Gedeck,Sebastian Koch, Ul...20061378.527810311.2889.0
Incendies714Drama,Mystery,WarTwins journey to the Middle East to discover t...Denis VilleneuveLubna Azabal, Mélissa Désormeaux-Poulin, Maxim...20101318.2928636.8680.0
Taare Zameen Par992Drama,Family,MusicAn eight-year-old boy is thought to be a lazy ...Aamir KhanDarsheel Safary, Aamir Khan, Tanay Chheda, Sac...20071658.51026971.2042.0
\n", + "
" + ], + "text/plain": [ + " rank genre \\\n", + "Title \n", + "3 Idiots 431 Comedy,Drama \n", + "The Lives of Others 477 Drama,Thriller \n", + "Incendies 714 Drama,Mystery,War \n", + "Taare Zameen Par 992 Drama,Family,Music \n", + "\n", + " description \\\n", + "Title \n", + "3 Idiots Two friends are searching for their long lost ... \n", + "The Lives of Others In 1984 East Berlin, an agent of the secret po... \n", + "Incendies Twins journey to the Middle East to discover t... \n", + "Taare Zameen Par An eight-year-old boy is thought to be a lazy ... \n", + "\n", + " director \\\n", + "Title \n", + "3 Idiots Rajkumar Hirani \n", + "The Lives of Others Florian Henckel von Donnersmarck \n", + "Incendies Denis Villeneuve \n", + "Taare Zameen Par Aamir Khan \n", + "\n", + " actors year \\\n", + "Title \n", + "3 Idiots Aamir Khan, Madhavan, Mona Singh, Sharman Joshi 2009 \n", + "The Lives of Others Ulrich Mühe, Martina Gedeck,Sebastian Koch, Ul... 2006 \n", + "Incendies Lubna Azabal, Mélissa Désormeaux-Poulin, Maxim... 2010 \n", + "Taare Zameen Par Darsheel Safary, Aamir Khan, Tanay Chheda, Sac... 2007 \n", + "\n", + " runtime rating votes revenue_millions metascore \n", + "Title \n", + "3 Idiots 170 8.4 238789 6.52 67.0 \n", + "The Lives of Others 137 8.5 278103 11.28 89.0 \n", + "Incendies 131 8.2 92863 6.86 80.0 \n", + "Taare Zameen Par 165 8.5 102697 1.20 42.0 " + ] + }, + "execution_count": 79, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "movies[((movies['year'] >= 2005) & (movies['year'] <= 2010)) # get movies between years 2005 and 2010\n", + " & (movies['rating'] > 8.0) # get movies with rating greater than 8.0\n", + " & (movies['revenue_millions'] < movies['revenue_millions'].quantile(0.25))] # get movies with revenue less than 25th percentile" + ] + }, + { + "cell_type": "markdown", + "id": "a9613f65", + "metadata": { + "id": "a9613f65" + }, + "source": [ + "If you want to check this, you can remember we used `.describe()` to get the 25th percentile of revenue and it was about 17.4, we can access the value directly by using the `quantile()` method with a float of 0.25." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "023a7cae", + "metadata": { + "id": "023a7cae", + "outputId": "61e68c26-4886-42bd-a7c3-806f1a415f13" + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
rankyearruntimeratingvotesrevenue_millionsmetascore
count1000.0000001000.0000001000.0000001000.0000001.000000e+031000.000000936.000000
mean500.5000002012.783000113.1720006.7232001.698083e+0582.95637658.985043
std288.8194363.20596218.8109080.9454291.887626e+0596.41204317.194757
min1.0000002006.00000066.0000001.9000006.100000e+010.00000011.000000
25%250.7500002010.000000100.0000006.2000003.630900e+0417.44250047.000000
50%500.5000002014.000000111.0000006.8000001.107990e+0560.37500059.500000
75%750.2500002016.000000123.0000007.4000002.399098e+0599.17750072.000000
max1000.0000002016.000000191.0000009.0000001.791916e+06936.630000100.000000
\n", + "
" + ], + "text/plain": [ + " rank year runtime rating votes \\\n", + "count 1000.000000 1000.000000 1000.000000 1000.000000 1.000000e+03 \n", + "mean 500.500000 2012.783000 113.172000 6.723200 1.698083e+05 \n", + "std 288.819436 3.205962 18.810908 0.945429 1.887626e+05 \n", + "min 1.000000 2006.000000 66.000000 1.900000 6.100000e+01 \n", + "25% 250.750000 2010.000000 100.000000 6.200000 3.630900e+04 \n", + "50% 500.500000 2014.000000 111.000000 6.800000 1.107990e+05 \n", + "75% 750.250000 2016.000000 123.000000 7.400000 2.399098e+05 \n", + "max 1000.000000 2016.000000 191.000000 9.000000 1.791916e+06 \n", + "\n", + " revenue_millions metascore \n", + "count 1000.000000 936.000000 \n", + "mean 82.956376 58.985043 \n", + "std 96.412043 17.194757 \n", + "min 0.000000 11.000000 \n", + "25% 17.442500 47.000000 \n", + "50% 60.375000 59.500000 \n", + "75% 99.177500 72.000000 \n", + "max 936.630000 100.000000 " + ] + }, + "execution_count": 80, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "movies.describe()" + ] + }, + { + "cell_type": "markdown", + "id": "117dac7b", + "metadata": { + "id": "117dac7b" + }, + "source": [ + "### Finally a brief look at functions\n", + "We will cover functions more in detail in a future lesson, for now you should know that iterating over a DataFrame as it was a list, for example with a for loop, is very slow. An efficient alternative to this is using the `apply()` function. This method passes every value in the DataFrame through a function using vectorization.\n", + "\n", + "Vectorization is a style of programming where operations are applied to whole arrays instead of individual elements, this makes it really efficient.\n", + "\n", + "Lets create a function to rate our movies. The following code creates a function that will return \"good\" if the rating is equals or greater than 8.0, else it will return false. This rating will be created as a new column in our DataFrame." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ebb6625a", + "metadata": { + "id": "ebb6625a" + }, + "outputs": [], + "source": [ + "# Create our function\n", + "# Will return \"good\" if x is greater or equals to 8.0\n", + "# Will return \"bad\" if x is less than 8.0\n", + "\n", + "# def is a keyword to define functions. We will see more of this in a future lesson\n", + "def rating_function(x):\n", + " if x >= 8.0:\n", + " return \"good\"\n", + " else:\n", + " return \"bad\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3e9e6a1b", + "metadata": { + "id": "3e9e6a1b", + "outputId": "62e5e120-c814-49e7-f895-c3b648814add" + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
rankgenredescriptiondirectoractorsyearruntimeratingvotesrevenue_millionsmetascorerating_category
Title
Guardians of the Galaxy1Action,Adventure,Sci-FiA group of intergalactic criminals are forced ...James GunnChris Pratt, Vin Diesel, Bradley Cooper, Zoe S...20141218.1757074333.1376.0good
Prometheus2Adventure,Mystery,Sci-FiFollowing clues to the origin of mankind, a te...Ridley ScottNoomi Rapace, Logan Marshall-Green, Michael Fa...20121247.0485820126.4665.0bad
Split3Horror,ThrillerThree girls are kidnapped by a man with a diag...M. Night ShyamalanJames McAvoy, Anya Taylor-Joy, Haley Lu Richar...20161177.3157606138.1262.0bad
Sing4Animation,Comedy,FamilyIn a city of humanoid animals, a hustling thea...Christophe LourdeletMatthew McConaughey,Reese Witherspoon, Seth Ma...20161087.260545270.3259.0bad
Suicide Squad5Action,Adventure,FantasyA secret government agency recruits some of th...David AyerWill Smith, Jared Leto, Margot Robbie, Viola D...20161236.2393727325.0240.0bad
\n", + "
" + ], + "text/plain": [ + " rank genre \\\n", + "Title \n", + "Guardians of the Galaxy 1 Action,Adventure,Sci-Fi \n", + "Prometheus 2 Adventure,Mystery,Sci-Fi \n", + "Split 3 Horror,Thriller \n", + "Sing 4 Animation,Comedy,Family \n", + "Suicide Squad 5 Action,Adventure,Fantasy \n", + "\n", + " description \\\n", + "Title \n", + "Guardians of the Galaxy A group of intergalactic criminals are forced ... \n", + "Prometheus Following clues to the origin of mankind, a te... \n", + "Split Three girls are kidnapped by a man with a diag... \n", + "Sing In a city of humanoid animals, a hustling thea... \n", + "Suicide Squad A secret government agency recruits some of th... \n", + "\n", + " director \\\n", + "Title \n", + "Guardians of the Galaxy James Gunn \n", + "Prometheus Ridley Scott \n", + "Split M. Night Shyamalan \n", + "Sing Christophe Lourdelet \n", + "Suicide Squad David Ayer \n", + "\n", + " actors \\\n", + "Title \n", + "Guardians of the Galaxy Chris Pratt, Vin Diesel, Bradley Cooper, Zoe S... \n", + "Prometheus Noomi Rapace, Logan Marshall-Green, Michael Fa... \n", + "Split James McAvoy, Anya Taylor-Joy, Haley Lu Richar... \n", + "Sing Matthew McConaughey,Reese Witherspoon, Seth Ma... \n", + "Suicide Squad Will Smith, Jared Leto, Margot Robbie, Viola D... \n", + "\n", + " year runtime rating votes revenue_millions \\\n", + "Title \n", + "Guardians of the Galaxy 2014 121 8.1 757074 333.13 \n", + "Prometheus 2012 124 7.0 485820 126.46 \n", + "Split 2016 117 7.3 157606 138.12 \n", + "Sing 2016 108 7.2 60545 270.32 \n", + "Suicide Squad 2016 123 6.2 393727 325.02 \n", + "\n", + " metascore rating_category \n", + "Title \n", + "Guardians of the Galaxy 76.0 good \n", + "Prometheus 65.0 bad \n", + "Split 62.0 bad \n", + "Sing 59.0 bad \n", + "Suicide Squad 40.0 bad " + ] + }, + "execution_count": 82, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "movies['rating_category'] = movies['rating'].apply(rating_function)\n", + "movies.head()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "dff3917c", + "metadata": { + "id": "dff3917c" + }, + "outputs": [], + "source": [ + "subset.to_csv('subset.csv') # this is how you save a selection to csv\n", + "# the code above will create a new csv called 'subset.csv'" + ] + }, + { + "cell_type": "markdown", + "id": "77d43f68", + "metadata": { + "id": "77d43f68" + }, + "source": [ + "### Optional Exercise\n", + "- Select the movies with ratings between 6.0 and 8.5 released between 2010 and 2016. Save this selection to a variable.\n", + "- Select movies with runtime greater than 120 and rating greater or equal to 8.0. Save this selection to a variable.\n", + "- Save these two selections to two `.csv` files using the `df.to_csv('name_of_csv.csv')` function. Where 'df' is the name of your DataFrame\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3996f85f", + "metadata": { + "id": "3996f85f" + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.10.4" + }, + "colab": { + "provenance": [], + "include_colab_link": true + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} \ No newline at end of file From 06cde4a4d3602bbabeeb258bb3cbf7e01e64bdb8 Mon Sep 17 00:00:00 2001 From: awells-nrao Date: Mon, 3 Jun 2024 19:31:47 -0400 Subject: [PATCH 3/7] Homework 1 answers --- Lesson_1_Python_basics.ipynb | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/Lesson_1_Python_basics.ipynb b/Lesson_1_Python_basics.ipynb index f6099a8..cfc49c8 100644 --- a/Lesson_1_Python_basics.ipynb +++ b/Lesson_1_Python_basics.ipynb @@ -43,7 +43,7 @@ } }, "id": "puGBUv0gVvGQ", - "execution_count": 2, + "execution_count": null, "outputs": [ { "output_type": "stream", @@ -3758,6 +3758,38 @@ " - a / b" ] }, + { + "cell_type": "code", + "source": [ + "a = input('Enter your value ')\n", + "b = input('Enter your value ')\n", + "print(a)\n", + "print(b)\n", + "print(\"A+B: \", a + b)" + ], + "metadata": { + "id": "9fynGCVibmMW", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "5dc75c0d-2eb0-43ee-b0eb-3e5130faa686" + }, + "id": "9fynGCVibmMW", + "execution_count": 5, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter your value 2\n", + "Enter your value 3\n", + "2\n", + "3\n", + "A+B: 23\n" + ] + } + ] + }, { "cell_type": "markdown", "id": "c9cf9f05", From 9d7daebe8361120eaeabbe32ef0f79c69332114c Mon Sep 17 00:00:00 2001 From: aliawofford9317 Date: Mon, 3 Jun 2024 19:39:32 -0400 Subject: [PATCH 4/7] See the optional assignment for solution --- Lesson_1_Python_basics_hw1_corrected.ipynb | 3893 ++++++++++++++++++++ 1 file changed, 3893 insertions(+) create mode 100644 Lesson_1_Python_basics_hw1_corrected.ipynb diff --git a/Lesson_1_Python_basics_hw1_corrected.ipynb b/Lesson_1_Python_basics_hw1_corrected.ipynb new file mode 100644 index 0000000..9c7d11b --- /dev/null +++ b/Lesson_1_Python_basics_hw1_corrected.ipynb @@ -0,0 +1,3893 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "\"Open" + ] + }, + { + "cell_type": "markdown", + "id": "65297d5d", + "metadata": { + "id": "65297d5d" + }, + "source": [ + "## Lesson 1 - Python basics\n", + "In this lesson we will cover:\n", + "- What is a variable and how to declare it\n", + "- Python basic operators\n", + "- Data types in Python\n", + "- Lists\n", + "- Dictionaries\n", + "- Tuples\n", + "- Optional exercises\n", + "\n", + "All along the lessons you will see comments on the code starting with `#`, these are comment lines and are used to describe what the code is doing" + ] + }, + { + "cell_type": "code", + "source": [ + "print(\"Test1\")" + ], + "metadata": { + "id": "puGBUv0gVvGQ", + "outputId": "8a52b530-a040-4961-d76f-ffd2650f8e4a", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "id": "puGBUv0gVvGQ", + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Test1\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "id": "d927a42b", + "metadata": { + "id": "d927a42b" + }, + "source": [ + "## What is a variable?\n", + "\n", + "Imagine variables as containers to store data values.\n", + "We can asign values to these containers using the `=` sign." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d273a7b9", + "metadata": { + "id": "d273a7b9" + }, + "outputs": [], + "source": [ + "# Here we assign a to 4, and A to \"Python\"\n", + "a = 4\n", + "A = \"Python\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c6a73ac0", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "c6a73ac0", + "outputId": "3fe8ec58-4bcf-4e4e-f61c-247289a70216" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "4\n", + "Python\n" + ] + } + ], + "source": [ + "# Print the variables\n", + "print(a)\n", + "print(A)" + ] + }, + { + "cell_type": "markdown", + "id": "a800cba9", + "metadata": { + "id": "a800cba9" + }, + "source": [ + "From the example above we can see that variables are case sensitive, so a variable named `Variable` will be different from another one called `variable`.\n", + "Variable names can consist of uppercase and lowercase letters, digits and underscore. The first character of a variable name cannot be a digit" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "36e9c3df", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "36e9c3df", + "outputId": "a3363fc3-15f7-4a52-c29c-ab1b9daef5ec" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "55\n", + "49\n" + ] + } + ], + "source": [ + "Variable = 55\n", + "variable = 49\n", + "print(Variable)\n", + "print(variable)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3ade135f", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 108 + }, + "id": "3ade135f", + "outputId": "450fd7d2-13a5-4ecf-ef05-44700cb55c67" + }, + "outputs": [ + { + "output_type": "error", + "ename": "SyntaxError", + "evalue": "invalid decimal literal (, line 2)", + "traceback": [ + "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m2\u001b[0m\n\u001b[0;31m 1_variable = \"Hi\" #invalid variable name, will throw an error\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid decimal literal\n" + ] + } + ], + "source": [ + "a_variable = \"Hello\"\n", + "1_variable = \"Hi\" #invalid variable name, will throw an error" + ] + }, + { + "cell_type": "markdown", + "id": "c31ad403", + "metadata": { + "id": "c31ad403" + }, + "source": [ + "### Reserved words in Python\n", + "For variable names:\n", + "- We can use number, letters and underscores\n", + "- Variables can include numbers, but not as the first character\n", + "- There are some reserverd words in Python that cannot be used as variable names:\n", + "\n", + "`False\tdef\tif\traise\n", + "None\tdel\timport\treturn\n", + "True\telif\tin\ttry\n", + "and\telse\tis\twhile\n", + "as\texcept\tlambda\twith\n", + "assert\tfinally\tnonlocal\tyield\n", + "break\tfor\tnot\n", + "class\tfrom\tor\n", + "continue\tglobal\tpass`\n", + "\n", + "You can see this list any time by typing `help(\"keywords\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9e951328", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "9e951328", + "outputId": "f8def180-c2e2-4632-b9cd-932a72f7f32d" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "\n", + "Here is a list of the Python keywords. Enter any keyword to get more help.\n", + "\n", + "False class from or\n", + "None continue global pass\n", + "True def if raise\n", + "and del import return\n", + "as elif in try\n", + "assert else is while\n", + "async except lambda with\n", + "await finally nonlocal yield\n", + "break for not \n", + "\n" + ] + } + ], + "source": [ + "help(\"keywords\")" + ] + }, + { + "cell_type": "markdown", + "id": "2ecace5e", + "metadata": { + "id": "2ecace5e" + }, + "source": [ + "## Operators\n", + "We now know how to create variables. Lets explore the basic operators in Python. Operators are special symbols in Python that carry out arithmetic or logical computations\n", + "\n", + "Arithmetic operators are used to perform mathematical operations like addition, substraction, multiplication, etc\n", + "\n", + "| Symbol | Operator | Description |\n", + "|--------|----------|-------------|\n", + "| + | Addition | Add two operands |\n", + "| - | Substraction | Substract two operands |\n", + "| * | Multiplication | Multiply two operands |\n", + "| / | Division | Divide two operands |\n", + "| % | Modulus | Remainder of the division of left operand by the right |\n", + "| // | Floor division | Division that results into whole number adjusted to the left in the number line |\n", + "| ** | Exponent | Left operand raised to the power of right |" + ] + }, + { + "cell_type": "markdown", + "id": "eb521890", + "metadata": { + "id": "eb521890" + }, + "source": [ + "## Types\n", + "Each variable can be of a particular type, but this type does not needs to be declared when you create the variable, and can even be changed after they have been set" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f1ce33e5", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "f1ce33e5", + "outputId": "ca5c1079-8576-4342-cceb-50f1a67709cc" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "100\n" + ] + } + ], + "source": [ + "n = 100 #variable of type integer\n", + "print(n)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9ba75bd0", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "9ba75bd0", + "outputId": "494fa8ef-d7ad-4e8a-cd88-c948331e7542" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Now Im a different variable, of type string\n" + ] + } + ], + "source": [ + "n = \"Now Im a different variable, of type string\"\n", + "print(n)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "643fc42f", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "643fc42f", + "outputId": "9129e0fb-0a8c-4c8a-bb8d-a45a20422543" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "300 300 300\n" + ] + } + ], + "source": [ + "#Python also allows for chained variable assignments\n", + "a = b = c = 300\n", + "print(a, b, c)" + ] + }, + { + "cell_type": "markdown", + "id": "e4e0ed01", + "metadata": { + "id": "e4e0ed01" + }, + "source": [ + "Python is a highly object-oriented language. Every itme of data in Python is an object of a specific type or class. Consider the following code. Python does the following:\n", + "- Create a integer object\n", + "- Gives a value of 300\n", + "- Displays it to the console" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a62138f0", + "metadata": { + "id": "a62138f0", + "outputId": "d57bdbee-7c83-4a42-e0ff-e21ef3ec715c" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "100\n" + ] + } + ], + "source": [ + "print(100)" + ] + }, + { + "cell_type": "markdown", + "id": "85e49441", + "metadata": { + "id": "85e49441" + }, + "source": [ + "To see the actual integer object you can use the `type()` function" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5cbda040", + "metadata": { + "id": "5cbda040", + "outputId": "2b3f1dc6-936d-41da-884f-db37075efc59" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "int" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(100)" + ] + }, + { + "cell_type": "markdown", + "id": "64edd62c", + "metadata": { + "id": "64edd62c" + }, + "source": [ + "## Basic Data Types\n", + "- Numeric (integer numbers, floating point)\n", + "- String (letters, characters)\n", + "- Boolean (True or False, 0 or 1)" + ] + }, + { + "cell_type": "markdown", + "id": "2f8cd151", + "metadata": { + "id": "2f8cd151" + }, + "source": [ + "### Integers\n", + "An integer value can be as big as you need it to be, this is only constrained by the amount of memory in the system. Python will interpret a sequence of decimal digits without any prefix to be a decimal number:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "46402c16", + "metadata": { + "id": "46402c16", + "outputId": "7ef3a609-71c6-4020-e4ab-cb78e9035ee5" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789\n" + ] + } + ], + "source": [ + "print(123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2698b8d4", + "metadata": { + "id": "2698b8d4", + "outputId": "f1a10ed0-1343-47c1-ca53-dcf26c371a83" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10\n" + ] + } + ], + "source": [ + "print(10)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "fa03d0d3", + "metadata": { + "id": "fa03d0d3", + "outputId": "cb0ec153-90f9-4109-b61b-662fc8206e3c" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n", + "8\n", + "16\n" + ] + } + ], + "source": [ + "#Binary representation\n", + "#use 0b prefix\n", + "print(0b10)\n", + "\n", + "#Octal representation\n", + "#use 0O prefix\n", + "print(0O10)\n", + "\n", + "#Hexadecimal representation\n", + "#use 0X prefix\n", + "print(0X10)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "36a83fc0", + "metadata": { + "id": "36a83fc0", + "outputId": "e349ff1f-c9a1-4ae3-8081-b5e359c39199" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "int" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#The data type is still integer\n", + "type(0b10)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7e26ec9e", + "metadata": { + "id": "7e26ec9e", + "outputId": "69662ac3-3399-4287-f505-b81c4a468c75" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "int" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(0O10)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3e11bb06", + "metadata": { + "id": "3e11bb06", + "outputId": "d46634e0-4f56-40af-83d6-ce760e363bc5" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "int" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(0X10)" + ] + }, + { + "cell_type": "markdown", + "id": "9fe061d9", + "metadata": { + "id": "9fe061d9" + }, + "source": [ + "### Floating point numbers\n", + "The `float` type designates a floating-point number, which specifies a decimal point. This can also represent scientific notation using the character `e` or `E`" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "96c151b0", + "metadata": { + "id": "96c151b0", + "outputId": "f9a0936f-f0d3-43e6-b4c6-32224e0a92db" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "3.14" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "3.14" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "689dee34", + "metadata": { + "id": "689dee34", + "outputId": "65755d5a-79f9-40db-c78d-7497011be6f2" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "float" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(3.14)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8a599c7a", + "metadata": { + "id": "8a599c7a", + "outputId": "71e5a5bb-37b1-48ba-9301-cbb7a4c1abd5" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "0.99" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + ".99" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b597680a", + "metadata": { + "id": "b597680a", + "outputId": "8ad5c909-1905-46ea-cd58-6b349f446a2f" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "500000000.0" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "5e8" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6af99295", + "metadata": { + "id": "6af99295", + "outputId": "52d205ea-35be-4408-df90-8e8863e69442" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "float" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(5e8)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "dc9d4797", + "metadata": { + "id": "dc9d4797", + "outputId": "244d908b-e04e-473d-c2cd-0fd4f9276f35" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "5.67e-06" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "5.67E-6" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c0379fb3", + "metadata": { + "id": "c0379fb3", + "outputId": "25bddf4b-9b4d-48d4-a974-07a00c381857" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "10.0" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#We can also convert or cast a integer to a float with float()\n", + "float(10)" + ] + }, + { + "cell_type": "markdown", + "id": "6fd43043", + "metadata": { + "id": "6fd43043" + }, + "source": [ + "Almost all platforms represent Python float values as 64-bit “double-precision” values, according to the IEEE 754 standard. In that case, the maximum value a floating-point number can have is approximately 1.8 ⨉ 10308. Python will indicate a number greater than that by the string inf" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8488b000", + "metadata": { + "id": "8488b000", + "outputId": "2840bc2d-65f2-426d-d250-aea25da3e3db" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "1.79e+308" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "1.79e308" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e9e58b9a", + "metadata": { + "id": "e9e58b9a", + "outputId": "cbd6ac27-8b11-4b8a-8aae-c2264e554907" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "inf" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "1.8e308" + ] + }, + { + "cell_type": "markdown", + "id": "545099db", + "metadata": { + "id": "545099db" + }, + "source": [ + "The closest a nonzero number can be to zero is approximately 5.0 ⨉ 10-324. Anything closer to zero than that is effectively zero:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2da777ac", + "metadata": { + "id": "2da777ac", + "outputId": "fe9ce9fe-a5b7-431a-8066-5c2ffea4a925" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "5e-324" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "5e-324" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cdde2a65", + "metadata": { + "id": "cdde2a65", + "outputId": "77822327-84b6-443d-9a50-5b08448f3468" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "0.0" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "1e-325" + ] + }, + { + "cell_type": "markdown", + "id": "69cf2b84", + "metadata": { + "id": "69cf2b84" + }, + "source": [ + "In most cases the internal representation of a floating-point number is an approximation of the actual value. In practice, the difference between the actual value and the represented value is very small and should not usually cause significant problems." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "05711ac5", + "metadata": { + "id": "05711ac5", + "outputId": "2d2b81fc-3e5c-443e-d322-261cac803740" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# == means 'is equal?' or 'is this equal to this?'\n", + "0.1 + 0.2 == 0.3" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c4f0446e", + "metadata": { + "id": "c4f0446e", + "outputId": "56c0fe28-4215-4e41-f5f1-217c3d3cb1eb" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0.1 = 0.10000000000000001\n", + "0.2 = 0.20000000000000001\n", + "0.3 = 0.29999999999999999\n" + ] + } + ], + "source": [ + "#Lets format our float numbers to have 17 decimal places\n", + "print(\"0.1 = {0:.17f}\".format(0.1))\n", + "print(\"0.2 = {0:.17f}\".format(0.2))\n", + "print(\"0.3 = {0:.17f}\".format(0.3))" + ] + }, + { + "cell_type": "markdown", + "id": "4f5896de", + "metadata": { + "id": "4f5896de" + }, + "source": [ + "### Complex numbers\n", + "Complex numbers are specified as `+j`. You can print only the real or imaginary part by using `.imag` or `.real`" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "298bf4f7", + "metadata": { + "id": "298bf4f7", + "outputId": "86b4756d-fea4-47fc-af04-300066d80d8f" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "(2+5j)" + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "2+5j" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "55f38c7c", + "metadata": { + "id": "55f38c7c", + "outputId": "ded3f605-b6bb-4205-afe9-df9832cec8a5" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "complex" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(2+5j)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d8d84ba7", + "metadata": { + "id": "d8d84ba7", + "outputId": "3ffc4012-2f56-41ee-b211-a36f1fd3b5ff" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "1.0" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "comp = 1+2j\n", + "comp.real" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "19826f04", + "metadata": { + "id": "19826f04", + "outputId": "c0c422e1-0f5c-40b4-fd7e-a1b1b584bfec" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "2.0" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "comp.imag" + ] + }, + { + "cell_type": "markdown", + "id": "59bb12dd", + "metadata": { + "id": "59bb12dd" + }, + "source": [ + "### Strings\n", + "Strings are sequences of character data. In Python this is called `str`.\n", + "Strings can be limited by single or double quotes." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "78aea91a", + "metadata": { + "id": "78aea91a", + "outputId": "2b82da34-e663-4ba9-f628-46a4d0f6956e" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "This is a string\n" + ] + }, + { + "data": { + "text/plain": [ + "str" + ] + }, + "execution_count": 40, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "print(\"This is a string\")\n", + "type(\"What is my type?\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "22b82343", + "metadata": { + "id": "22b82343", + "outputId": "56531d65-f4ce-4fa2-f36d-cbc08b71f4ee" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "This is also a string\n" + ] + }, + { + "data": { + "text/plain": [ + "str" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "print('This is also a string')\n", + "type('Am I a string?')" + ] + }, + { + "cell_type": "markdown", + "id": "145cb9e7", + "metadata": { + "id": "145cb9e7" + }, + "source": [ + "A string can contain as many characters as you wish. The only limit is the computer memory. A string can also be empty." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1f921753", + "metadata": { + "id": "1f921753", + "outputId": "5993f553-a91f-4752-95de-cd575471088c" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "str" + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "''\n", + "type('')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8d4d828d", + "metadata": { + "id": "8d4d828d", + "outputId": "1fe26634-4f4e-46c5-8c02-fbd7952f35f1" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Here is a single quote ''\n", + "Here is a double quote \"\"\n" + ] + } + ], + "source": [ + "#Double and single quotes can be combined\n", + "print(\"Here is a single quote ''\")\n", + "\n", + "print('Here is a double quote \"\"')" + ] + }, + { + "cell_type": "markdown", + "id": "78932997", + "metadata": { + "id": "78932997" + }, + "source": [ + "Here are some useful string functions, concatenation and indexing" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "10ae543b", + "metadata": { + "id": "10ae543b", + "outputId": "442567cf-f58c-472f-f67b-553d333eed62" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "17" + ] + }, + "execution_count": 46, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "message = \"what do you like?\"\n", + "# length of string\n", + "len(message)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "38b2f65c", + "metadata": { + "id": "38b2f65c", + "outputId": "62ac24af-c319-4a49-b601-e37426b23a85" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'WHAT DO YOU LIKE?'" + ] + }, + "execution_count": 47, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Make upper-case. See also str.lower()\n", + "message.upper()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f30c3eec", + "metadata": { + "id": "f30c3eec", + "outputId": "e67c17f8-f6f2-4dd2-d12b-f1b38426db89" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'what do you like?'" + ] + }, + "execution_count": 48, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Make lower-case.\n", + "message.lower()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "58378fff", + "metadata": { + "id": "58378fff", + "outputId": "2069a2fe-f66c-412d-9d62-0fafd90a999d" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'What do you like?'" + ] + }, + "execution_count": 49, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Capitalize. See also str.title()\n", + "message.capitalize()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e6b59e79", + "metadata": { + "id": "e6b59e79", + "outputId": "0ad94937-17ce-4c34-acae-ab0c1e55dd77" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'What Do You Like?'" + ] + }, + "execution_count": 50, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Capitalize every first letter\n", + "message.title()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b45c0f40", + "metadata": { + "id": "b45c0f40", + "outputId": "de480c6a-b20b-4136-f82a-da819c35a813" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'what do you like? I like Python'" + ] + }, + "execution_count": 52, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Concatenate or join two or more strings with +\n", + "message + \" I like Python\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4c466e71", + "metadata": { + "id": "4c466e71", + "outputId": "5b6f5381-4070-4585-e754-e7491e0f4a33" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'I like PythonI like PythonI like PythonI like PythonI like Python'" + ] + }, + "execution_count": 53, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# an astherisc * will multiply your string\n", + "like = \"I like Python\"\n", + "like * 5" + ] + }, + { + "cell_type": "markdown", + "id": "a151b0f1", + "metadata": { + "id": "a151b0f1" + }, + "source": [ + "Indexing means accesing the individual elements or characters of a string (or list, as you will see later). To index a string use the `[]` brackets and the number you want to index. Just remember Python uses zero-based index." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d49a55db", + "metadata": { + "id": "d49a55db", + "outputId": "3de9d1be-e85b-4db5-8dad-ec0697239602" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'I'" + ] + }, + "execution_count": 55, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "like[0]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ff85a50d", + "metadata": { + "id": "ff85a50d", + "outputId": "f8e674a5-86d3-4827-b8b3-976df7b45161" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "' '" + ] + }, + "execution_count": 58, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "like[1]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "474dcf0c", + "metadata": { + "id": "474dcf0c", + "outputId": "e0f5066d-0357-4d5b-c122-4737a22884cb" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "I l i k\n" + ] + } + ], + "source": [ + "print(like[0], like[1], like[2], like[3], like[4])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a72d19c1", + "metadata": { + "id": "a72d19c1", + "outputId": "5f1beac8-4abe-4a73-88f2-7fbb523ec0df" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'n'" + ] + }, + "execution_count": 66, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# A negative index will start counting from the right side\n", + "# Here is the last letter\n", + "like[-1]" + ] + }, + { + "cell_type": "markdown", + "id": "5fe325f2", + "metadata": { + "id": "5fe325f2" + }, + "source": [ + "### Optional Exercise\n", + "Create two string variables, one with your first name, another with your last name.\n", + "- Concatenate these two variables\n", + "- Index just the first letter of your name and the first letter of your last name using `[]`, concatenate and print these two characters.\n", + "- Can you print the initials of your first and last name but this time only in lowercase letters?\n", + "- Try to print the following rectangle using only string multiplication with `*`\n", + "\n", + "`**************` \\\n", + "`* *` \\\n", + "`* *` \\\n", + "`* *` \\\n", + "`* *` \\\n", + "`* *` \\\n", + "`**************`" + ] + }, + { + "cell_type": "markdown", + "id": "6a3766cf", + "metadata": { + "id": "6a3766cf" + }, + "source": [ + "### Boolean Type\n", + "Objects of Boolean type may have one of two values, `True` or `False`:\n", + "Expressions in Python are often evaluated in Boolean context, meaning they are interpreted to represent truth or falsehood." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "fbb1b3cc", + "metadata": { + "id": "fbb1b3cc", + "outputId": "fbf979f0-c122-411a-93de-6e16bc301472" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 69, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Compare if 4 if less than 5\n", + "result = (4 < 5)\n", + "result" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c61ec305", + "metadata": { + "id": "c61ec305", + "outputId": "0ae30f1d-bf97-49c0-9190-a690610c6e1e" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "bool" + ] + }, + "execution_count": 71, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(result)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b0e45897", + "metadata": { + "id": "b0e45897", + "outputId": "91c5fa15-5438-47c0-b25a-3bcc8054c10f" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True False\n" + ] + } + ], + "source": [ + "print(True, False)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b5aab6f0", + "metadata": { + "id": "b5aab6f0", + "outputId": "5d693b92-1a18-4c9b-f446-feb5be1fbcea" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 76, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Not zero\n", + "bool(2014)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3e20f5c3", + "metadata": { + "id": "3e20f5c3", + "outputId": "e0775f95-39cd-40e3-c823-d7a48a88ff35" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 77, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# A zero is equal to False\n", + "bool(0)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "67d70d06", + "metadata": { + "id": "67d70d06", + "outputId": "f5082d59-bd32-4dc1-932d-2fc6ae639e45" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 78, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Also not zero\n", + "bool(3.1415)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "61a8db9a", + "metadata": { + "id": "61a8db9a", + "outputId": "ef6c6fc4-aab2-4f5c-a301-8c7bd078e2a3" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 79, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "bool(None)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c4ac8aec", + "metadata": { + "id": "c4ac8aec", + "outputId": "9f8551d4-dab8-4410-b4b4-fb88bc0b6a62" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 81, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Empty string\n", + "bool(\"\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b55886b2", + "metadata": { + "id": "b55886b2", + "outputId": "f8f7bb15-6968-4ceb-d27a-36e0b6e2baaf" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 82, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "bool(\"abc\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "510bda47", + "metadata": { + "id": "510bda47", + "outputId": "d6dc341c-1ee2-4b74-f644-fadf1a54813d" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 83, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "bool([1, 2, 3])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6466ab80", + "metadata": { + "id": "6466ab80", + "outputId": "a8699a22-cffc-4c6f-f4a8-dae7db947fbc" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 84, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Empty list\n", + "bool([])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a1082049", + "metadata": { + "id": "a1082049", + "outputId": "a75f6022-b173-48d6-f1cb-1206416c7e08" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 85, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "bool(0.0)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0f4a3cf2", + "metadata": { + "id": "0f4a3cf2", + "outputId": "cc870867-55d3-4021-e0f5-def6f76d7766" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 86, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "bool(1.0)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a5ad8551", + "metadata": { + "id": "a5ad8551", + "outputId": "3b8e6f01-d2f1-4971-f44b-35f7391aa28b" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 87, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "int(True)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ebaa2672", + "metadata": { + "id": "ebaa2672", + "outputId": "f948da62-c2ce-48a5-cf15-5ff0e84665c4" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 89, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "int(False)" + ] + }, + { + "cell_type": "markdown", + "id": "156dfe28", + "metadata": { + "id": "156dfe28" + }, + "source": [ + "### Other useful data types\n", + "### Lists\n", + "Lists are mutable collections of objects, similar to arrays in other programming languages. Lists are defined in Python by enclosing a comma-separated sequence of objects in square brackets `[]`\n", + "- Lists are ordered\n", + "- Lists can contain different types of objects\n", + "- List objects can be accessed by index\n", + "- Lists can be nested to arbitrary depth e.g. list of lists\n", + "- Lists are mutable, they can be changed" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4add7d38", + "metadata": { + "id": "4add7d38", + "outputId": "ca4867e6-920a-48d0-ef90-93d228d92f7a" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "['John', 'Mary', 'Max', 'Wanda']" + ] + }, + "execution_count": 91, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Creating a string list\n", + "friends = ['John', 'Mary', 'Max', 'Wanda']\n", + "friends" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "65faca10", + "metadata": { + "id": "65faca10", + "outputId": "f9f2538e-cea6-4bfe-91eb-d9170aab6153" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 92, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Lists are ordered\n", + "# lists with the same elements in different order are not the same\n", + "a = [1, 2, 3, 4, 5]\n", + "b = [1, 2, 4, 3, 5]\n", + "\n", + "a == b" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "14fa7954", + "metadata": { + "id": "14fa7954", + "outputId": "6dd5452f-2066-4dfc-f0d8-329a85027f6a" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 93, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a is b" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d0ae682c", + "metadata": { + "id": "d0ae682c", + "outputId": "9325e4d5-acaf-49de-9ec0-c4b580df35e0" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[10, 3.14, 'hello', True]" + ] + }, + "execution_count": 94, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Lists can contain different types of objects\n", + "c = [10, 3.14, 'hello', True]\n", + "c" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1be3520d", + "metadata": { + "id": "1be3520d", + "outputId": "55aa9765-fd77-41a0-974b-60432bbce949" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 95, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(c)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "33c22ce1", + "metadata": { + "id": "33c22ce1", + "outputId": "cbfaa505-355c-4b70-abc6-f2286795360b" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "bool" + ] + }, + "execution_count": 97, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(c[3])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "64a0c4a9", + "metadata": { + "id": "64a0c4a9" + }, + "outputs": [], + "source": [ + "# Lists can contain the same object multiple times\n", + "d = ['bark', 'cat', 'cat', 'dog']" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2ea04079", + "metadata": { + "id": "2ea04079", + "outputId": "b9932b83-5932-4d31-9bc2-25d5230895b0" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'bark'" + ] + }, + "execution_count": 103, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Lists objects can be accessed by index\n", + "d[0]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5c65e5ee", + "metadata": { + "id": "5c65e5ee", + "outputId": "0bd4988d-226b-40e9-8e72-faf8c993cc35" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'cat'" + ] + }, + "execution_count": 104, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d[1]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b0d744d5", + "metadata": { + "id": "b0d744d5", + "outputId": "79c78efa-856b-4940-ef44-b39ec1389529" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'dog'" + ] + }, + "execution_count": 105, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Remember a negative index counts from the end of the list\n", + "d[-1]" + ] + }, + { + "cell_type": "markdown", + "id": "1b769660", + "metadata": { + "id": "1b769660" + }, + "source": [ + "Slicing a list means taking a range of the objects that exist in that list, this is done by the square brackets and two index numbers separated by `:` similar to this `[0:3]`. In a list `a`, `a[m:n]` returns the portion of a from index m to, but not including, index n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f2db9054", + "metadata": { + "id": "f2db9054", + "outputId": "51031920-fcdc-407a-8178-bde646a75f0e" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "['John', 'Mary']" + ] + }, + "execution_count": 106, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "friends[0:2]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b6d5d7d6", + "metadata": { + "id": "b6d5d7d6", + "outputId": "87d72d38-b517-431e-a660-4a1c7d600d01" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "['Mary', 'Max']" + ] + }, + "execution_count": 110, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# not including index 3\n", + "friends[1:3]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "000942df", + "metadata": { + "id": "000942df", + "outputId": "8aa7b819-e7a5-4be4-ebd5-4376317bc4a3" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'Wanda'" + ] + }, + "execution_count": 111, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "friends[3]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2a5e7c1e", + "metadata": { + "id": "2a5e7c1e", + "outputId": "de0b0a37-d336-4678-f4ab-8440ee305645" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "['Mary', 'Max', 'Wanda']" + ] + }, + "execution_count": 118, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Negative slicing is supported\n", + "friends[-3: 4]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "538dcb72", + "metadata": { + "id": "538dcb72", + "outputId": "36e6319d-689b-40e9-8459-e94dc49e8a69" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "['John', 'Mary', 'Max']" + ] + }, + "execution_count": 119, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Omiting the first index starts at the beginning of the list\n", + "friends[:3]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "21e387e2", + "metadata": { + "id": "21e387e2", + "outputId": "3ae1f8d8-6f9d-4ee8-86db-c711395f62a1" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "['John', 'Mary', 'Max', 'Wanda']" + ] + }, + "execution_count": 120, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Omiting the second index extend the slice to the end of the list\n", + "friends[0:]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "187c870f", + "metadata": { + "id": "187c870f", + "outputId": "12f1a708-195c-48d2-9b30-b274af334fd8" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "['John', 'Max']" + ] + }, + "execution_count": 121, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# You can also specify a step\n", + "friends[0:4:2]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7e202ccb", + "metadata": { + "id": "7e202ccb", + "outputId": "4e5187fc-56ba-413c-d117-b5d269e1a885" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" + ] + }, + "execution_count": 126, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Lets visualize step/stride with a numeric list\n", + "numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n", + "numbers" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e5b4299b", + "metadata": { + "id": "e5b4299b", + "outputId": "934f02a3-34f8-4564-afbb-095009f5bb83" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 2, 4, 6, 8]" + ] + }, + "execution_count": 127, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Start in index 0, include all elements, step every 2 elements\n", + "numbers[0::2]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ae80c559", + "metadata": { + "id": "ae80c559", + "outputId": "913bc032-4446-4ca6-f70d-adfba9fc4476" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'Hello'" + ] + }, + "execution_count": 128, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# You can also slice strings!\n", + "message = 'Hello'\n", + "message[:]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "499bcc44", + "metadata": { + "id": "499bcc44", + "outputId": "16c9e2d9-926a-45be-dca8-4b55834d5984" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'llo'" + ] + }, + "execution_count": 129, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "message[2:5]" + ] + }, + { + "cell_type": "markdown", + "id": "752a8084", + "metadata": { + "id": "752a8084" + }, + "source": [ + "The concatenation (+) and replication (*) operators:\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "89d9c0ee", + "metadata": { + "id": "89d9c0ee", + "outputId": "301ddd2c-c909-4c3e-be11-1c6c0e89660f" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "['hello', 'yes', 'no']" + ] + }, + "execution_count": 131, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "e = ['hello', 'yes', 'no']\n", + "e" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "aa2f6237", + "metadata": { + "id": "aa2f6237", + "outputId": "4b2949cf-7bb2-42d4-efdc-0a1429377505" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "['hello', 'yes', 'no', 'here', 'mylist']" + ] + }, + "execution_count": 132, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "e + ['here', 'mylist']" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ed1fac87", + "metadata": { + "id": "ed1fac87", + "outputId": "660ca2f6-c0cb-4665-eb01-a95fc9a3619d" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "['hello', 'yes', 'no', 'hello', 'yes', 'no']" + ] + }, + "execution_count": 133, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "e * 2" + ] + }, + { + "cell_type": "markdown", + "id": "553f2fb4", + "metadata": { + "id": "553f2fb4" + }, + "source": [ + "The len(), min(), and max() functions" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "639a486a", + "metadata": { + "id": "639a486a", + "outputId": "83c13509-43e7-4e78-f795-753bad5e041b" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "5" + ] + }, + "execution_count": 140, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "f = [5, 10, 15, 20, 25]\n", + "len(f)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "fa4cfa36", + "metadata": { + "id": "fa4cfa36", + "outputId": "703e1e77-5cc4-4b93-94ec-297e6abb7256" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "5" + ] + }, + "execution_count": 141, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "min(f)\n", + "# Try this function with a string list. Can you explain what is happening?" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "43f778ea", + "metadata": { + "id": "43f778ea", + "outputId": "eaf4f168-2bb2-4c15-89a9-69ce7eac29b2" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "25" + ] + }, + "execution_count": 143, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "max(f)\n", + "# Try this function with a string list. Can you explain what is happening?" + ] + }, + { + "cell_type": "markdown", + "id": "e93b35a1", + "metadata": { + "id": "e93b35a1" + }, + "source": [ + "Lists can be nested" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "67c25d8e", + "metadata": { + "id": "67c25d8e", + "outputId": "88cec9b8-4a8f-4984-d145-7f9e30162e8a" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[[1, 2, 3, 4], [10, 20, 30], ['hello', 'from', 'my list']]" + ] + }, + "execution_count": 144, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "my_lists = [[1, 2, 3, 4], [10, 20, 30], ['hello', 'from', 'my list']]\n", + "my_lists" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "926818e8", + "metadata": { + "id": "926818e8", + "outputId": "665b0a05-c195-4bcb-9b7f-3c3024e01ebf" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3, 4]" + ] + }, + "execution_count": 145, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "my_lists[0]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2656eb4d", + "metadata": { + "id": "2656eb4d", + "outputId": "d5753983-2036-42e4-9068-e2750da48a4b" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 147, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Accessing the second index\n", + "my_lists[0][2]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3f9b96c5", + "metadata": { + "id": "3f9b96c5", + "outputId": "e5b1977d-a39f-43ec-f86c-dbd7bd6bc909" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "['hello', 'from', 'my list']" + ] + }, + "execution_count": 148, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "my_lists[2]" + ] + }, + { + "cell_type": "markdown", + "id": "4388b319", + "metadata": { + "id": "4388b319" + }, + "source": [ + "Lists are mutable, meaning they can be changed" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c400c936", + "metadata": { + "id": "c400c936", + "outputId": "7690e2a1-88fd-468f-a2f7-f2266edbf945" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "['eggs', 'cheese', 'milk', 'bread']" + ] + }, + "execution_count": 161, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "groceries = ['eggs', 'cheese', 'milk', 'bread']\n", + "groceries" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5483caa4", + "metadata": { + "id": "5483caa4" + }, + "outputs": [], + "source": [ + "groceries[2] = 'soy milk'" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "767edce9", + "metadata": { + "id": "767edce9", + "outputId": "7800a1d9-8a1d-431f-8013-de30d3a57cec" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "['eggs', 'cheese', 'soy milk', 'bread']" + ] + }, + "execution_count": 163, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "groceries" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2c70d911", + "metadata": { + "id": "2c70d911", + "outputId": "f99bdc6b-ca55-4e09-b813-c8bc1b1c1a4f" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "['eggs', 'soy milk', 'bread']" + ] + }, + "execution_count": 164, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# delete an element with del()\n", + "del groceries[1]\n", + "groceries" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9b43a237", + "metadata": { + "id": "9b43a237", + "outputId": "84ca8855-20d8-4d7a-db97-76549f7664eb" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "['eggs', 'soy milk', 'bread', 'coffee']" + ] + }, + "execution_count": 165, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Add a value at the end of a list with append\n", + "groceries.append('coffee')\n", + "groceries" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "90ba3f90", + "metadata": { + "id": "90ba3f90", + "outputId": "c236aa64-6c5f-4103-8ceb-5e2b652d235c" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "['bread', 'coffee', 'eggs', 'soy milk']" + ] + }, + "execution_count": 167, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# sort a list\n", + "groceries.sort()\n", + "groceries" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "fb475e99", + "metadata": { + "id": "fb475e99", + "outputId": "1d0b2a1f-6f1b-4245-daae-09a027e9154e" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[22, 18, 15, 25, 26, 19, 35, 42]" + ] + }, + "execution_count": 168, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ages = [22, 18, 15, 25, 26, 19, 35, 42]\n", + "ages" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a35aaf5a", + "metadata": { + "id": "a35aaf5a", + "outputId": "332436ae-dafe-465b-bb7c-1c2d060b391f" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[15, 18, 19, 22, 25, 26, 35, 42]" + ] + }, + "execution_count": 169, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ages.sort()\n", + "ages" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "402772f3", + "metadata": { + "id": "402772f3", + "outputId": "010c9a16-1442-449a-958f-189fce13e2b5" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[15, 18, 19, 22, 25, 26, 35, 42]" + ] + }, + "execution_count": 170, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Invert a list\n", + "ages" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "bba06ef4", + "metadata": { + "id": "bba06ef4", + "outputId": "a80d43c9-38a9-41ac-91f7-7d032b5ec646" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[42, 35, 26, 25, 22, 19, 18, 15]" + ] + }, + "execution_count": 171, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ages[::-1]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0579280f", + "metadata": { + "id": "0579280f", + "outputId": "deb94956-db6c-4f29-c945-66ee15ae4853" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[15, 18, 22, 25, 26, 35, 42]" + ] + }, + "execution_count": 172, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Delete an element from a specified index\n", + "ages.pop(2)\n", + "ages" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "edff7aa5", + "metadata": { + "id": "edff7aa5", + "outputId": "5a0a1fb0-596c-479b-de6f-ff860649853f" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[15, 18, 30, 22, 26, 35, 42]" + ] + }, + "execution_count": 174, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# You can also specify a value to remove with .remove()\n", + "ages.remove(25)\n", + "ages" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1ef9eb85", + "metadata": { + "id": "1ef9eb85", + "outputId": "176cd752-aabe-412f-ce1d-f6f4cd495957" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[15, 18, 30, 22, 25, 26, 35, 42]" + ] + }, + "execution_count": 173, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Insert a value at a specified index\n", + "ages.insert(2, 30)\n", + "ages" + ] + }, + { + "cell_type": "markdown", + "id": "fbfce63d", + "metadata": { + "id": "fbfce63d" + }, + "source": [ + "### Tuples\n", + "Tuples are static ordered collections of objects.\n", + "Tuples are identical to lists except in the following:\n", + "- Tuples are defined by `()`\n", + "- Tuples are inmutable, they cannot change" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5d74f8dc", + "metadata": { + "id": "5d74f8dc", + "outputId": "29b5da80-6859-4299-fd22-ec98381ec0b7" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "('here', 'is', 'a', 'tuple')" + ] + }, + "execution_count": 175, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "t = ('here', 'is', 'a', 'tuple')\n", + "t" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6a640ad9", + "metadata": { + "id": "6a640ad9", + "outputId": "d94fa68c-55c2-48eb-9b67-8d5976044519" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'here'" + ] + }, + "execution_count": 176, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "t[0]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1776cf43", + "metadata": { + "id": "1776cf43", + "outputId": "36e40901-0d06-479e-e8eb-e31808567597" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "('here', 'is', 'a', 'tuple')" + ] + }, + "execution_count": 177, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "t[0:]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "604bf39d", + "metadata": { + "id": "604bf39d", + "outputId": "23c3ecd9-7b8b-4b49-9565-c2c46fd514cc" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'tuple'" + ] + }, + "execution_count": 178, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "t[-1]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4d9d6c44", + "metadata": { + "id": "4d9d6c44", + "outputId": "2418e95d-5afb-4217-ffd5-1ca2e6fe3422" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "('is', 'a')" + ] + }, + "execution_count": 179, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "t[1:3]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ab2dd269", + "metadata": { + "id": "ab2dd269", + "outputId": "da8863f3-9334-4c30-ed6d-5ef2e293f828" + }, + "outputs": [ + { + "ename": "TypeError", + "evalue": "'tuple' object does not support item assignment", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", + "Input \u001b[1;32mIn [180]\u001b[0m, in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m# you cannot modify a tuple\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m t[\u001b[38;5;241m2\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mone\u001b[39m\u001b[38;5;124m'\u001b[39m\n", + "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" + ] + } + ], + "source": [ + "# you cannot modify a tuple\n", + "t[2] = 'one'" + ] + }, + { + "cell_type": "markdown", + "id": "46859bd8", + "metadata": { + "id": "46859bd8" + }, + "source": [ + "Why use a tuple?\n", + "Program execution is faster when manipulating a tuple than it is for the equivalent list. (This is probably not going to be noticeable when the list or tuple is small.)\n", + "\n", + "Sometimes you don’t want data to be modified. If the values in the collection are meant to remain constant for the life of the program, using a tuple instead of a list guards against accidental modification." + ] + }, + { + "cell_type": "markdown", + "id": "6f65270a", + "metadata": { + "id": "6f65270a" + }, + "source": [ + "### Finally, dictionaries\n", + "Dictionaries and lists share the following characteristics:\n", + "\n", + "- Both are mutable.\n", + "- Both are dynamic. They can grow and shrink as needed.\n", + "- Both can be nested. A list can contain another list. A dictionary can contain another dictionary. A dictionary can also contain a list, and vice versa.\n", + "\n", + "Dictionaries differ from lists primarily in how elements are accessed:\n", + "\n", + "- List elements are accessed by their position in the list, via indexing.\n", + "- Dictionary elements are accessed via keys.\n", + "\n", + "`{key:value}`" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a629af63", + "metadata": { + "id": "a629af63", + "outputId": "363725b1-6d23-4c00-f30e-bca2f8abec48" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{'USA': 'Baseball', 'Canada': 'Hockey', 'Mexico': 'Soccer'}" + ] + }, + "execution_count": 181, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d = {'USA' : 'Baseball', 'Canada' : 'Hockey', 'Mexico' : 'Soccer'}\n", + "d" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0fb41624", + "metadata": { + "id": "0fb41624", + "outputId": "295ecd6b-973d-4cff-95d8-7a70f5dc9d52" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'Baseball'" + ] + }, + "execution_count": 182, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# To access a value, call the dictionary key\n", + "d['USA']" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "50c3d9ac", + "metadata": { + "id": "50c3d9ac", + "outputId": "51ab1036-2465-4c8d-bbe4-1a7f87ea018b" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{'USA': 'Football', 'Canada': 'Hockey', 'Mexico': 'Soccer'}" + ] + }, + "execution_count": 184, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Dictionaries can be changed\n", + "d['USA'] = 'Football'\n", + "d" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f879a757", + "metadata": { + "id": "f879a757", + "outputId": "08a26c26-9f42-4706-8908-9fe64d4e372c" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{'USA': 'Football',\n", + " 'Canada': 'Hockey',\n", + " 'Mexico': 'Soccer',\n", + " 'Chile': 'Bird watching'}" + ] + }, + "execution_count": 185, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# To add a new key value just assign a new one\n", + "d['Chile'] = 'Bird watching'\n", + "d" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2f1c3d60", + "metadata": { + "id": "2f1c3d60", + "outputId": "80c19813-d2b5-4a56-ab76-1e40c742c523" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{'USA': 'Football', 'Canada': 'Hockey', 'Mexico': 'Soccer', 'Chile': 'Surfing'}" + ] + }, + "execution_count": 186, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Updating an entry works similarly\n", + "d['Chile'] = 'Surfing'\n", + "d" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7a889e3f", + "metadata": { + "id": "7a889e3f", + "outputId": "f3aacfcf-8844-455c-ccfe-4f84b74b3c5c" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{'USA': 'Football', 'Canada': 'Hockey', 'Chile': 'Surfing'}" + ] + }, + "execution_count": 187, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# To delete an element use the del statement and the key\n", + "del d['Mexico']\n", + "d" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0334f47c", + "metadata": { + "id": "0334f47c", + "outputId": "aab03273-4251-404e-aab6-855c8e0b5021" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{3: 'd', 2: 'c', 1: 'b', 0: 'a'}" + ] + }, + "execution_count": 192, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Integers can also be used as keys\n", + "d2 = {3: 'd', 2: 'c', 1: 'b', 0: 'a'}\n", + "d2" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "99be702d", + "metadata": { + "id": "99be702d", + "outputId": "d0538e70-bff3-4801-d04c-32be9b2db075" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'a'" + ] + }, + "execution_count": 193, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d2[0]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "15e701ad", + "metadata": { + "id": "15e701ad", + "outputId": "6bed1fb9-6925-451d-a069-a2439781d2b9" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{'names': ['Jon', 'Bob', 'Randy'],\n", + " 'ages': [25, 26, 27],\n", + " 'pets': {'dog': 'Max', 'cat': 'Butter', 'turtle': 'Speedy'}}" + ] + }, + "execution_count": 206, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d3 = {}\n", + "\n", + "d3['names'] = ['Jon', 'Bob', 'Randy']\n", + "d3['ages'] = [ 25, 26, 27]\n", + "d3['pets'] = {'dog' : 'Max', 'cat' : 'Butter', 'turtle' : 'Speedy'}\n", + "\n", + "d3" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c944027e", + "metadata": { + "id": "c944027e", + "outputId": "ecbefb49-38ff-445a-fee6-3e4e7571a921" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'Butter'" + ] + }, + "execution_count": 207, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Access a nested dictionary value\n", + "d3['pets']['cat']" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6b9ddfa5", + "metadata": { + "id": "6b9ddfa5", + "outputId": "00fe22e4-6045-45df-dc6f-2aa44b315245" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "27" + ] + }, + "execution_count": 208, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Access a nested list value\n", + "d3['ages'][2]" + ] + }, + { + "cell_type": "markdown", + "id": "c7ee73a0", + "metadata": { + "id": "c7ee73a0" + }, + "source": [ + "Some dictionary methods\n", + "- keys()\n", + "- values()\n", + "- clear()\n", + "- pop()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0ead69bf", + "metadata": { + "id": "0ead69bf", + "outputId": "163ffb15-fffa-411f-d69a-80591e6550fd" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "dict_keys(['names', 'ages', 'pets'])" + ] + }, + "execution_count": 209, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Return keys in dictionary\n", + "d3.keys()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "296fc747", + "metadata": { + "id": "296fc747", + "outputId": "e951b832-a99d-43f7-d9ed-bfae147c9fa7" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "dict_values([['Jon', 'Bob', 'Randy'], [25, 26, 27], {'dog': 'Max', 'cat': 'Butter', 'turtle': 'Speedy'}])" + ] + }, + "execution_count": 210, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Return values in dictionary\n", + "d3.values()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "228dc067", + "metadata": { + "id": "228dc067", + "outputId": "b6f7aefc-8c37-49b9-949c-9849bd4dc3e6" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{'dog': 'Max', 'cat': 'Butter', 'turtle': 'Speedy'}" + ] + }, + "execution_count": 212, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d3.pop('pets')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "119265b0", + "metadata": { + "id": "119265b0", + "outputId": "a0878405-68c9-4e38-a712-e173be884db5" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{'names': ['Jon', 'Bob', 'Randy'], 'ages': [25, 26, 27]}" + ] + }, + "execution_count": 213, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d3" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "aec6f54e", + "metadata": { + "id": "aec6f54e", + "outputId": "300fa7e6-5987-4bbf-aa9b-00ae8a65d880" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{}" + ] + }, + "execution_count": 214, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Clear a dictionary\n", + "d3.clear()\n", + "d3" + ] + }, + { + "cell_type": "markdown", + "id": "80ae6538", + "metadata": { + "id": "80ae6538" + }, + "source": [ + "## Converting Data types\n", + "- To convert to String `str()`\n", + "- To convert to Integer `int()`\n", + "- To convert to Float `float()`\n", + "- To convert to Bool `bool()`" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9bb3accb", + "metadata": { + "id": "9bb3accb", + "outputId": "bf5149d3-b045-4030-ee1c-ea655ca13a59" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'23'" + ] + }, + "execution_count": 215, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "age = 23\n", + "str(age)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3d5febf9", + "metadata": { + "id": "3d5febf9", + "outputId": "13d26e65-a772-4ac6-e1d7-8f685e9c565f" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "23 years old\n" + ] + } + ], + "source": [ + "print(str(age) + \" years old\")" + ] + }, + { + "cell_type": "markdown", + "id": "74e8584c", + "metadata": { + "id": "74e8584c" + }, + "source": [ + "## Exercises for participation credit\n", + "Please complete the following exercises and upload your completed notebook to your Github repository for participation credit.\n", + "\n", + "We can use `input()` to catch input from the user. This is a string type variable." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "311dbb98", + "metadata": { + "id": "311dbb98", + "outputId": "44224894-b7b9-4d57-a51f-0c9680f1c972", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 351 + } + }, + "outputs": [ + { + "ename": "KeyboardInterrupt", + "evalue": "Interrupted by user", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Enter your value '\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36mraw_input\u001b[0;34m(self, prompt)\u001b[0m\n\u001b[1;32m 849\u001b[0m \u001b[0;34m\"raw_input was called, but this frontend does not support input requests.\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 850\u001b[0m )\n\u001b[0;32m--> 851\u001b[0;31m return self._input_request(str(prompt),\n\u001b[0m\u001b[1;32m 852\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_parent_ident\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 853\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_parent_header\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36m_input_request\u001b[0;34m(self, prompt, ident, parent, password)\u001b[0m\n\u001b[1;32m 893\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 894\u001b[0m \u001b[0;31m# re-raise KeyboardInterrupt, to truncate traceback\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 895\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Interrupted by user\"\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 896\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mException\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 897\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlog\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwarning\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Invalid Message:\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mexc_info\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mKeyboardInterrupt\u001b[0m: Interrupted by user" + ] + } + ], + "source": [ + "x = input('Enter your value ')\n", + "print(x)" + ] + }, + { + "cell_type": "markdown", + "id": "d21d74a0", + "metadata": { + "id": "d21d74a0" + }, + "source": [ + "1. Write a code that gets two integers from the user. Save the first input as `a` and the second value as `b`. Run and print the following operations:\n", + " - a + b\n", + " - a - b\n", + " - a * b\n", + " - a ** b\n", + " - a / b" + ] + }, + { + "cell_type": "code", + "source": [ + "a = input('Enter your value ')\n", + "b = input('Enter your value ')\n", + "print(a)\n", + "print(b)\n", + "print(\"A+B: \", a + b)" + ], + "metadata": { + "id": "9fynGCVibmMW" + }, + "id": "9fynGCVibmMW", + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "## correct answer is\n", + "\n", + "a= int(input())\n" + ], + "metadata": { + "id": "u_smqsKBdQDR", + "outputId": "5c5608ea-23a4-4194-9d36-2d1242687511", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "id": "u_smqsKBdQDR", + "execution_count": 5, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "55\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "id": "c9cf9f05", + "metadata": { + "id": "c9cf9f05" + }, + "source": [ + "2. Reverse this list aList = [100, 200, 300, 400, 500]. Expected result = [500, 400, 300, 200, 100]" + ] + }, + { + "cell_type": "markdown", + "id": "7793dda3", + "metadata": { + "id": "7793dda3" + }, + "source": [ + "3. Create the following list ` ['My', 'name', 'is', 'Kelly']`\n", + "\n", + "Using only these two lists:\n", + "- `list1 = [\"M\", \"na\", \"i\", \"Ke\"]`\n", + "- `list2 = [\"y\", \"me\", \"s\", \"lly\"]`" + ] + }, + { + "cell_type": "markdown", + "id": "e9f296ee", + "metadata": { + "id": "e9f296ee" + }, + "source": [ + "4. Split the following list in 3 equal sized lists. Invert the the new lists\n", + "- Original list [11, 45, 8, 23, 14, 12, 78, 45, 89]\n", + "\n", + "\n", + "- Chunk 1 [11, 45, 8]\n", + "\n", + "After reversing it [8, 45, 11]\n", + "\n", + "- Chunk 2 [23, 14, 12]\n", + "\n", + "After reversing it [12, 14, 23]\n", + "\n", + "- Chunk 3 [78, 45, 89]\n", + "\n", + "After reversing it [89, 45, 78]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b02e2848", + "metadata": { + "id": "b02e2848" + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.10.4" + }, + "colab": { + "provenance": [], + "include_colab_link": true + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} \ No newline at end of file From 4a2badcd38df5e84298ce750847791cefb387faf Mon Sep 17 00:00:00 2001 From: awells-nrao Date: Wed, 5 Jun 2024 16:38:12 -0400 Subject: [PATCH 5/7] Class Notes --- Lesson_1_Python_basics_class_awells.ipynb | 2616 +++++++++++++++++++++ 1 file changed, 2616 insertions(+) create mode 100644 Lesson_1_Python_basics_class_awells.ipynb diff --git a/Lesson_1_Python_basics_class_awells.ipynb b/Lesson_1_Python_basics_class_awells.ipynb new file mode 100644 index 0000000..ba75a18 --- /dev/null +++ b/Lesson_1_Python_basics_class_awells.ipynb @@ -0,0 +1,2616 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "\"Open" + ] + }, + { + "cell_type": "markdown", + "id": "65297d5d", + "metadata": { + "id": "65297d5d" + }, + "source": [ + "## Lesson 1 - Python basics\n", + "In this lesson we will cover:\n", + "- What is a variable and how to declare it\n", + "- Python basic operators\n", + "- Data types in Python\n", + "- Lists\n", + "- Dictionaries\n", + "- Tuples\n", + "- Optional exercises\n", + "\n", + "All along the lessons you will see comments on the code starting with `#`, these are comment lines and are used to describe what the code is doing" + ] + }, + { + "cell_type": "code", + "source": [ + "print(\"My Class Notes\")" + ], + "metadata": { + "id": "G7P-ViuSHqCN", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "32887220-bfac-4de8-a75e-775d500936aa" + }, + "id": "G7P-ViuSHqCN", + "execution_count": 1, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "My Class Notes\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "id": "d927a42b", + "metadata": { + "id": "d927a42b" + }, + "source": [ + "## What is a variable?\n", + "\n", + "Imagine variables as containers to store data values.\n", + "We can asign values to these containers using the `=` sign." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d273a7b9", + "metadata": { + "id": "d273a7b9" + }, + "outputs": [], + "source": [ + "# Here we assign a to 4, and A to \"Python\"\n", + "a = 4\n", + "A = \"Python\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c6a73ac0", + "metadata": { + "id": "c6a73ac0" + }, + "outputs": [], + "source": [ + "# Print the variables\n" + ] + }, + { + "cell_type": "markdown", + "id": "a800cba9", + "metadata": { + "id": "a800cba9" + }, + "source": [ + "From the example above we can see that variables are case sensitive, so a variable named `Variable` will be different from another one called `variable`.\n", + "Variable names can consist of uppercase and lowercase letters, digits and underscore. The first character of a variable name cannot be a digit" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "36e9c3df", + "metadata": { + "id": "36e9c3df" + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3ade135f", + "metadata": { + "id": "3ade135f" + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "c31ad403", + "metadata": { + "id": "c31ad403" + }, + "source": [ + "### Reserved words in Python\n", + "For variable names:\n", + "- We can use number, letters and underscores\n", + "- Variables can include numbers, but not as the first character\n", + "- There are some reserverd words in Python that cannot be used as variable names:\n", + "\n", + "`False\tdef\tif\traise\n", + "None\tdel\timport\treturn\n", + "True\telif\tin\ttry\n", + "and\telse\tis\twhile\n", + "as\texcept\tlambda\twith\n", + "assert\tfinally\tnonlocal\tyield\n", + "break\tfor\tnot\n", + "class\tfrom\tor\n", + "continue\tglobal\tpass`\n", + "\n", + "You can see this list any time by typing `help(\"keywords\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9e951328", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "9e951328", + "outputId": "f8def180-c2e2-4632-b9cd-932a72f7f32d" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "\n", + "Here is a list of the Python keywords. Enter any keyword to get more help.\n", + "\n", + "False class from or\n", + "None continue global pass\n", + "True def if raise\n", + "and del import return\n", + "as elif in try\n", + "assert else is while\n", + "async except lambda with\n", + "await finally nonlocal yield\n", + "break for not \n", + "\n" + ] + } + ], + "source": [ + "help(\"keywords\")" + ] + }, + { + "cell_type": "markdown", + "id": "2ecace5e", + "metadata": { + "id": "2ecace5e" + }, + "source": [ + "## Operators\n", + "We now know how to create variables. Lets explore the basic operators in Python. Operators are special symbols in Python that carry out arithmetic or logical computations\n", + "\n", + "Arithmetic operators are used to perform mathematical operations like addition, substraction, multiplication, etc\n", + "\n", + "| Symbol | Operator | Description |\n", + "|--------|----------|-------------|\n", + "| + | Addition | Add two operands |\n", + "| - | Substraction | Substract two operands |\n", + "| * | Multiplication | Multiply two operands |\n", + "| / | Division | Divide two operands |\n", + "| % | Modulus | Remainder of the division of left operand by the right |\n", + "| // | Floor division | Division that results into whole number adjusted to the left in the number line |\n", + "| ** | Exponent | Left operand raised to the power of right |" + ] + }, + { + "cell_type": "markdown", + "id": "eb521890", + "metadata": { + "id": "eb521890" + }, + "source": [ + "## Types\n", + "Each variable can be of a particular type, but this type does not needs to be declared when you create the variable, and can even be changed after they have been set" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f1ce33e5", + "metadata": { + "id": "f1ce33e5" + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9ba75bd0", + "metadata": { + "id": "9ba75bd0" + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "643fc42f", + "metadata": { + "id": "643fc42f" + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "e4e0ed01", + "metadata": { + "id": "e4e0ed01" + }, + "source": [ + "Python is a highly object-oriented language. Every itme of data in Python is an object of a specific type or class. Consider the following code. Python does the following:\n", + "- Create a integer object\n", + "- Gives a value of 300\n", + "- Displays it to the console" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a62138f0", + "metadata": { + "id": "a62138f0" + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "85e49441", + "metadata": { + "id": "85e49441" + }, + "source": [ + "To see the actual integer object you can use the `type()` function" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5cbda040", + "metadata": { + "id": "5cbda040" + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "64edd62c", + "metadata": { + "id": "64edd62c" + }, + "source": [ + "## Basic Data Types\n", + "- Numeric (integer numbers, floating point)\n", + "- String (letters, characters)\n", + "- Boolean (True or False, 0 or 1)" + ] + }, + { + "cell_type": "markdown", + "id": "2f8cd151", + "metadata": { + "id": "2f8cd151" + }, + "source": [ + "### Integers\n", + "An integer value can be as big as you need it to be, this is only constrained by the amount of memory in the system. Python will interpret a sequence of decimal digits without any prefix to be a decimal number:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "46402c16", + "metadata": { + "id": "46402c16" + }, + "outputs": [], + "source": [ + "#print(123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2698b8d4", + "metadata": { + "id": "2698b8d4" + }, + "outputs": [], + "source": [ + "#print(10)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "fa03d0d3", + "metadata": { + "id": "fa03d0d3" + }, + "outputs": [], + "source": [ + "#Binary representation\n", + "#use 0b prefix\n", + "#print(0b10)\n", + "\n", + "#Octal representation\n", + "#use 0O prefix\n", + "#print(0O10)\n", + "\n", + "#Hexadecimal representation\n", + "#use 0X prefix\n", + "#print(0X10)" + ] + }, + { + "cell_type": "code", + "source": [ + "## Quick Check for knowlege how would you check the data type for the following representations previous binary (0b10),Octal(0O10) and hexadecial (0X10) and what are they?" + ], + "metadata": { + "id": "v3-ma7tY6Htw" + }, + "execution_count": null, + "outputs": [], + "id": "v3-ma7tY6Htw" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "36a83fc0" + }, + "outputs": [], + "source": [ + "#Answer for binary 0b10\n" + ], + "id": "36a83fc0" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "7e26ec9e" + }, + "outputs": [], + "source": [ + "# Answer for Octal for 0O10" + ], + "id": "7e26ec9e" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "3e11bb06" + }, + "outputs": [], + "source": [ + "#Answer for hexidecimal 0X10" + ], + "id": "3e11bb06" + }, + { + "cell_type": "markdown", + "id": "9fe061d9", + "metadata": { + "id": "9fe061d9" + }, + "source": [ + "### Floating point numbers\n", + "The `float` type designates a floating-point number, which specifies a decimal point. This can also represent scientific notation using the character `e` or `E`" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "96c151b0", + "metadata": { + "id": "96c151b0", + "outputId": "f9a0936f-f0d3-43e6-b4c6-32224e0a92db" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "3.14" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "3.14" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "689dee34", + "metadata": { + "id": "689dee34" + }, + "outputs": [], + "source": [ + "#type(3.14)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8a599c7a", + "metadata": { + "id": "8a599c7a" + }, + "outputs": [], + "source": [ + "#.99" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b597680a", + "metadata": { + "id": "b597680a" + }, + "outputs": [], + "source": [ + "#5e8" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6af99295", + "metadata": { + "id": "6af99295", + "outputId": "52d205ea-35be-4408-df90-8e8863e69442" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "float" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#type(5e8)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "dc9d4797", + "metadata": { + "id": "dc9d4797" + }, + "outputs": [], + "source": [ + "#5.67E-6" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c0379fb3", + "metadata": { + "id": "c0379fb3" + }, + "outputs": [], + "source": [ + "#We can also convert or cast a integer to a float with float().\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "id": "6fd43043", + "metadata": { + "id": "6fd43043" + }, + "source": [ + "Almost all platforms represent Python float values as 64-bit “double-precision” values, according to the IEEE 754 standard. In that case, the maximum value a floating-point number can have is approximately 1.8 ⨉ 10308. Python will indicate a number greater than that by the string inf" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8488b000", + "metadata": { + "id": "8488b000" + }, + "outputs": [], + "source": [ + "#1.79e308" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e9e58b9a", + "metadata": { + "id": "e9e58b9a" + }, + "outputs": [], + "source": [ + "#1.8e308" + ] + }, + { + "cell_type": "markdown", + "id": "545099db", + "metadata": { + "id": "545099db" + }, + "source": [ + "The closest a nonzero number can be to zero is approximately 5.0 ⨉ 10-324. Anything closer to zero than that is effectively zero:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2da777ac", + "metadata": { + "id": "2da777ac" + }, + "outputs": [], + "source": [ + "#5e-324" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cdde2a65", + "metadata": { + "id": "cdde2a65" + }, + "outputs": [], + "source": [ + "#1e-325" + ] + }, + { + "cell_type": "markdown", + "id": "69cf2b84", + "metadata": { + "id": "69cf2b84" + }, + "source": [ + "In most cases the internal representation of a floating-point number is an approximation of the actual value. In practice, the difference between the actual value and the represented value is very small and should not usually cause significant problems." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "05711ac5", + "metadata": { + "id": "05711ac5" + }, + "outputs": [], + "source": [ + "# == means 'is equal?' or 'is this equal to this?'\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c4f0446e", + "metadata": { + "id": "c4f0446e" + }, + "outputs": [], + "source": [ + "#Lets format our float numbers to have 17 decimal places\n" + ] + }, + { + "cell_type": "markdown", + "id": "4f5896de", + "metadata": { + "id": "4f5896de" + }, + "source": [ + "### Complex numbers\n", + "Complex numbers are specified as `+j`. You can print only the real or imaginary part by using `.imag` or `.real`" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "298bf4f7", + "metadata": { + "id": "298bf4f7", + "outputId": "3e961100-44e1-4c7d-e310-9a9902c199ee", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "(2+5j)" + ] + }, + "metadata": {}, + "execution_count": 25 + } + ], + "source": [ + "2+5j" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "55f38c7c", + "metadata": { + "id": "55f38c7c", + "outputId": "ded3f605-b6bb-4205-afe9-df9832cec8a5" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "complex" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(2+5j)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d8d84ba7", + "metadata": { + "id": "d8d84ba7", + "outputId": "3ffc4012-2f56-41ee-b211-a36f1fd3b5ff" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "1.0" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#comp = 1+2j\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "19826f04", + "metadata": { + "id": "19826f04" + }, + "outputs": [], + "source": [ + "# try printing the imaginary part" + ] + }, + { + "cell_type": "markdown", + "id": "59bb12dd", + "metadata": { + "id": "59bb12dd" + }, + "source": [ + "### Strings\n", + "Strings are sequences of character data. In Python this is called `str`.\n", + "Strings can be limited by single or double quotes." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "78aea91a", + "metadata": { + "id": "78aea91a" + }, + "outputs": [], + "source": [ + "#Try printing using This is a string using and check data type \"\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "22b82343", + "metadata": { + "id": "22b82343" + }, + "outputs": [], + "source": [ + "# Try printing string using '' and check the data type" + ] + }, + { + "cell_type": "markdown", + "id": "145cb9e7", + "metadata": { + "id": "145cb9e7" + }, + "source": [ + "A string can contain as many characters as you wish. The only limit is the computer memory. A string can also be empty." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1f921753", + "metadata": { + "id": "1f921753" + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8d4d828d", + "metadata": { + "id": "8d4d828d" + }, + "outputs": [], + "source": [ + "#Double and single quotes can be combined\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "id": "78932997", + "metadata": { + "id": "78932997" + }, + "source": [ + "Here are some useful string functions, concatenation and indexing" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "10ae543b", + "metadata": { + "id": "10ae543b" + }, + "outputs": [], + "source": [ + "#message = \"what do you like?\"\n", + "# length of string\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "38b2f65c", + "metadata": { + "id": "38b2f65c" + }, + "outputs": [], + "source": [ + "# Make upper-case. See also str.lower()\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f30c3eec", + "metadata": { + "id": "f30c3eec" + }, + "outputs": [], + "source": [ + "# Make lower-case.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "58378fff", + "metadata": { + "id": "58378fff", + "outputId": "2069a2fe-f66c-412d-9d62-0fafd90a999d" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'What do you like?'" + ] + }, + "execution_count": 49, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Capitalize. See also str.title()\n", + "message.capitalize()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e6b59e79", + "metadata": { + "id": "e6b59e79" + }, + "outputs": [], + "source": [ + "# Capitalize every first letter\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b45c0f40", + "metadata": { + "id": "b45c0f40" + }, + "outputs": [], + "source": [ + "# How to Concatenate or join two or more strings with +\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4c466e71", + "metadata": { + "id": "4c466e71" + }, + "outputs": [], + "source": [ + "# an astherisc * will multiply your string\n", + "#like = \"I like Python\"\n" + ] + }, + { + "cell_type": "markdown", + "id": "a151b0f1", + "metadata": { + "id": "a151b0f1" + }, + "source": [ + "Indexing means accesing the individual elements or characters of a string (or list, as you will see later). To index a string use the `[]` brackets and the number you want to index. Just remember Python uses zero-based index." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d49a55db", + "metadata": { + "id": "d49a55db" + }, + "outputs": [], + "source": [ + "#indexing\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ff85a50d", + "metadata": { + "id": "ff85a50d" + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "474dcf0c", + "metadata": { + "id": "474dcf0c" + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a72d19c1", + "metadata": { + "id": "a72d19c1" + }, + "outputs": [], + "source": [ + "# A negative index will start counting from the right side\n", + "# Here is the last letter\n" + ] + }, + { + "cell_type": "markdown", + "id": "5fe325f2", + "metadata": { + "id": "5fe325f2" + }, + "source": [ + "### Optional Exercise\n", + "Create two string variables, one with your first name, another with your last name.\n", + "- Concatenate these two variables\n", + "- Index just the first letter of your name and the first letter of your last name using `[]`, concatenate and print these two characters.\n", + "- Can you print the initials of your first and last name but this time only in lowercase letters?\n", + "- Try to print the following rectangle using only string multiplication with `*`\n", + "\n", + "`**************` \\\n", + "`* *` \\\n", + "`* *` \\\n", + "`* *` \\\n", + "`* *` \\\n", + "`* *` \\\n", + "`**************`" + ] + }, + { + "cell_type": "markdown", + "id": "6a3766cf", + "metadata": { + "id": "6a3766cf" + }, + "source": [ + "### Boolean Type\n", + "Objects of Boolean type may have one of two values, `True` or `False`:\n", + "Expressions in Python are often evaluated in Boolean context, meaning they are interpreted to represent truth or falsehood." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "fbb1b3cc", + "metadata": { + "id": "fbb1b3cc" + }, + "outputs": [], + "source": [ + "# Compare if 4 if less than 5\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c61ec305", + "metadata": { + "id": "c61ec305", + "outputId": "0ae30f1d-bf97-49c0-9190-a690610c6e1e" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "bool" + ] + }, + "execution_count": 71, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b0e45897", + "metadata": { + "id": "b0e45897" + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b5aab6f0", + "metadata": { + "id": "b5aab6f0", + "outputId": "5d693b92-1a18-4c9b-f446-feb5be1fbcea" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 76, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Not zero\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3e20f5c3", + "metadata": { + "id": "3e20f5c3", + "outputId": "e0775f95-39cd-40e3-c823-d7a48a88ff35" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 77, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# A zero is equal to False\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "67d70d06", + "metadata": { + "id": "67d70d06", + "outputId": "f5082d59-bd32-4dc1-932d-2fc6ae639e45" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 78, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Also not zero\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "61a8db9a", + "metadata": { + "id": "61a8db9a", + "outputId": "ef6c6fc4-aab2-4f5c-a301-8c7bd078e2a3" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 79, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# try a bool on None\n", + "bool(None)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c4ac8aec", + "metadata": { + "id": "c4ac8aec" + }, + "outputs": [], + "source": [ + "# what about an Empty string bool\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b55886b2", + "metadata": { + "id": "b55886b2" + }, + "outputs": [], + "source": [ + "#bool(\"abc\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "510bda47", + "metadata": { + "id": "510bda47" + }, + "outputs": [], + "source": [ + "#bool([1, 2, 3])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6466ab80", + "metadata": { + "id": "6466ab80" + }, + "outputs": [], + "source": [ + "# Empty list\n", + "#bool([])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a1082049", + "metadata": { + "id": "a1082049" + }, + "outputs": [], + "source": [ + "#bool(0.0)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0f4a3cf2", + "metadata": { + "id": "0f4a3cf2" + }, + "outputs": [], + "source": [ + "#bool(1.0)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a5ad8551", + "metadata": { + "id": "a5ad8551" + }, + "outputs": [], + "source": [ + "#int(True)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ebaa2672", + "metadata": { + "id": "ebaa2672" + }, + "outputs": [], + "source": [ + "#int(False)" + ] + }, + { + "cell_type": "markdown", + "id": "156dfe28", + "metadata": { + "id": "156dfe28" + }, + "source": [ + "### Other useful data types\n", + "### Lists\n", + "Lists are mutable collections of objects, similar to arrays in other programming languages. Lists are defined in Python by enclosing a comma-separated sequence of objects in square brackets `[]`\n", + "- Lists are ordered\n", + "- Lists can contain different types of objects\n", + "- List objects can be accessed by index\n", + "- Lists can be nested to arbitrary depth e.g. list of lists\n", + "- Lists are mutable, they can be changed" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4add7d38", + "metadata": { + "id": "4add7d38" + }, + "outputs": [], + "source": [ + "# Creating a string list\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "65faca10", + "metadata": { + "id": "65faca10" + }, + "outputs": [], + "source": [ + "# Lists are ordered\n", + "# lists with the same elements in different order are not the same\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "14fa7954", + "metadata": { + "id": "14fa7954" + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d0ae682c", + "metadata": { + "id": "d0ae682c" + }, + "outputs": [], + "source": [ + "# Lists can contain different types of objects\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1be3520d", + "metadata": { + "id": "1be3520d" + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "33c22ce1", + "metadata": { + "id": "33c22ce1" + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "64a0c4a9", + "metadata": { + "id": "64a0c4a9" + }, + "outputs": [], + "source": [ + "# Lists can contain the same object multiple times\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2ea04079", + "metadata": { + "id": "2ea04079" + }, + "outputs": [], + "source": [ + "# Lists objects can be accessed by index\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5c65e5ee", + "metadata": { + "id": "5c65e5ee" + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b0d744d5", + "metadata": { + "id": "b0d744d5" + }, + "outputs": [], + "source": [ + "# Remember a negative index counts from the end of the list\n" + ] + }, + { + "cell_type": "markdown", + "id": "1b769660", + "metadata": { + "id": "1b769660" + }, + "source": [ + "Slicing a list means taking a range of the objects that exist in that list, this is done by the square brackets and two index numbers separated by `:` similar to this `[0:3]`. In a list `a`, `a[m:n]` returns the portion of a from index m to, but not including, index n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f2db9054", + "metadata": { + "id": "f2db9054" + }, + "outputs": [], + "source": [ + "#slicing list\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b6d5d7d6", + "metadata": { + "id": "b6d5d7d6" + }, + "outputs": [], + "source": [ + "# not including index 3\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "000942df", + "metadata": { + "id": "000942df" + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2a5e7c1e", + "metadata": { + "id": "2a5e7c1e" + }, + "outputs": [], + "source": [ + "# Negative slicing is supported\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "538dcb72", + "metadata": { + "id": "538dcb72", + "outputId": "36e6319d-689b-40e9-8459-e94dc49e8a69" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "['John', 'Mary', 'Max']" + ] + }, + "execution_count": 119, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Omiting the first index starts at the beginning of the list\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "21e387e2", + "metadata": { + "id": "21e387e2" + }, + "outputs": [], + "source": [ + "# Omiting the second index extend the slice to the end of the list\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "187c870f", + "metadata": { + "id": "187c870f" + }, + "outputs": [], + "source": [ + "# You can also specify a step\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7e202ccb", + "metadata": { + "id": "7e202ccb" + }, + "outputs": [], + "source": [ + "# Lets visualize step/stride with a numeric list\n", + "#numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n", + "#numbers" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e5b4299b", + "metadata": { + "id": "e5b4299b" + }, + "outputs": [], + "source": [ + "# Start in index 0, include all elements, step every 2 elements\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ae80c559", + "metadata": { + "id": "ae80c559" + }, + "outputs": [], + "source": [ + "# You can also slice strings!\n", + "message = 'Hello'\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "499bcc44", + "metadata": { + "id": "499bcc44" + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "752a8084", + "metadata": { + "id": "752a8084" + }, + "source": [ + "The concatenation (+) and replication (*) operators:\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "89d9c0ee", + "metadata": { + "id": "89d9c0ee" + }, + "outputs": [], + "source": [ + "#e = ['hello', 'yes', 'no']\n", + "#e" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "aa2f6237", + "metadata": { + "id": "aa2f6237" + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ed1fac87", + "metadata": { + "id": "ed1fac87" + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "553f2fb4", + "metadata": { + "id": "553f2fb4" + }, + "source": [ + "The len(), min(), and max() functions" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "639a486a", + "metadata": { + "id": "639a486a" + }, + "outputs": [], + "source": [ + "#f = [5, 10, 15, 20, 25]\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "fa4cfa36", + "metadata": { + "id": "fa4cfa36" + }, + "outputs": [], + "source": [ + "\n", + "# Try this function with a string list. Can you explain what is happening?" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "43f778ea", + "metadata": { + "id": "43f778ea" + }, + "outputs": [], + "source": [ + "\n", + "# Try this function with a string list. Can you explain what is happening?" + ] + }, + { + "cell_type": "markdown", + "id": "e93b35a1", + "metadata": { + "id": "e93b35a1" + }, + "source": [ + "Lists can be nested" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "67c25d8e", + "metadata": { + "id": "67c25d8e" + }, + "outputs": [], + "source": [ + "#my_lists = [[1, 2, 3, 4], [10, 20, 30], ['hello', 'from', 'my list']]\n", + "#my_lists" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "926818e8", + "metadata": { + "id": "926818e8" + }, + "outputs": [], + "source": [ + "#my_lists[0]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2656eb4d", + "metadata": { + "id": "2656eb4d" + }, + "outputs": [], + "source": [ + "# Accessing the second index\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3f9b96c5", + "metadata": { + "id": "3f9b96c5" + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "4388b319", + "metadata": { + "id": "4388b319" + }, + "source": [ + "Lists are mutable, meaning they can be changed" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c400c936", + "metadata": { + "id": "c400c936", + "outputId": "7690e2a1-88fd-468f-a2f7-f2266edbf945" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "['eggs', 'cheese', 'milk', 'bread']" + ] + }, + "execution_count": 161, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#groceries = ['eggs', 'cheese', 'milk', 'bread']\n", + "#groceries" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5483caa4", + "metadata": { + "id": "5483caa4" + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "767edce9", + "metadata": { + "id": "767edce9", + "outputId": "7800a1d9-8a1d-431f-8013-de30d3a57cec" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "['eggs', 'cheese', 'soy milk', 'bread']" + ] + }, + "execution_count": 163, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2c70d911", + "metadata": { + "id": "2c70d911" + }, + "outputs": [], + "source": [ + "# delete an element with del()\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9b43a237", + "metadata": { + "id": "9b43a237" + }, + "outputs": [], + "source": [ + "# Add a value at the end of a list with append\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "90ba3f90", + "metadata": { + "id": "90ba3f90", + "outputId": "c236aa64-6c5f-4103-8ceb-5e2b652d235c" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "['bread', 'coffee', 'eggs', 'soy milk']" + ] + }, + "execution_count": 167, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# sort a list\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "fb475e99", + "metadata": { + "id": "fb475e99", + "outputId": "1d0b2a1f-6f1b-4245-daae-09a027e9154e" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[22, 18, 15, 25, 26, 19, 35, 42]" + ] + }, + "execution_count": 168, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#ages = [22, 18, 15, 25, 26, 19, 35, 42]\n", + "#ages" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a35aaf5a", + "metadata": { + "id": "a35aaf5a" + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "402772f3", + "metadata": { + "id": "402772f3" + }, + "outputs": [], + "source": [ + "# Invert a list\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "bba06ef4", + "metadata": { + "id": "bba06ef4" + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0579280f", + "metadata": { + "id": "0579280f" + }, + "outputs": [], + "source": [ + "# Delete an element from a specified index\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "edff7aa5", + "metadata": { + "id": "edff7aa5" + }, + "outputs": [], + "source": [ + "# You can also specify a value to remove with .remove()\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1ef9eb85", + "metadata": { + "id": "1ef9eb85" + }, + "outputs": [], + "source": [ + "# Insert a value at a specified index\n" + ] + }, + { + "cell_type": "markdown", + "id": "fbfce63d", + "metadata": { + "id": "fbfce63d" + }, + "source": [ + "### Tuples\n", + "Tuples are static ordered collections of objects.\n", + "Tuples are identical to lists except in the following:\n", + "- Tuples are defined by `()`\n", + "- Tuples are inmutable, they cannot change" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5d74f8dc", + "metadata": { + "id": "5d74f8dc" + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6a640ad9", + "metadata": { + "id": "6a640ad9" + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1776cf43", + "metadata": { + "id": "1776cf43" + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "604bf39d", + "metadata": { + "id": "604bf39d" + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4d9d6c44", + "metadata": { + "id": "4d9d6c44" + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ab2dd269", + "metadata": { + "id": "ab2dd269" + }, + "outputs": [], + "source": [ + "# you cannot modify a tuple\n" + ] + }, + { + "cell_type": "markdown", + "id": "46859bd8", + "metadata": { + "id": "46859bd8" + }, + "source": [ + "Why use a tuple?\n", + "Program execution is faster when manipulating a tuple than it is for the equivalent list. (This is probably not going to be noticeable when the list or tuple is small.)\n", + "\n", + "Sometimes you don’t want data to be modified. If the values in the collection are meant to remain constant for the life of the program, using a tuple instead of a list guards against accidental modification." + ] + }, + { + "cell_type": "markdown", + "id": "6f65270a", + "metadata": { + "id": "6f65270a" + }, + "source": [ + "### Finally, dictionaries\n", + "Dictionaries and lists share the following characteristics:\n", + "\n", + "- Both are mutable.\n", + "- Both are dynamic. They can grow and shrink as needed.\n", + "- Both can be nested. A list can contain another list. A dictionary can contain another dictionary. A dictionary can also contain a list, and vice versa.\n", + "\n", + "Dictionaries differ from lists primarily in how elements are accessed:\n", + "\n", + "- List elements are accessed by their position in the list, via indexing.\n", + "- Dictionary elements are accessed via keys.\n", + "\n", + "`{key:value}`" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a629af63", + "metadata": { + "id": "a629af63", + "outputId": "363725b1-6d23-4c00-f30e-bca2f8abec48" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{'USA': 'Baseball', 'Canada': 'Hockey', 'Mexico': 'Soccer'}" + ] + }, + "execution_count": 181, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d = {'USA' : 'Baseball', 'Canada' : 'Hockey', 'Mexico' : 'Soccer'}\n", + "d" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0fb41624", + "metadata": { + "id": "0fb41624", + "outputId": "295ecd6b-973d-4cff-95d8-7a70f5dc9d52" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'Baseball'" + ] + }, + "execution_count": 182, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# To access a value, call the dictionary key\n", + "d['USA']" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "50c3d9ac", + "metadata": { + "id": "50c3d9ac", + "outputId": "51ab1036-2465-4c8d-bbe4-1a7f87ea018b" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{'USA': 'Football', 'Canada': 'Hockey', 'Mexico': 'Soccer'}" + ] + }, + "execution_count": 184, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Dictionaries can be changed\n", + "d['USA'] = 'Football'\n", + "d" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f879a757", + "metadata": { + "id": "f879a757", + "outputId": "08a26c26-9f42-4706-8908-9fe64d4e372c" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{'USA': 'Football',\n", + " 'Canada': 'Hockey',\n", + " 'Mexico': 'Soccer',\n", + " 'Chile': 'Bird watching'}" + ] + }, + "execution_count": 185, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# To add a new key value just assign a new one\n", + "d['Chile'] = 'Bird watching'\n", + "d" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2f1c3d60", + "metadata": { + "id": "2f1c3d60", + "outputId": "80c19813-d2b5-4a56-ab76-1e40c742c523" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{'USA': 'Football', 'Canada': 'Hockey', 'Mexico': 'Soccer', 'Chile': 'Surfing'}" + ] + }, + "execution_count": 186, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Updating an entry works similarly\n", + "d['Chile'] = 'Surfing'\n", + "d" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7a889e3f", + "metadata": { + "id": "7a889e3f", + "outputId": "f3aacfcf-8844-455c-ccfe-4f84b74b3c5c" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{'USA': 'Football', 'Canada': 'Hockey', 'Chile': 'Surfing'}" + ] + }, + "execution_count": 187, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# To delete an element use the del statement and the key\n", + "del d['Mexico']\n", + "d" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0334f47c", + "metadata": { + "id": "0334f47c", + "outputId": "aab03273-4251-404e-aab6-855c8e0b5021" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{3: 'd', 2: 'c', 1: 'b', 0: 'a'}" + ] + }, + "execution_count": 192, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Integers can also be used as keys\n", + "d2 = {3: 'd', 2: 'c', 1: 'b', 0: 'a'}\n", + "d2" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "99be702d", + "metadata": { + "id": "99be702d", + "outputId": "d0538e70-bff3-4801-d04c-32be9b2db075" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'a'" + ] + }, + "execution_count": 193, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d2[0]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "15e701ad", + "metadata": { + "id": "15e701ad", + "outputId": "6bed1fb9-6925-451d-a069-a2439781d2b9" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{'names': ['Jon', 'Bob', 'Randy'],\n", + " 'ages': [25, 26, 27],\n", + " 'pets': {'dog': 'Max', 'cat': 'Butter', 'turtle': 'Speedy'}}" + ] + }, + "execution_count": 206, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d3 = {}\n", + "\n", + "d3['names'] = ['Jon', 'Bob', 'Randy']\n", + "d3['ages'] = [ 25, 26, 27]\n", + "d3['pets'] = {'dog' : 'Max', 'cat' : 'Butter', 'turtle' : 'Speedy'}\n", + "\n", + "d3" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c944027e", + "metadata": { + "id": "c944027e", + "outputId": "ecbefb49-38ff-445a-fee6-3e4e7571a921" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'Butter'" + ] + }, + "execution_count": 207, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Access a nested dictionary value\n", + "d3['pets']['cat']" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6b9ddfa5", + "metadata": { + "id": "6b9ddfa5", + "outputId": "00fe22e4-6045-45df-dc6f-2aa44b315245" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "27" + ] + }, + "execution_count": 208, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Access a nested list value\n", + "d3['ages'][2]" + ] + }, + { + "cell_type": "markdown", + "id": "c7ee73a0", + "metadata": { + "id": "c7ee73a0" + }, + "source": [ + "Some dictionary methods\n", + "- keys()\n", + "- values()\n", + "- clear()\n", + "- pop()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0ead69bf", + "metadata": { + "id": "0ead69bf", + "outputId": "163ffb15-fffa-411f-d69a-80591e6550fd" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "dict_keys(['names', 'ages', 'pets'])" + ] + }, + "execution_count": 209, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Return keys in dictionary\n", + "d3.keys()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "296fc747", + "metadata": { + "id": "296fc747", + "outputId": "e951b832-a99d-43f7-d9ed-bfae147c9fa7" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "dict_values([['Jon', 'Bob', 'Randy'], [25, 26, 27], {'dog': 'Max', 'cat': 'Butter', 'turtle': 'Speedy'}])" + ] + }, + "execution_count": 210, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Return values in dictionary\n", + "d3.values()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "228dc067", + "metadata": { + "id": "228dc067", + "outputId": "b6f7aefc-8c37-49b9-949c-9849bd4dc3e6" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{'dog': 'Max', 'cat': 'Butter', 'turtle': 'Speedy'}" + ] + }, + "execution_count": 212, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d3.pop('pets')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "119265b0", + "metadata": { + "id": "119265b0", + "outputId": "a0878405-68c9-4e38-a712-e173be884db5" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{'names': ['Jon', 'Bob', 'Randy'], 'ages': [25, 26, 27]}" + ] + }, + "execution_count": 213, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d3" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "aec6f54e", + "metadata": { + "id": "aec6f54e", + "outputId": "300fa7e6-5987-4bbf-aa9b-00ae8a65d880" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{}" + ] + }, + "execution_count": 214, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Clear a dictionary\n", + "d3.clear()\n", + "d3" + ] + }, + { + "cell_type": "markdown", + "id": "80ae6538", + "metadata": { + "id": "80ae6538" + }, + "source": [ + "## Converting Data types\n", + "- To convert to String `str()`\n", + "- To convert to Integer `int()`\n", + "- To convert to Float `float()`\n", + "- To convert to Bool `bool()`" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9bb3accb", + "metadata": { + "id": "9bb3accb", + "outputId": "bf5149d3-b045-4030-ee1c-ea655ca13a59" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'23'" + ] + }, + "execution_count": 215, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "age = 23\n", + "str(age)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3d5febf9", + "metadata": { + "id": "3d5febf9", + "outputId": "13d26e65-a772-4ac6-e1d7-8f685e9c565f" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "23 years old\n" + ] + } + ], + "source": [ + "print(str(age) + \" years old\")" + ] + }, + { + "cell_type": "markdown", + "id": "74e8584c", + "metadata": { + "id": "74e8584c" + }, + "source": [ + "## Exercises for participation credit\n", + "Please complete the following exercises and upload your completed notebook to your Github repository for participation credit.\n", + "\n", + "We can use `input()` to catch input from the user. This is a string type variable." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "311dbb98", + "metadata": { + "id": "311dbb98", + "outputId": "bb5129ae-6ea0-44b1-95d8-cb669642a888", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter your value 8\n", + "8\n" + ] + } + ], + "source": [ + "x = input('Enter your value ')\n", + "print(x)" + ] + }, + { + "cell_type": "markdown", + "id": "d21d74a0", + "metadata": { + "id": "d21d74a0" + }, + "source": [ + "1. Write a code that gets two integers from the user. Save the first input as `a` and the second value as `b`. Run and print the following operations:\n", + " - a + b\n", + " - a - b\n", + " - a * b\n", + " - a ** b\n", + " - a / b" + ] + }, + { + "cell_type": "markdown", + "id": "c9cf9f05", + "metadata": { + "id": "c9cf9f05" + }, + "source": [ + "2. Reverse this list aList = [100, 200, 300, 400, 500]. Expected result = [500, 400, 300, 200, 100]" + ] + }, + { + "cell_type": "markdown", + "id": "7793dda3", + "metadata": { + "id": "7793dda3" + }, + "source": [ + "3. Create the following list ` ['My', 'name', 'is', 'Kelly']`\n", + "\n", + "Using only these two lists:\n", + "- `list1 = [\"M\", \"na\", \"i\", \"Ke\"]`\n", + "- `list2 = [\"y\", \"me\", \"s\", \"lly\"]`" + ] + }, + { + "cell_type": "markdown", + "id": "e9f296ee", + "metadata": { + "id": "e9f296ee" + }, + "source": [ + "4. Split the following list in 3 equal sized lists. Invert the the new lists\n", + "- Original list [11, 45, 8, 23, 14, 12, 78, 45, 89]\n", + "\n", + "\n", + "- Chunk 1 [11, 45, 8]\n", + "\n", + "After reversing it [8, 45, 11]\n", + "\n", + "- Chunk 2 [23, 14, 12]\n", + "\n", + "After reversing it [12, 14, 23]\n", + "\n", + "- Chunk 3 [78, 45, 89]\n", + "\n", + "After reversing it [89, 45, 78]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b02e2848", + "metadata": { + "id": "b02e2848" + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.10.4" + }, + "colab": { + "provenance": [], + "include_colab_link": true + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} \ No newline at end of file From 2954d7b3d4f32cc3cabdc49e51b3b57df70404d9 Mon Sep 17 00:00:00 2001 From: awells-nrao Date: Wed, 5 Jun 2024 16:41:30 -0400 Subject: [PATCH 6/7] Created using Colab --- Lesson_1_Python_basics_class_awells.ipynb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Lesson_1_Python_basics_class_awells.ipynb b/Lesson_1_Python_basics_class_awells.ipynb index ba75a18..99555c0 100644 --- a/Lesson_1_Python_basics_class_awells.ipynb +++ b/Lesson_1_Python_basics_class_awells.ipynb @@ -33,14 +33,15 @@ { "cell_type": "code", "source": [ - "print(\"My Class Notes\")" + "print(\"My Class Notes\")\n", + "print(\"More Work\")" ], "metadata": { "id": "G7P-ViuSHqCN", "colab": { "base_uri": "https://localhost:8080/" }, - "outputId": "32887220-bfac-4de8-a75e-775d500936aa" + "outputId": "781cb456-2ea7-44e4-fa2c-d55754f3e48b" }, "id": "G7P-ViuSHqCN", "execution_count": 1, @@ -49,7 +50,8 @@ "output_type": "stream", "name": "stdout", "text": [ - "My Class Notes\n" + "My Class Notes\n", + "More Work\n" ] } ] From ff92bc169b6a5998cd127013523da9eb23859b8c Mon Sep 17 00:00:00 2001 From: awells-nrao Date: Wed, 5 Jun 2024 16:49:31 -0400 Subject: [PATCH 7/7] Created using Colab --- Lesson_1_Python_basics_class_awells.ipynb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Lesson_1_Python_basics_class_awells.ipynb b/Lesson_1_Python_basics_class_awells.ipynb index 99555c0..0c98a11 100644 --- a/Lesson_1_Python_basics_class_awells.ipynb +++ b/Lesson_1_Python_basics_class_awells.ipynb @@ -34,14 +34,14 @@ "cell_type": "code", "source": [ "print(\"My Class Notes\")\n", - "print(\"More Work\")" + "print(\"222\")" ], "metadata": { "id": "G7P-ViuSHqCN", "colab": { "base_uri": "https://localhost:8080/" }, - "outputId": "781cb456-2ea7-44e4-fa2c-d55754f3e48b" + "outputId": "b9803318-97f2-4a0f-d3a2-86b8aa985b6d" }, "id": "G7P-ViuSHqCN", "execution_count": 1, @@ -51,7 +51,7 @@ "name": "stdout", "text": [ "My Class Notes\n", - "More Work\n" + "222\n" ] } ]