{ "cells": [ { "cell_type": "markdown", "id": "bbeb5139-9063-431f-a542-9cc550fdf80e", "metadata": {}, "source": [ "# Appendix C — Python tutorial\n", "\n", "Click the binder button [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/minireference/noBSstats/HEAD?labpath=tutorials/python_tutorial.ipynb) or this link [`bit.ly/pytut3`](https://bit.ly/pytut3) to run this notebooks interactively.\n", "\n", "\n", "\n", "**Abstract:** \n", "In this tutorial,\n", "I'll introduce you to the basics of the Python programming language.\n", "Don't worry, learning Python is not complicated.\n", "You're not going to become a programmer or anything like that, \n", "I'm just going to show you **how to use Python as a calculator**.\n", "We'll start by setting up a Python coding environment\n", "on your computer (**JupyterLab Desktop**).\n", "We'll then cover basic building blocks like *expressions*, *variables*,\n", "*functions*, *lists*, `for`-*loops*, etc.\n", "By the end of this tutorial,\n", "you'll be familiar with the essential parts of the Python syntax needed to use the most kick-ass, powerful calculator that ever existed." ] }, { "cell_type": "markdown", "id": "2978fcb2-0ec9-4de7-8138-6c6df4531f2a", "metadata": {}, "source": [ "# Introduction" ] }, { "cell_type": "markdown", "id": "c47c163b-474f-4104-adcc-d30b339c5a08", "metadata": {}, "source": [ "## Python is a fancy calculator\n", "\n", "Python commands are similar to the commands you give to a calculator\n", "when you want to compute something.\n", "The same way a calculator has different buttons for the various arithmetic operations,\n", "the Python language has a number of commands you can \"run\" or \"execute.\"\n", "Knowing how to use Python gives you access to hundreds of useful libraries and thousands of functions, which are like calculator buttons specialized for different domains: math, science, statistics, machine learning, etc.\n", "\n", " " ] }, { "cell_type": "markdown", "id": "8615be8f-f3f2-4c2e-bda6-40460e43ba9b", "metadata": {}, "source": [ "## Why you should learn\n", "\n", "Here are some things you can do using Python:\n", "\n", "- You can use Python as a basic calculator\n", " for doing arithmetic calculations using math operations\n", " like `+`, `-`, `*`, `/`, and other math functions.\n", "- Python is also a scientific calculator\n", " since it provides functions like `sin`, `cos`, `log`, etc.\n", "- You can use Python as a graphical calculator\n", " to plot functions and visualize data.\n", "- Python is an extensible, programmable calculator\n", " that allows you to define your own functions and operations.\n", "- Python provides powerful libraries for numerical computing (`numpy`),\n", " scientific computing (`scipy`),\n", " and symbolic math calculations (`sympy`).\n", "- Python has libraries for data management (`pandas`), \n", " data visualization (`seaborn`),\n", " and statistics (`statsmodels`).\n", "\n", "If any of these seems useful to you,\n", "then read on to get started learning Python!\n", "\n", "" ] }, { "cell_type": "markdown", "id": "20413784-779d-47f5-9c3e-619e9219da33", "metadata": {}, "source": [ "## How to learn Python\n", "\n", "The fastest way to learn Python is through hands-on experimentation\n", "in an interactive coding environment like a Jupyter notebook.\n", "By running various Python commands and seeing the results they produce,\n", "you can quickly learn what Python functions (calculator buttons) are available,\n", "and what they do.\n", "\n", "This tutorial is intended for absolute beginners with no prior programming experience.\n", "I'll guide you step-by-step through the code examples,\n", "and explain what the code does in plain English.\n", "You can then explore the Python code examples,\n", "run them on your own,\n", "or \"play\" with them by changing the commands\n", "and running them with different inputs to see how the outputs change.\n", "I've also prepared some exercises for you,\n", "that will allow you to practice what you've been learning.\n", "\n", "" ] }, { "cell_type": "markdown", "id": "9932ab25-4c7b-404a-8a4b-e11c37b5644a", "metadata": { "tags": [] }, "source": [ "# Getting started\n", "\n", "Follow the six steps outlined below\n", "to setup a computational learning environment on your computer\n", "that will allow you to run this tutorial interactively." ] }, { "cell_type": "markdown", "id": "c666f552-35ea-4177-9773-4f849fb44195", "metadata": { "tags": [] }, "source": [ "## Installing JupyterLab Desktop\n", "\n", "**JupyterLab** is a computing platform that makes it easy to run Python code.\n", "JupyterLab provides a notebook interface that allows you to run and edit Python commands interactively,\n", "which is the learning environment I want you to have while reading the rest of this tutorial.\n", "**JupyterLab Desktop** is a convenient all-in-one application\n", "that you can install on your computer to run JupyterLab.\n", "\n", "### STEP 1: Download JupyterLab Desktop\n", "Visit the web page\n", "[`github.com/jupyterlab/jupyterlab-desktop`](https://github.com/jupyterlab/jupyterlab-desktop#jupyterlab-desktop)\n", "and choose the download link for your operating system.\n", "Complete the installation steps.\n", "\n", "### STEP 2: Start JuputerLab Desktop\n", "Launch the JuputerLab Desktop application and choose the **New session...** option from the menu.\n", "You might be prompted to install a \"bundled Python environment,\" which you should accept.\n", "After this is done,\n", "you should see a window similar to the one shown below.\n", "\n", "![Screenshot of the JupyterLab user interface showing File Browser in the left sidebar and the main launcher window open. The button called Python 3 (ipykernel) is used to create a new Python 3 notebook.](https://raw.githubusercontent.com/minireference/noBSstats/main/tutorials/src/attachments/jupyter-lab-desktop-launcher.png)\n", "\n", "\n", "### STEP 3: Create a new notebook\n", "Click the **Python 3 (ipykernel)** button to create a new Jupyter notebook." ] }, { "cell_type": "markdown", "id": "1e6121d9-4a15-4b2a-b6eb-2928973e4417", "metadata": { "tags": [] }, "source": [ "## Notebooks are interactive documents\n", "\n", "A Jupyter notebook consists of a sequence of cells,\n", "similar to how a text document consists of a series of paragraphs.\n", "The notebook you created in STEP 3 has a single empty code cell,\n", "which is ready to accept Python commands.\n", "\n", "### STEP 4: Try a numerical calculation\n", "Enter the expression `2 + 3` into the code cell,\n", "then press **SHIFT+ENTER** to run the code.\n", "You should see the result of the calculation displayed on a new line immediately below your input.\n", "The cursor will automatically move to the next code cell,\n", "as shown in the screenshot below.\n", "\n", "![Screenshot showing the contents of Jupyter notebook with labels for the input and output of the first code cell. The screenshot also highlights the notebook execution control buttons: run, stop, restart, and run all.](https://raw.githubusercontent.com/minireference/noBSstats/main/tutorials/src/attachments/jupyter-lab-first-notebook.png)\n", "\n", "\n", "#### Notebook execution controls\n", "\n", "The menu bar at the top of the notebook contains the notebook execution control buttons.\n", "The **play** button executes the code in the current cell,\n", "and is equivalent to pressing **SHIFT+ENTER**.\n", "The **stop** and **restart** buttons\n", "can be used to interrupt a computation that is stuck or taking too long.\n", "The **run all** button is useful when you want to rerun all the cells in the notebook from start to finish." ] }, { "cell_type": "markdown", "id": "bef4b786-c7ab-4a5c-822a-0dea036071c1", "metadata": {}, "source": [ "## Code cells contain Python commands\n", "\n", "\n", "Each code cell in a notebook is a command prompt\n", "that allows you to enter Python commands and \"run\" them by pressing **SHIFT+ENTER**,\n", "or by clicking the **play** button in the toolbar.\n", "Let's see the example commands from screenshot again.\n", "We can make Python compute the sum of two numbers by entering `2+3` in a code cell,\n", "then pressing **SHIFT+ENTER**." ] }, { "cell_type": "code", "execution_count": 1, "id": "aee41fcb-590e-46c1-9a78-a42123e8039f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 + 3" ] }, { "cell_type": "markdown", "id": "c04a2057-1046-4078-a193-3e0d31c28493", "metadata": {}, "source": [ "When you *run* a code cell,\n", "you're telling the computer to **evaluate** the Python command it contains.\n", "Python then displays the result of the computation immediately below.\n", "In the above example,\n", "the input is the expression `2 + 3`,\n", "and the output is the number `5`.\n", "\n", "Let's now compute a more complicated math expression $(1+4)^2 - 3$.\n", "The Python equivalent of this math expression is shown below." ] }, { "cell_type": "code", "execution_count": 2, "id": "5a6aed0f-42af-4d97-948e-d5ead5a03dcc", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "22" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(1+4)**2 - 3" ] }, { "cell_type": "markdown", "id": "719096a2-490a-45e1-85ba-f7503a384f0b", "metadata": {}, "source": [ "Note the Python syntax for math operations is similar to the notation we use in math:\n", "addition is `+`, subtraction is `-`, multiplication is `*`, division is `/`, and we use the parentheses `(` and `)` to tell Python to compute `1+4` first. The syntax for exponents is a little unusual, we use two multiplication signs (asterisks) to denote exponentiation: $a^n$ = `a**n`.\n", "\n" ] }, { "cell_type": "markdown", "id": "4a483666-7524-47bc-8c37-5ade0bf6baa3", "metadata": {}, "source": [ "Running a code cell is similar to using the EQUALS button on a calculator:\n", "whatever math expression you entered,\n", "the calculator will compute its value and display its result.\n", "The interaction with the Python calculator is similar:\n", "you input some Python code,\n", "press **SHIFT+ENTER** and Python will run your code and display the result." ] }, { "cell_type": "markdown", "id": "7cc40c9c-1d3d-4afe-b3d4-24e770ee36b1", "metadata": {}, "source": [ "### STEP 5: Your turn to try!\n", "\n", "Type in some expression involving numbers and operations in a new code cell,\n", "then run it by pressing **SHIFT+ENTER** or by using the **play** button in the toolbar." ] }, { "cell_type": "code", "execution_count": null, "id": "d56aa3c9-1910-47dc-a1b4-ee1ff34d360d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "05458b3e-f3f7-4fec-b955-8a65f8c6dbba", "metadata": {}, "source": [ "### STEP 6: Download the tutorial notebook\n", "Click this link [`bit.ly/pytut3nb`](https://bit.ly/pytut3nb) to download the notebook `python_tutorial.ipynb`\n", "and save it to a permanent location on your computer\n", "(e.g. create a new folder `Documents/LearningPython/` and place the tutorial notebook in that folder).\n", "Switch back to the JupyterLab window\n", "and use the **File Browser** panel to navigate to the location on your computer where you saved the notebook `python_tutorial.ipynb`,\n", "then open it.\n", "\n", "If you are reading this in print or as a static document (web page or PDF),\n", "I expect you to **follow along with the notebook on your computer for the remainder of this tutorial**,\n", "so that you can play with the code examples interactively.\n", "The tutorial notebook contains many pre-filled code cells\n", "with examples of Python commands for you to run.\n", "I encourage you to edit the contents of the code cells\n", "and re-run them to see how the output changes.\n", "Remember that trial-and-error is the best way to learn Python!\n", "\n", "\n", "#### Cell operations menu\n", "In the top-right corner of each code cell,\n", "you'll find a useful set of cell operations.\n", "\n", "![Cell operation menu. Use these buttons to move the current cell, duplicate it, or add a new cell above or below the current cell.](https://raw.githubusercontent.com/minireference/noBSstats/main/tutorials/src/attachments/cell-operations.png)\n", "\n", "You can use **insert new cell below** button\n", "to create a blank code cell at any place in the notebook\n", "and experiment with some variation of the code you are learning.\n", "Try running the code with different numbers,\n", "or come up with your own examples.\n", "**Think of this notebook as your personal playground for exploring Python commands.**\n", "Don't worry you can't break anything!\n", "\n", "\n", "#### Learning on hard mode\n", "If you are feeling particularly ambitious,\n", "you can try manually typing-out all the code examples into a new notebook.\n", "In general,\n", "I recommend that you type things out by yourself rather than relying on copy-paste.\n", "It's good to practice the speed of your typing skills\n", "and develop the \"muscle memory\" for entering special characters like `[`, `]`, `{`, `}`, `>`, `<`, `#`, etc.\n", "\n", "\n", "\n", "" ] }, { "cell_type": "markdown", "id": "79f306e8-dd3a-4db5-94a8-1a4aa0ba4102", "metadata": {}, "source": [ "# Expressions and variables\n", "\n", "Most Python commands involve computing the value of some Python expression\n", "and storing the resulting value in a variable.\n", "If our goal is to learn Python,\n", "then we better start by explaining what Python expressions are\n", "and how to storing values into variables works." ] }, { "cell_type": "markdown", "id": "665e4567-4fc1-4e0e-b49a-38ac3a543d2d", "metadata": {}, "source": [ "## Expressions\n", "\n", "A Python expression is an arbitrary combination of values, operators, and function calls.\n", "We write expressions and ask Python to compute their value.\n", "Here is an example of an expression involving several math operations,\n", "like the ones we saw earlier." ] }, { "cell_type": "code", "execution_count": 3, "id": "5a9ba864-b931-4b4f-9eba-853c528f9067", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5**2 - 3*7" ] }, { "cell_type": "markdown", "id": "13e413d7-ea52-4324-ae42-5dca9fc56a76", "metadata": {}, "source": [ "Let's now look at a fancier expression\n", "that shows other aspects of the Python syntax\n", "like lists and function calls.\n", "The following expression computes the sum of a list of three numbers using the function `sum`." ] }, { "cell_type": "code", "execution_count": 4, "id": "82a01e67-d23b-49f7-b29f-68fae23353c7", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum( [1,2,3] )" ] }, { "cell_type": "markdown", "id": "0f04977b-585a-44fa-86d1-ab2a17c1490c", "metadata": {}, "source": [ "In Python,\n", "we define lists using the square brackets `[` and `]`.\n", "The code `[1,2,3]` defines a list of three elements: `1`, `2`, and `3`.\n", "The function `sum` computes the sum of the list of numbers.\n", "The parentheses `(` and `)` are used to delimit the input to the function.\n", "We'll have a lot more to say about lists and functions later on in this tutorial.\n", "For now,\n", "I just wanted to show you an expression that involves\n", "something other than arithmetic operations,\n", "as a preview of what is to come.\n", "\n", "" ] }, { "cell_type": "markdown", "id": "2920b7af-4076-4310-906b-070428c37cfc", "metadata": { "tags": [] }, "source": [ "## Python comments\n", "\n", "Comments are annotations in code cells intended for human readers.\n", "We write comments in Python by prefixing them with the character `#`.\n", "Python will ignore the `#` character and any text that comes after it on that line." ] }, { "cell_type": "code", "execution_count": 5, "id": "020ea4fd-c984-4723-8d5e-f9969430d631", "metadata": {}, "outputs": [], "source": [ "# this is a comment" ] }, { "cell_type": "markdown", "id": "3b480e55-a8a4-4cd2-bb15-34b2f458d6b4", "metadata": {}, "source": [ "The text \"`# this is a comment`\" is ignored by the Python interpreter.\n", "As far as Python is concerned,\n", "the above code cell is completely empty.\n", "I'll use comments in this tutorial\n", "to provide an additional \"narrative\" for each line of code.\n", "\n", "At first I'll use comments at the literal level\n", "to explain how to read the code (What).\n", "I'll also use comments to explain what special syntax does (How).\n", "Towards the end of the tutorial,\n", "the comments will switch to explaining the intent of each command (Why).\n", "\n", "" ] }, { "cell_type": "markdown", "id": "84b5250b-e095-4c88-af4b-99d007d0e1e4", "metadata": {}, "source": [ "## Variables\n", "\n", "A variable is a **name** we use to refer to some value.\n", "This is similar to how variables are used in math.\n", "Variables are used to represent constants, function inputs, function outputs,\n", "and all kinds of intermediate values in calculations.\n", "We use the assignment operator `=` to store values into variables.\n", "\n", "" ] }, { "cell_type": "markdown", "id": "04159e5c-6062-420b-9186-a6dd81127208", "metadata": { "tags": [] }, "source": [ "### The assignment operator" ] }, { "cell_type": "markdown", "id": "da99271c-dea8-453e-99c0-87219dad6684", "metadata": {}, "source": [ "In all the code examples above,\n", "we computed various Python expressions,\n", "but we didn't do anything with the results.\n", "The more common pattern in Python\n", "is to **store the result of an expression evaluation into a variable**.\n", "\n", "For example, here is the code that computes the expression `2+3`\n", "and stored the result in a variable named `x`." ] }, { "cell_type": "code", "execution_count": 6, "id": "2c31a7d6-c751-4151-8e07-69833bbdbe95", "metadata": {}, "outputs": [], "source": [ "x = 2+3 # calculate 2+3 and store the result into x" ] }, { "cell_type": "markdown", "id": "2eba69d4-b7e3-46e9-ab60-62addf264ee6", "metadata": {}, "source": [ "The result of this expression is that the value `5` gets saved in the variable `x`.\n", "Python evaluates this command from right-to-left:\n", "Python first looks at the expression `2+3` and computes its result `5`,\n", "then it sees the remainder of the statement `x =`,\n", "which means we want to store the result into the variable `x`.\n", "The assignment statement doesn't have any output,\n", "so running this code cell doesn't display anything." ] }, { "cell_type": "markdown", "id": "fc3e1bd0-852f-445f-bead-f637bd11cea7", "metadata": {}, "source": [ "To display the contents of the variable `x`,\n", "we can write its name in a code cell." ] }, { "cell_type": "code", "execution_count": 7, "id": "25c39e4c-4134-4d3f-a328-5bfd72e8fab8", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x # print x" ] }, { "cell_type": "markdown", "id": "92af9b26-c142-45f0-a1d4-9cee99bd57f3", "metadata": {}, "source": [ "Note the information about the variable `x` is available in this code cell,\n", "even though we defined it in the previous code cell.\n", "This is the whole point of the computational notebook environment:\n", "**every code cell runs in a \"context\" that includes the results from all the previous code cells**.\n", "When you use the **run all** button in the toolbar,\n", "and read the notebook from top-to-bottom,\n", "it's as if you're telling a story,\n", "which define different characters (variables) and show them in action (computations).\n", "\n", "Note also we didn't need to call any special function to display the contents of the variable. This is because, in a notebook interface, the value of **the last expression in each code block gets displayed automatically**.\n", "Later in the tutorial, we'll learn about the function `print` that you can use to display values in the middle of a code cell or to customize how values are displayed." ] }, { "cell_type": "markdown", "id": "b8160009-dd87-411a-a040-68b33c7d82ad", "metadata": {}, "source": [ "We can combine the commands \"assign the result of `2+3` to `x`\" and \"display `x`\" into a single code cell, as shown below." ] }, { "cell_type": "code", "execution_count": 8, "id": "9db5fbc6-7464-4a49-9f4c-fbc28aa8d8f4", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 2+3 # store 2+3 into x\n", "x # print x" ] }, { "cell_type": "markdown", "id": "acd78c45-95d5-433a-9d13-5ae6e9dfbc7d", "metadata": {}, "source": [ "The first line in this code block computes the expression `2+3`\n", "and assigns the result to the variable `x`.\n", "The second line evaluates the variable `x` to display its contents." ] }, { "cell_type": "markdown", "id": "3ca1a5e0-34a9-4a92-bd14-99956747ef32", "metadata": {}, "source": [ "### Exercise 1: using the assignment operator\n", "\n", "Imitate the above code block, to create another variable `y` that contains the value of the expression $(1+4)^2 - 3$, then print the contents of the variable `y` to show the result." ] }, { "cell_type": "code", "execution_count": 9, "id": "979eec9d-eaaa-405e-9a9f-32a3ba4c3a50", "metadata": {}, "outputs": [], "source": [ "# Instructions: put your answer in this code cell\n" ] }, { "cell_type": "markdown", "id": "04f3ebbf-02aa-44dd-9e7b-b767e088ad45", "metadata": {}, "source": [ "Click for solution.\n" ] }, { "cell_type": "markdown", "id": "5f831b20-c8c2-4fb4-a562-1f9a67b134f9", "metadata": {}, "source": [ "### Reading the assignment operator\n", "\n", "\n", "The meaning of the assignment operator `=`\n", "is not the same as the meaning of the operator $=$ used for writing math equations,\n", "so you should *not* read `x = 5` as \"x equals 5.\"\n", "The assignment statement `x = 5` tells Python to\n", "store the value `5` (the right-hand side)\n", "into the variable `x` (the left-hand side),\n", "so the correct way to read this statement is,\n", "\"set `x` to `5`\", \"Put `5` into `x`\", \n", "or \"record `5` under the name `x`\".\n", "The symbol `=` is not a passive statement of equality,\n", "but an active verb telling Python to store the result of an expression\n", "into a particular variable.\n", "\n", "\n" ] }, { "cell_type": "markdown", "id": "25cc03f3-09ca-41b5-9f5f-10045261da51", "metadata": {}, "source": [ "To summarize,\n", "the syntax of an assignment statement is as follows:\n", "\n", "```Python\n", " = \n", "```\n", "\n", "The assignment operator \"`=`\"is used to store the value of the expression `` into the memory location ``, which is usually a variable name. Later in this tutorial, we'll learn how to store values in other places (inside containers like lists and dictionaries)." ] }, { "cell_type": "markdown", "id": "6a634670-2ddd-4bc5-acfb-80017c02b9ce", "metadata": { "tags": [] }, "source": [ "## Using multi-line expressions for step-by-step calculations\n", "\n", "Okay so how does storing values into variables help us with real-world practical calculations?\n", "Let me show you an example of a complicated calculation that we can do easily in Python.\n", "\n", "" ] }, { "cell_type": "markdown", "id": "31d09853-6ddf-4633-afd2-ac3224b15408", "metadata": {}, "source": [ "### Example 1: number of seconds in one week\n", "\n", "Let's say we need to calculate how many seconds there are in one week.\n", "We know there are $60$ seconds in one minute,\n", "$60$ minutes in one hour,\n", "$24$ hours in one day,\n", "and $7$ days in one week.\n", "We can calculate the number of seconds in one week step by step,\n", "where in each step we perform a very simple calculation.\n", "We'll define several intermediate variables\n", "and give the variable descriptive names\n", "to help us keep track of what is going on." ] }, { "cell_type": "code", "execution_count": 11, "id": "7ede33a7-e8d8-4c4f-b99e-33c60b79906c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "604800" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "one_min = 60 # number of seconds in one minute\n", "one_hour = one_min * 60 # calc. number of sec in one hour \n", "one_day = one_hour * 24 # calc. number of sec in one day \n", "one_week = one_day * 7 # calc. number of sec in one week \n", "one_week # print the value of one_week" ] }, { "cell_type": "markdown", "id": "99badcda-6d2a-420a-87bf-a6e173b39c3f", "metadata": {}, "source": [ "Note we can use the underscore `_` as part of variable names.\n", "This is a [common pattern](https://en.wikipedia.org/wiki/Snake_case) for variable names in Python code,\n", "because the name `some_name` is easier to read than `somename`." ] }, { "cell_type": "markdown", "id": "11df42a3-9be0-485c-ba7c-03c474562ce0", "metadata": {}, "source": [ "Okay, let's see if you can do this now.\n", "Here are some real-world calculations you might want to perform.\n", "Let's see if you can use what we have learned about Python variables and expressions\n", "to solve these exercises on your own." ] }, { "cell_type": "markdown", "id": "460c613f-6e81-4f43-82de-9617566b7259", "metadata": {}, "source": [ "### Exercise 2: weight conversion\n", "\n", "Johnny weights 107 kg, and wants to know his weight in pounds.\n", "One kilogram is equivalent to 2.2 lbs.\n", "Can you write the Python expression for computing Johnny's weight in pounds?\n", "\n", "I have already created a code cell where you can write your answer,\n", "and even started the code for you\n", "by defining the variable `weight_in_kg` which contains the weight in kilograms.\n", "To complete this exercise,\n", "I want you to replace the `...` placeholder with a Python expression\n", "that uses the variable `weight_in_kg`." ] }, { "cell_type": "code", "execution_count": 12, "id": "f86b43af-81dd-45f3-bcdd-89051ba33dfb", "metadata": {}, "outputs": [], "source": [ "# Instructions: replace ... with your answer\n", "\n", "weight_in_kg = 107 # store 107 into weight_in_kg\n", "weight_in_lb = ..." ] }, { "cell_type": "markdown", "id": "8c99ac7e-1037-4627-8108-6dd256e31f6c", "metadata": {}, "source": [ "Click for solution.\n" ] }, { "cell_type": "markdown", "id": "30911d22-dc5f-4946-940b-77da250a6db0", "metadata": {}, "source": [ "### Exercise 3: tax calculation\n", "\n", "You're buying an item at the store and the price is $\\$50$ dollars.\n", "The government imposes a $10\\%$ tax on your purchase.\n", "Calculate the total you'll have to pay,\n", "including the $10\\%$ tax." ] }, { "cell_type": "code", "execution_count": 14, "id": "237560d6-99f1-4ae1-a24f-896bbe07b26d", "metadata": {}, "outputs": [], "source": [ "# Instructions: replace the ...s with the appropriate calculations\n", "\n", "price = 50.0\n", "taxes = ...\n", "total = ..." ] }, { "cell_type": "markdown", "id": "8ffc6245", "metadata": {}, "source": [ "Click for solution.\n" ] }, { "cell_type": "markdown", "id": "3037b7cf", "metadata": {}, "source": [ "### Exercise 4: temperature conversion\n", "\n", "The formula for converting temperatures from Celsius to Fahrenheit\n", "is $F = \\tfrac{9}{5} \\cdot C + 32$.\n", "Given the variable `C` that contains the current temperature in Celsius,\n", "write the expression that calculates the current temperature in Fahrenheit and store the answer in a new variable named `F`." ] }, { "cell_type": "code", "execution_count": 16, "id": "3210ba16-950d-4120-87cb-5327aeb40e82", "metadata": {}, "outputs": [], "source": [ "# Instructions: replace the ... with a Python expression\n", "\n", "C = 20\n", "F = ..." ] }, { "cell_type": "markdown", "id": "c3046415-7507-476b-8188-a6fdbb5cc33b", "metadata": {}, "source": [ "Check your formula works by changing the value of `C`,\n", "and re-running the code cell.\n", "When `C = 100`, the temperature in Fahrenheit should be `212.0`." ] }, { "cell_type": "markdown", "id": "3ff6d575-93f4-41b4-b433-2c71938f7cfc", "metadata": {}, "source": [ "Click for solution.\n" ] }, { "cell_type": "markdown", "id": "363344af-c5e9-4c8b-8cf5-a8fcf4cc95da", "metadata": { "tags": [] }, "source": [ "## Variable types\n", "\n", "Every variable in Python has a *type*,\n", "which tells you what kind of data it contains,\n", "and what kind of operations you can do with it.\n", "There are two types of variables for storing numbers,\n", "and another type for storing text.\n", "There are also several types of \"container variables\"\n", "like lists and dictionaries.\n", "\n", "Here is a list of the most common types of variables in Python:\n", "\n", "- **Integers** (`int`): used to store whole numbers like\n", " `42`, `65`, `78`, `-4`, `-200`, etc.\n", " Python integers are roughly equivalent to math set of integers $\\mathbb{Z}$.\n", "- **Floating point numbers** (`float`): used to store decimals like\n", " `4.6`,`78.5`, `1000.0` = `1e3`, `0.001` = `1e-3`, `123.456` = `1.23456e2`, etc.\n", "- **Lists** (`list`): ordered container for other values.\n", " For example, the list `[61, 79, 98, 72]` contains four integers.\n", " The beginning and the end of the list are denoted by the square brackets `[` and `]`,\n", " and its elements are separated by commas.\n", "- **Strings** (`str`): used to store text like `\"Hello\"`, `\"Hello everyone\"`.\n", " Strings are denoted using either double quotes `\"Hi\"` or single quotes `'Hi'`.\n", "- **Boolean values** (`bool`): logic variables with only two possible values,\n", " `True` or `False`." ] }, { "cell_type": "markdown", "id": "f681849b-5910-474d-8ca0-08d8baf87b0d", "metadata": {}, "source": [ "Let's look at some examples of the different types of variables.\n", "" ] }, { "cell_type": "code", "execution_count": 18, "id": "cf6eba8f", "metadata": {}, "outputs": [], "source": [ "score = 98 # an int\n", "average = 77.5 # a float\n", "scores = [61, 79, 98, 72] # a list\n", "message = \"Hello everyone\" # a str\n", "above_the_average = True # a bool" ] }, { "cell_type": "markdown", "id": "4578b957-6946-4db2-9a85-88decf12d0fa", "metadata": {}, "source": [ "To see the contents of the variable `score`,\n", "we simply type its name." ] }, { "cell_type": "code", "execution_count": 19, "id": "298f5e87-1588-4d1d-936f-f416725ec033", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "98" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "score" ] }, { "cell_type": "markdown", "id": "406b2440-a0b5-450e-903c-fd9707b865b0", "metadata": {}, "source": [ "What type of variable is the variable `score`?\n", "To see the *type* of a variable,\n", "we can call the function `type` like this:" ] }, { "cell_type": "code", "execution_count": 20, "id": "727080f6-ea61-45e1-99de-1ae8e6a31969", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(score)" ] }, { "cell_type": "markdown", "id": "81534283-5651-47b3-8551-2b37855ab41c", "metadata": {}, "source": [ "The output tells us that `score` is of type `int` (an integer)." ] }, { "cell_type": "markdown", "id": "67eb671b-130b-4ca2-9de7-adffde6b5084", "metadata": {}, "source": [ "### Exercise 5: values and types of variables\n", "\n", "Print the *contents* and the *type* of the variables\n", "`average`, `scores`, `message`, and `above_the_average` that were defined above." ] }, { "cell_type": "code", "execution_count": 21, "id": "089a1e45-5d10-43a0-a66f-d06492af0b6f", "metadata": {}, "outputs": [], "source": [ "# Instructions: Use this code cell to show the contents of each variable\n", "# then use the function `type` to print its type.\n", "\n" ] }, { "cell_type": "markdown", "id": "6fe84d2f", "metadata": {}, "source": [ "Click for solution.\n" ] }, { "cell_type": "markdown", "id": "fd28e6b0-6a29-484b-a81f-b4e111041af5", "metadata": { "jp-MarkdownHeadingCollapsed": true, "tags": [] }, "source": [ "### Technical jargon: objects and methods\n", "\n", "\n", "The technical term *object* is used to describe the fundamental building blocks we use to store data when programming.\n", "We store different types of data into different types of objects,\n", "and the `int`egers, `float`ing point numbers, `list`s, `str`ings, and `bool`ean values\n", "are examples of different types of objects.\n", "Calling the function `type` on the object `obj`, `type(obj)`, tells us the type of the object `obj`.\n", "\n", "\n", "Objects have certain functions \"attached\" to them, which we call *methods*.\n", "Different types of objects have different kinds of methods,\n", "which make it easy to manipulate the data stored in the object.\n", "Earlier,\n", "we defined the Python `list` object `scores = [61,79,98,72]`,\n", "which is a container that holds four numbers.\n", "The list `scores` has methods like `.append`, `.insert`, `.extend`, `.reverse`, `.sort`, etc.\n", "These methods allow us to perform operations on the underlying data in the list.\n", "For example,\n", "if we want to sort the values in the list in increasing order,\n", "we can call the method `scores.sort()` and the list will become `[61,72,79,98]`.\n", "\n", "\n", "Every value in Python is an *object*.\n", "Learning Python requires being familiar with the different object types and their method.\n", "Later on in this tutorial,\n", "we'll have a whole section on lists where we'll describe their methods and use cases.\n", "There are also sections that cover boolean values, strings, and dictionaries.\n", "The key point to remember is that Python objects\n", "are like different types of LEGOs available for you to play with,\n", "and remember that you can use the function `type`\n", "to find out what kind of object you're working with." ] }, { "cell_type": "markdown", "id": "139e0683-9e47-45e3-a0e2-95877e73eaa4", "metadata": {}, "source": [ "# Functions\n", "\n", "Functions are the essential building blocks of Python programs.\n", "Functions allow us to encapsulate any sequence of operations\n", "as a reusable piece of functionality.\n", "You can think of a function as a chunk of code that you define once,\n", "then use multiple times by \"calling\" it from other places your code." ] }, { "cell_type": "markdown", "id": "a0d52a84-803e-4ecc-ac52-b857d2762766", "metadata": {}, "source": [ "## Calling functions\n", "\n", "The Python syntax for calling the function named \"`fun`\" with the input \"`arg`\"\n", "is to wrap the input in parentheses: `fun(arg)`.\n", "The syntax for calling functions in Python\n", "is borrowed from the math notation $f(a)$,\n", "which is used to describe evaluating the function $f$ on the input $a$.\n", "A Python function takes certain variable(s) as input\n", "and produces a certain variable as output.\n", "In programming,\n", "function inputs are called *arguments*\n", "and the output of the function is called its *return value*." ] }, { "cell_type": "markdown", "id": "1141e5bc-6662-45fe-bdf5-f447552a89a9", "metadata": {}, "source": [ "## Python built-in functions\n", "\n", "Python functions are like the different calculator buttons\n", "you can press to perform computations.\n", "Here is a list of the most common Python functions:\n", "\n", "- `type(obj)`: tells us the type of the object `obj`.\n", "- `sum(mylist)`: calculates the sum of the values in the list `mylist`.\n", "- `print(obj1,obj2,...)`: displays the objects `obj1`, `obj2`, etc. in the output cell.\n", "- `len(mylist)`: returns the length of the list object `mylist`.\n", "- `help(obj)`: displays help information about the object `obj`.\n", "- `range(a,b)`: creates the list of numbers `[a,a+1,...,b-1]`.\n", "- `str(obj)`: converts the object `obj` into text (`str`).\n", "\n", "You'll learn to use all these functions (and many others) in later sections of this tutorial." ] }, { "cell_type": "markdown", "id": "3385bba3-c07c-44a0-930a-656f215ef532", "metadata": {}, "source": [ "## Multiple arguments\n", "\n", "Some functions can accept multiple arguments,\n", "and we use commas to separate these arguments:\n", "`fun(arg1,arg2,arg3)`.\n", "For example,\n", "we can call the function `print` with several arguments to display them together on a single line." ] }, { "cell_type": "code", "execution_count": 23, "id": "72abf873-08c2-4a47-a323-078b62390033", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The average is 77.5 percent.\n" ] } ], "source": [ "print(\"The average is\", average, \"percent.\")" ] }, { "cell_type": "markdown", "id": "3e638013-675d-4a93-9ba7-c7c8d5f8d78a", "metadata": {}, "source": [ "## Keyword arguments\n", "\n", "Some functions accept optional *keyword arguments* (also called *options*)\n", "that modify the function's behaviour.\n", "For example,\n", "the `print` function accepts the keyword argument `sep` (separator)\n", "that controls what text is used to separate the different values you want to print.\n", "The default value for the option `sep` is a single space `\" \"`,\n", "which is why the number `77.5`\n", "appears separated by spaces from the text in the output above.\n", "\n", "We can specify a different value for the `sep` keyword argument,\n", "if we want to use a different separator, as shown below." ] }, { "cell_type": "code", "execution_count": 24, "id": "87991ad0-6269-4a50-ab83-c2bd987adfb6", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "average=77.5\n" ] } ], "source": [ "print(\"average\", average, sep=\"=\")" ] }, { "cell_type": "markdown", "id": "4a597cb4-750b-4a28-940c-ef39ec3fe89b", "metadata": {}, "source": [ "## Python syntax for defining new functions\n", "\n", "Python makes it easy to define functions,\n", "which is like adding new buttons to the calculator.\n", "To define a new function called `fun`,\n", "we use the following syntax:\n", "\n", "```Python\n", "def fun(arg): # define the function `fun` that takes `arg` as input\n", " # first line of calculations\n", " # second line of calculations \n", " return # specify the output value of the function\n", "```\n", "\n", "You can think of this function definition as adding the new button `fun` to the Python calculator.\n", "Let's go over the code example above line-by-line\n", "to explain all the new elements of syntax.\n", "We start with the Python keyword `def`,\n", "then give the name of the function we want to define,\n", "which is `fun` in this case.\n", "Next,\n", "we specify the arguments that the function expects inside parentheses.\n", "In this example,\n", "the function `fun` takes a single input called `arg`.\n", "The colon `:` indicates the beginning of the function body.\n", "The function body is an *indented code block* (each line begins with four spaces)\n", "that specifies the calculations that the function is supposed to perform on the input `arg`.\n", "The last line in the function body is a `return` statement\n", "that tells us the output of the function (its *return value*).\n", "\n", "Once we have defined the function `fun` using the `def`-syntax above,\n", "we can now call the function in code cells below this one.\n", "For example,\n", "calling `fun(5)` will perform the calculations steps 1 and 2 on the value `5`,\n", "then return the value output calculated from this input.\n", "In other words,\n", "anywhere you see the function call expression `fun(5)`,\n", "you can mentally replace it with the output value computed by `fun` when `arg` is `5`.\n", "I know this may seem complicated as hell at first sight,\n", "but I assure you that it will make sense after we look at some examples." ] }, { "cell_type": "markdown", "id": "bc6cecdd-23b2-4a2d-8aed-4efd0beddb80", "metadata": {}, "source": [ "### Example 2: adding the `double` button to the calculator\n", "\n", "Let's define a new Python function called `double`,\n", "which takes a number as input and multiplies the number by two." ] }, { "cell_type": "code", "execution_count": 25, "id": "6d4e3530", "metadata": {}, "outputs": [], "source": [ "def double(x): # define the function `double` with input `x`\n", " y = 2*x # compute 2 times `x` and store the result in `y`\n", " return y # return the value of `y` as output" ] }, { "cell_type": "markdown", "id": "d79d687b-2b95-4dcb-ad7f-da90b39ab1bb", "metadata": {}, "source": [ "Note the body of the function (the second and third lines) is indented by four spaces.\n", "In Python,\n", "we use indentation to delimit when a code block starts and when it ends.\n", "In the above example,\n", "function's body contains two lines of code,\n", "but in general,\n", "Python functions can contain dozens or even hundreds of lines of code:\n", "any sequence of operations you might want to perform on the input `x`\n", "to calculate the output `y`." ] }, { "cell_type": "markdown", "id": "2d867494-881b-48f9-8a7e-6531f272f16d", "metadata": {}, "source": [ "To call the function `double`,\n", "we use the function name followed by the\n", "function's input argument in parentheses,\n", "as we saw earlier." ] }, { "cell_type": "code", "execution_count": 26, "id": "680f7ed9", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "12" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "double(6) # call the function `double` on the input `6`" ] }, { "cell_type": "markdown", "id": "14b83bea-7e4e-4123-b971-3f34a3416298", "metadata": {}, "source": [ "Let's narrate what happened in the above code cell from Python's perspective.\n", "When Python sees the expression `double(6)` it recognizes it as a function call.\n", "It then looks for the definition of the function `double`\n", "and finds the `def` statement in the previous code cell,\n", "which tells it what steps need to be performed on the input.\n", "It then performs the steps in the function body with the value `6` stored in the variable `x`,\n", "which results in `12` as the return value.\n", "Visit the link [`tinyurl.com/bdzr69s9`](https://tinyurl.com/bdzr69s9)\n", "to see a visualization of what happens when we call the function `double`\n", "with different inputs." ] }, { "cell_type": "markdown", "id": "cc1b3bc1-9947-4d62-a702-da823d14e93f", "metadata": {}, "source": [ "### Example 3: tax calculating function\n", "\n", "Let's add a function that adds 10\\% tax to a given purchase `price`\n", "and returns the total of the price plus taxes." ] }, { "cell_type": "code", "execution_count": 27, "id": "42d6774a", "metadata": {}, "outputs": [], "source": [ "def add_taxes(price): # define the function `add_taxes` with input `price`\n", " taxes = 0.10 * price # compute 10% of price and store in taxes\n", " total = price + taxes # compute the total\n", " return total # return the value in `total`" ] }, { "cell_type": "markdown", "id": "d44f5a79-b64a-47fc-88ad-ec2a56c2ce33", "metadata": {}, "source": [ "We can now use the function `add_taxes` to calculate total cost\n", "of purchasing different kinds of items." ] }, { "cell_type": "code", "execution_count": 28, "id": "ffb9ee76-533a-49c8-80cb-70719fd9b4cf", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "55.0" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "add_taxes(50) # calculate the total cost of purchasing a $50 item" ] }, { "cell_type": "code", "execution_count": 29, "id": "3ab08765-b554-4d97-bafc-3614890157b6", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "110.0" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "add_taxes(100) # calculate the total cost of purchasing a $100 item" ] }, { "cell_type": "markdown", "id": "13f188a7-4d80-46db-87e6-1485bd08b09b", "metadata": {}, "source": [ "Try to narrate what happened when Python evaluated the above function call expressions.\n", "You can imitate the narrative from the point of view of Python\n", "like the one for the function call `double(6)`,\n", "but applied to the function calls `add_taxes(50)` and `add_taxes(100)`.\n", "This is an important exercise that I highly recommend\n", "so you'll become comfortable with the semantics of function calls.\n", "Click this link [`tinyurl.com/muufc5cn`](https://tinyurl.com/muufc5cn)\n", "to see a visualization of the `add_taxes` function calls." ] }, { "cell_type": "markdown", "id": "9df04daf-4d51-49f5-a5db-ce7c2aece223", "metadata": {}, "source": [ "### Example 4: calculating the mean of a list of numbers\n", "\n", "The formula for computing the mean (average value) of a list of numbers\n", "$[x_1, x_2, \\ldots, x_n]$ is\n", "$\\overline{\\mathbf{x}} = \\left( x_1 + x_2 + \\cdots + x_n \\right) / n$.\n", "In words,\n", "the average value is the sum of the values,\n", "divided by the length of the list.\n", "Let's write the function `mean` that computes the mean of a list `values`." ] }, { "cell_type": "code", "execution_count": 30, "id": "a2752b71", "metadata": {}, "outputs": [], "source": [ "def mean(values): # define the function `mean` with input `values`\n", " n = len(values) # compute the length of the list\n", " avg = sum(values) / n # compute the average using the formula\n", " return avg # return the value in `avg`" ] }, { "cell_type": "markdown", "id": "be068abc-401b-4a7c-8ff7-41a555ed6958", "metadata": {}, "source": [ "The calculations this function performs\n", "are the same as the math formula.\n", "Let's try the function on a list of numbers." ] }, { "cell_type": "code", "execution_count": 31, "id": "e4da20dc-5c18-48a0-a487-194716256c20", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.5" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mean([1,2,3,4]) # calculate the mean of the values [1,2,3,4]" ] }, { "cell_type": "markdown", "id": "948798c1-35cd-48f7-93fd-71119a94f443", "metadata": {}, "source": [ "Indeed, $(1+2+3+4)/4 = 10/4 = 2.5$, so the function seems to be working as expected." ] }, { "cell_type": "markdown", "id": "6b6b939c-7e65-4e3c-a9c0-7255e5b13592", "metadata": {}, "source": [ "### Exercise 6: temperature conversion function\n", "\n", "Define a Python function called `temp_C_to_F` that converts\n", "temperatures from Celsius `C` to Fahrenheit `F`.\n", "The formula for converting a temperature in Celsius\n", "to a temperature in Fahrenheit\n", "is $F = \\tfrac{9}{5} \\cdot C + 32$.\n", "\n", "Hint: You can reuse the code from **Exercise 4**." ] }, { "cell_type": "code", "execution_count": 32, "id": "cbdb8946-991b-4851-b9fd-043ebf245d92", "metadata": {}, "outputs": [], "source": [ "# Instructions: replace ... with your answer\n", "\n", "def temp_C_to_F(C):\n", " ...\n" ] }, { "cell_type": "markdown", "id": "78964c7e", "metadata": {}, "source": [ "Click for solution.\n" ] }, { "cell_type": "markdown", "id": "eb7b0ce3-f857-49b0-b59f-1935fc2e7517", "metadata": {}, "source": [ "There is a lot more to learn about Python functions,\n", "but there are also other topics I want to show you,\n", "so we'll have to move on now." ] }, { "cell_type": "markdown", "id": "e0752b0b-2d68-4a03-8a9f-539f566a1e17", "metadata": {}, "source": [ "# Python lists and `for`-loops\n", "\n", "Python lists allow us to easily manipulate thousands or millions of values.\n", "A `for`-loop is a programming construct used to repeat some operation multiple times.\n", "We often use `for`-loops to perform a calculation for each element in a list.\n", "We'll now describe how Python lists work,\n", "then describe `for`-loops in the next section." ] }, { "cell_type": "markdown", "id": "499f5589-9568-43a3-ba6c-ed43b3df568e", "metadata": {}, "source": [ "## Lists\n", "\n", "The Python syntax for creating a list starts with an opening square bracket `[`,\n", "followed by the list elements separated by commas `,`,\n", "and ends with a closing bracket `]`.\n", "For example,\n", "here is how to define the list `scores` that contains four integers:" ] }, { "cell_type": "code", "execution_count": 34, "id": "f76410af", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[61, 79, 98, 72]" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "scores = [61, 79, 98, 72] # define a list of four numbers\n", "scores" ] }, { "cell_type": "markdown", "id": "cba309bc-7968-4f12-a0e1-83a1cfe57a72", "metadata": {}, "source": [ "A list has a length,\n", "which you can obtain by calling the function `len` on it." ] }, { "cell_type": "code", "execution_count": 35, "id": "3847db1a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(scores) # compute the length of the list `scores`" ] }, { "cell_type": "markdown", "id": "a17d4c4c-67ac-43dc-8772-7a9a7b9cc48b", "metadata": {}, "source": [ "We use the `in` operator to check if a list contains a certain element." ] }, { "cell_type": "code", "execution_count": 36, "id": "c10641d1-0b67-4848-81a2-03a3f7fafb39", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "98 in scores # Is the number 98 contained in list `scores`?" ] }, { "cell_type": "markdown", "id": "42fc27e9-2762-47d1-8de9-7a9abaa7c9ff", "metadata": {}, "source": [ "The result is the boolean value `True`,\n", "which means the answer is:\n", "\"Yes, the number 98 is contained in the list `scores`.\"" ] }, { "cell_type": "markdown", "id": "e7cd103d-861c-4be0-acc4-db1bf6d890f1", "metadata": {}, "source": [ "### Accessing elements of a list\n", "\n", "We access the individual elements of the list `scores`\n", "using the square brackets syntax `scores[]`,\n", "where `` is the `0`-based index of the element we want to access.\n", "You can think of indices as analogous to the street numbers used to address different locations on a street.\n", "\n", "![Visualization of the Python list `scores` that shows the indices 0, 1, 2, 3 and the values 61, 79, 98, 72 of the four elements](https://raw.githubusercontent.com/minireference/noBSstats/main/tutorials/src/attachments/vizualization_of_scores_list.png)\n", "\n", "The visualization above shows the different indices and values of the elements in the list `scores`." ] }, { "cell_type": "markdown", "id": "6f918292-ecdb-42e4-b471-2809b8d95ca1", "metadata": {}, "source": [ "The first element of the list has index `0`,\n", "the second element has index `1`,\n", "and so on. \n", "The last element has an index equal to the length of the list minus one." ] }, { "cell_type": "code", "execution_count": 37, "id": "ae78304a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "61" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "scores[0] # first element in the list `scores`" ] }, { "cell_type": "code", "execution_count": 38, "id": "a89f88b9-88ce-4836-a78a-a4fda0c8762a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "79" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "scores[1] # second element in the list `scores`" ] }, { "cell_type": "code", "execution_count": 39, "id": "518f8065-6447-4398-bc39-d65cd5b3536c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "72" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "scores[3] # last element in the list `scores`" ] }, { "cell_type": "markdown", "id": "06b13868-3217-43a2-b3d2-dd0d49a87f15", "metadata": {}, "source": [ "Note the semantics used to *access* list elements\n", "is completely separate from the semantics used to *define* lists,\n", "even though both are based on the square brackets syntax `[` and `]`.\n", "The `[` and `]` in the expression `[1,2,3]` means \"create a list,\"\n", "while the `[` and `]` in `mylist[0]` means \"get the first element of `mylist`.\"" ] }, { "cell_type": "markdown", "id": "1404bff9-7888-43b5-8951-6eedacbf2324", "metadata": {}, "source": [ "### List slicing\n", "\n", "You can extract a subset of a list using the \"slice\" syntax `a:b`,\n", "which corresponds to the range of indices `a`, `a+1`, ..., `b-1`.\n", "For example,\n", "if you want to extract the first three elements in the list `scores`,\n", "use the slice `0:3`,\n", "which is equivalent to the indices `0`, `1`, and `2`." ] }, { "cell_type": "code", "execution_count": 40, "id": "a6110de9", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[61, 79, 98]" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "scores[0:3] # first three elements of the list `scores`" ] }, { "cell_type": "markdown", "id": "1d0df200-edcc-4cea-b6ba-10991d2ae66e", "metadata": {}, "source": [ "The result of selecting a slice from a list is another list." ] }, { "cell_type": "markdown", "id": "7642552b-776d-4d33-b32e-02097825c6b1", "metadata": {}, "source": [ "### List-related Python functions" ] }, { "cell_type": "markdown", "id": "139c3181-a2a0-4c98-b1ce-b4c68a29f94d", "metadata": {}, "source": [ "Here are some Python built-in functions for working with lists:\n", "\n", "- `len`: calculate length of the list\n", "- `sum`: add together all the values in a list of numbers\n", "- `max`: find the largest value in a list of numbers\n", "- `min`: find the smallest value in a list of numbers\n", "- `sorted`: return a copy of the list in sorted order\n", "- `print`: print the contents of the list" ] }, { "cell_type": "markdown", "id": "726e29cb-eaea-4865-8764-736cb6c47409", "metadata": {}, "source": [ "Use the code cell below to call each of these functions on the list `scores`\n", "and check that you get the answer you expect." ] }, { "cell_type": "code", "execution_count": 41, "id": "108ddba6-66de-4bcf-a6e2-1e6f201a4608", "metadata": {}, "outputs": [], "source": [ "# e.g. len(scores)\n" ] }, { "cell_type": "markdown", "id": "294a683c-2862-454b-82cf-42f29c5a8b72", "metadata": {}, "source": [ "### List methods\n", "\n", "List objects can be modified using their methods:\n", "`.sort()`, `.append()`, `.pop()`, and `.reverse()`.\n", "Remember a *method* is a function \"attached\" to a given object\n", "that we use to perform certain operations on that object.\n", "\n", "We'll now look at some examples that illustrate how to use the dot-syntax for calling list methods." ] }, { "cell_type": "markdown", "id": "db527e4f-06e9-4b05-8310-35ae0b91a283", "metadata": {}, "source": [ "To sort the list of `scores`,\n", "you can call its `.sort()` method." ] }, { "cell_type": "code", "execution_count": 42, "id": "feb2d894", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[61, 72, 79, 98]" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "scores.sort() # sort the list in increasing order\n", "scores # print `scores` to show it is now sorted" ] }, { "cell_type": "markdown", "id": "cd303650-47fb-43ed-b9b5-36651faa7085", "metadata": {}, "source": [ "Use the `.append()` method to add a new element to the list." ] }, { "cell_type": "code", "execution_count": 43, "id": "780efcce", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[61, 72, 79, 98, 22]" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "scores.append(22) # add `22` to the end of the list `scores`\n", "scores # print `scores` to show `22` was added" ] }, { "cell_type": "markdown", "id": "05e8a069-501b-4c2f-84fb-4a702b3b9c3f", "metadata": {}, "source": [ "The method `.pop()` extracts the last element of the list:" ] }, { "cell_type": "code", "execution_count": 44, "id": "15a74b2b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "22" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "scores.pop() # pull out the last element of the list `scores`" ] }, { "cell_type": "markdown", "id": "6eef945c-3739-4389-b743-45705e4dee4b", "metadata": {}, "source": [ "You can think of `.pop()` as the \"undo operation\" of the append operation." ] }, { "cell_type": "markdown", "id": "c569a01e-5fcc-4dc0-8f1f-c9fdf0f22677", "metadata": {}, "source": [ "To reverse the order of elements in the list,\n", "call its `.reverse()` method:" ] }, { "cell_type": "code", "execution_count": 45, "id": "407ec209", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[98, 79, 72, 61]" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "scores.reverse() # reverse the order of elements in the list\n", "scores # print `scores` to show it is now reversed" ] }, { "cell_type": "markdown", "id": "fd218d1d-9849-44ff-8ae5-82c6e8a31a58", "metadata": {}, "source": [ "Other useful list methods include `.insert(idx,newel)` and `.remove(el)`,\n", "which allow you to insert new elements\n", "or to remove elements at arbitrary places in the list." ] }, { "cell_type": "markdown", "id": "729699e7", "metadata": {}, "source": [ "## For loops\n", "\n", "The `for`-loop is a programming concept that allows us to repeat some operation or calculation multiple times.\n", "The most common use case of a `for`-loop is to perform some calculation for each element in a list. \n", "\n", "\n", "The syntax of a Python `for`-loop looks like this:\n", "\n", "```Python\n", "for in :\n", " \n", " \n", " \n", "\n", "```\n", "\n", "This code tells Python to repeat the operations 1, 2, and 3\n", "**for each** element `` in the list ``.\n", "The operations we want to repeat are indented by four spaces,\n", "which indicates they are part of the body of the `for`-loop.\n", "Recall we saw indented code blocks previously for the function body when defining a function.\n", "The syntax is the same here:\n", "all the lines at the same indentation level are part of the code block." ] }, { "cell_type": "markdown", "id": "a657df75-ca7c-434a-abcb-3ceeb251d657", "metadata": {}, "source": [ "### Example 5: print all the scores\n", "\n", "We start with a basic example of a `for`-loop\n", "that simply prints the value of each element." ] }, { "cell_type": "code", "execution_count": 46, "id": "56002dd2", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "61\n", "79\n", "98\n", "72\n" ] } ], "source": [ "scores = [61, 79, 98, 72] # define a list of four numbers\n", "\n", "for score in scores: # repeat for each score in the list scores:\n", " print(score) # print the value of `score`" ] }, { "cell_type": "markdown", "id": "1990cc66-28ac-43eb-9942-2266aa84608b", "metadata": {}, "source": [ "A `for`-loop describes a certain action (or actions)\n", "that you want Python to repeat.\n", "The `for`-loop shown in the code cell above\n", "instructs Python to repeat the action `print(score)` four times,\n", "once for each `score` in the list `scores`.\n", "The operation we want to repeat is indented by four spaces\n", "and hence \"inside\" the for loop." ] }, { "cell_type": "markdown", "id": "856e9b19-5e3d-4f96-999c-d85ce82216e0", "metadata": {}, "source": [ "### Example 6: compute the average score\n", "\n", "The math formula for computing the mean (average value) of a list of numbers\n", "$[x_1, x_2, \\ldots, x_n]$ is:\n", "$\\overline{\\mathbf{x}} = \\left( x_1 + x_2 + \\cdots + x_n \\right) / n$.\n", "We previously computed the average using the functions `sum` and `len`:\n", "`avg = sum(grades)/len(grades)`,\n", "but suppose we don't have access to the function `sum` for some reason,\n", "and we want to compute the sum of the grades using a `for`-loop.\n", "Here is the code for computing the average score." ] }, { "cell_type": "code", "execution_count": 47, "id": "dfcd2e70", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "77.5" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "total = 0 # variable used to store cumulative sums\n", "for score in scores: # repeat for each score in the list scores:\n", " total = total + score # add score to the running total\n", "\n", "# At this point, total contains the sum of the scores.\n", "\n", "avg = total / len(scores) # compute the average and store in `avg`\n", "avg # print `avg`" ] }, { "cell_type": "markdown", "id": "8a39ef9b-ba40-401d-a42c-8f30581431c8", "metadata": {}, "source": [ "On the first line,\n", "we define the temporary variable `total` (initially set to `0`),\n", "which we'll use to store the intermediate values of the sum after each iteration of the `for`-loop.\n", "Next,\n", "the `for`-loop tells Python to go through the list `scores`.\n", "For each `score` in the list,\n", "we perform the operation `total = total + score`.\n", "After the for loop is finished,\n", "the sum of all the `scores` is stored in the variable `total`,\n", "as if we calculated `total = sum(scores)`.\n", "To obtain the average,\n", "we divide `total` by the length of the list.\n", "\n", "Visit this link [`tinyurl.com/3bpx887v`](https://tinyurl.com/3bpx887v)\n", "to see a step-by-step visualization of the four steps (iterations)\n", "of the above `for`-loop execution,\n", "which shows how the variable `total` grows after each iteration.\n", "See also this\n", "[blog post](https://minireference.com/blog/python-for-stats/#for-loops)\n", "for more background on `for`-loops." ] }, { "cell_type": "markdown", "id": "dfb40478", "metadata": {}, "source": [ "#### Loop variable names are arbitrary\n", "\n", "The name of the variable used for the `for`-loop is totally up to you,\n", "but you should choose a logical name \n", "that accurately describes the list elements.\n", "For example,\n", "if the list is called `objs`,\n", "it makes sense to use `obj` as the name of the loop variable:\n", "\"`for obj in objs:`\".\n", "Given a list `profiles`,\n", "we would write the `for`-loop as \"`for profile in profiles:`\".\n", "Given a list `nodes`,\n", "we would use for-loop like \"`for node in nodes:`\", etc." ] }, { "cell_type": "markdown", "id": "0e043a84-8a9e-4bba-91f7-b1aebb63ec5f", "metadata": {}, "source": [ "### List comprehension (bonus topic)\n", "\n", "We often need to transform a list of values,\n", "by applying the same transformation to each value in the list.\n", "Using the standard `for`-loop syntax to apply the function `fun`\n", "to all the elements in a list requires four lines of code:\n", "\n", "```Python\n", "newvalues = [] # storage space for the transformed values\n", "for value in values: # repeat for each value in the list values\n", " newvalue = fun(value) # compute `fun(value)` and store in `newvalue`\n", " newvalues.append(newvalue) # add transformed value to the end of `newvalues`\n", "```\n", "\n", "Python provides the *list comprehension* syntax,\n", "which is shorthand for describing a for loop as a single line of code.\n", "\n", "\n", "```Python\n", "newvalues = [fun(value) for value in values]\n", "# apply `fun` to all `values` and store the result in `newvalues`\n", "```\n", "\n", "Here is an example of using the list comprehension syntax \n", "to apply the function `double` on all the numbers in the list `numbers`,\n", "and store the result in a new list called `doubles`." ] }, { "cell_type": "code", "execution_count": 48, "id": "0366153c-0d85-464b-b258-f23bbaa2f379", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 4, 6, 8, 10]" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "numbers = [1, 2, 3, 4, 5] \n", "doubles = [double(number) for number in numbers]\n", "doubles" ] }, { "cell_type": "markdown", "id": "bcba7d5b-d264-40b2-a4f8-19f8cc7a6ccf", "metadata": {}, "source": [ "List comprehension syntax the preferred way to express simple list transformations,\n", "since it easier to read and fits on a single line." ] }, { "cell_type": "markdown", "id": "38092175-0af9-4a9c-96e1-4f326b490166", "metadata": { "tags": [] }, "source": [ "# Boolean variables and conditional statements" ] }, { "cell_type": "markdown", "id": "50994c26-5de6-430d-913e-536211fa2f0f", "metadata": { "tags": [] }, "source": [ "## Boolean variables \n", "Boolean variables represent logical conditions\n", "that are either `True` or `False`.\n", "We obtain boolean values when we perform numerical comparisons\n", "using the operators like `<`, `>=`, `<=`, `==` (equal to), `!=` (not equal to)." ] }, { "cell_type": "code", "execution_count": 49, "id": "ddae232f-1bee-4264-b2bd-cc845ab06c1a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 3 # store 3 in the variable `x`\n", "x > 2 # Is `x` greater than 2?" ] }, { "cell_type": "markdown", "id": "e4e17cad-290a-4006-a961-5e0b5c9b1ccf", "metadata": {}, "source": [ "Another context where boolean values come up\n", "is when we use the `in` operator to check\n", "if an object is part of a list (or other container)." ] }, { "cell_type": "code", "execution_count": 50, "id": "ee093a89-c0e8-4697-b851-730266148e36", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 in [1,2,3,4] # Is the number 3 contained in the list [1,2,3,4]?" ] }, { "cell_type": "markdown", "id": "a148717b-9281-4480-90d5-9c01151deb7f", "metadata": {}, "source": [ "## Conditional statements\n", "\n", "Boolean values are used in conditional statements,\n", "which are blocks of Python code that may or may not be executed,\n", "depending on the value of a boolean value.\n", "The Python keywords `if`, `elif`, and `else`\n", "are used to create conditional statements." ] }, { "cell_type": "markdown", "id": "5d4c199e-ab25-4e6a-ba2c-142b3ff35518", "metadata": {}, "source": [ "Let's start with some examples of `if` statements." ] }, { "cell_type": "code", "execution_count": 51, "id": "aff62239-c311-4639-bcbb-3a67d617318a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "this code will run\n" ] } ], "source": [ "if True:\n", " print(\"this code will run\")\n", "\n", "if False:\n", " print(\"this code will not run\")" ] }, { "cell_type": "markdown", "id": "a77bd428-645e-4b74-bb2d-0767a466717c", "metadata": {}, "source": [ "The indented code block inside an `if` statement\n", "is executed only when the `if` condition is `True`.\n", "We can extend an `if` statement with an `else` clause,\n", "which allows us to specify a different code block\n", "that will run if the condition is `False`." ] }, { "cell_type": "code", "execution_count": 52, "id": "bb67abe5-01e6-413b-9f24-863e7270cca3", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x is greater than 2\n" ] } ], "source": [ "x = 3\n", "\n", "if x > 2:\n", " print(\"x is greater than 2\")\n", "else:\n", " print(\"x is less than or equal to 2\")" ] }, { "cell_type": "markdown", "id": "c4129c72-7547-45f6-a31c-e0d9127e0a97", "metadata": {}, "source": [ "We can check multiple conditions by\n", "using additional `elif` statements after the initial `if`,\n", "as shown in the next example." ] }, { "cell_type": "code", "execution_count": 53, "id": "b0cb5ac6-acdc-4af4-b953-c2c5f072d53f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "It's cold.\n" ] } ], "source": [ "temp = 8\n", "\n", "if temp > 22:\n", " print(\"It's hot.\")\n", "elif temp < 10:\n", " print(\"It's cold.\")\n", "else:\n", " print(\"It's OK.\")" ] }, { "cell_type": "markdown", "id": "b486554d-3660-40c6-9266-f5f868b39837", "metadata": {}, "source": [ "When Python sees this `if-elif-else` statement,\n", "it checks all the `if` and `elif` conditions one-by-one,\n", "and if it finds a condition that is `True`,\n", "it will execute the corresponding code block.\n", "If none of the `if` and `elif` conditions are `True`,\n", "then the `else` code block will be run.\n", "At the end of this `if-elif-else` statement,\n", "exactly one of the `print` commands will be executed." ] }, { "cell_type": "markdown", "id": "e05ed112-5aab-4db8-901e-4562d1658809", "metadata": {}, "source": [ "### Exercise 7: temperature assessment\n", "\n", "Add another condition to the temperature code\n", "to print `It's very hot!` if the temperature is above 30." ] }, { "cell_type": "code", "execution_count": 54, "id": "4149fb35-ddbb-44f3-9565-85d1c2aafe93", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "It's hot.\n" ] } ], "source": [ "temp = 33\n", "\n", "# Instructions: edit the code below to insert the new condition\n", "\n", "if temp > 22:\n", " print(\"It's hot.\")\n", "elif temp < 10:\n", " print(\"It's cold.\")\n", "else:\n", " print(\"It's OK.\")" ] }, { "cell_type": "markdown", "id": "40af59c2-e90e-4bd9-953d-3fbeee946dd5", "metadata": {}, "source": [ "Click for solution.\n" ] }, { "cell_type": "markdown", "id": "1906dd11-042e-4f9f-9af3-7d67070c1018", "metadata": {}, "source": [ "## Boolean expressions\n", "\n", "You can use the logical operations `and`, `or`, and `not`\n", "to combine individual boolean values into larger boolean expressions\n", "that check multiple conditions.\n", "The result of an `and` operator (logical conjunction)\n", "is `True` only if both operands are true,\n", "and `False` otherwise,\n", "as shown in the following table.\n", "\n", "| Boolean expression | Value |\n", "|---------------------------|---------|\n", "| `True and True` | `True` |\n", "| `True and False` | `False` |\n", "| `False and True` | `False` |\n", "| `False and False` | `False` |\n", "\n", "The `or` operator (logical disjunction) will result in `True`\n", "as long as at least one operand is `True`:\n", "\n", "| Boolean expression | Value |\n", "|---------------------------|----------|\n", "| `True or True` | `True` |\n", "| `True or False` | `True` |\n", "| `False or True` | `True` |\n", "| `False or False` | `False` |\n", "\n", "The `not` operator performs the negation of a boolean value.\n", "\n", "| Boolean expression | Value |\n", "|---------------------------|----------|\n", "| `not True` | `False` |\n", "| `not False` | `True` |\n", "\n", "Here is an example of using the `and` operator to check two conditions simultaneously." ] }, { "cell_type": "code", "execution_count": 56, "id": "b517fd0f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 3\n", "x >= 0 and x <= 10 # Is `x` between 0 and 10?" ] }, { "cell_type": "markdown", "id": "880e3129-a423-42a5-b003-54b33c1fe4a8", "metadata": {}, "source": [ "Try changing the value of `x` to make the boolean expression `False`." ] }, { "cell_type": "markdown", "id": "9db34712-53e1-412c-909a-41abe9a845f7", "metadata": {}, "source": [ "### Exercise 8: water phases\n", "\n", "The phase of water depends on temperature.\n", "The three possible phases of water are `\"gas\"` (water vapour), `\"liquid\"` (water), and `\"solid\"` (ice).\n", "The table below shows the phase of water depending on the temperature `temp`,\n", "expresses as math inequalities.\n", "\n", "| Temperature range | Phase |\n", "|--------------------|--------|\n", "| `temp >= 100` | gas |\n", "| `0 <= temp < 100` | liquid |\n", "| `temp < 0` | solid |\n", "\n", "\n", "Fill in the `if-elif-else` statement below\n", "to print the correct phase\n", "depending on the variable `temp`." ] }, { "cell_type": "code", "execution_count": 57, "id": "e9ff9df7-38b0-4812-8cc6-ef053f725797", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Ellipsis\n" ] } ], "source": [ "temp = 90 # the water temperature\n", "\n", "# Instructions: Fill-in the code of the if-elif-else statement by replacing\n", "# the ...s with the appropriate inequality conditions\n", "# and water phases (either \"gas\", \"liquid\", or \"solid\").\n", "\n", "if ...:\n", " print(...)\n", "elif ...:\n", " print(...)\n", "else:\n", " print(...)" ] }, { "cell_type": "markdown", "id": "72f83a8b-264d-4413-9a74-cc707408a977", "metadata": {}, "source": [ "Click for solution.\n" ] }, { "cell_type": "markdown", "id": "1057795c-4e61-42e2-b54c-d1be1879934f", "metadata": {}, "source": [ "### Exercise 9: assigning letter grades\n", "\n", "Teacher Joelle has computed the final scores of the students as a percentage (a `score` out of 100).\n", "The school where she teaches\n", "requires her to convert each student's `score` to a letter grade\n", "according to the following grading scale:\n", "\n", "| Grade | Numerical score interval |\n", "|-------|----------------------------|\n", "| A | 90% - 100% |\n", "| B | 70% - 89.999...% |\n", "| C | 50% - 69.999...% |\n", "| F | 0% - 49.999...% |\n", "\n", "\n", "Write an `if`-`elif`-`elif`-`else` statement\n", "that takes the `score` variable (a number between 0 and 100),\n", "and prints the appropriate letter grade for that score." ] }, { "cell_type": "code", "execution_count": 59, "id": "dd91cc5f-8079-4ee9-b36f-7d2177d8a1c6", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Ellipsis\n" ] } ], "source": [ "score = 90 # student score\n", "\n", "# Instructions: Fill-in the code of the if-elif-elif-else statement by replacing\n", "# the ...s with the appropriate conditions and letter grades.\n", "\n", "if ...:\n", " print(...)\n", "elif ...:\n", " print(...)\n", "elif ...:\n", " print(...)\n", "else:\n", " print(...)" ] }, { "cell_type": "markdown", "id": "5f32a484-b4c2-45e9-8b5c-348913e3eee8", "metadata": {}, "source": [ "Click for solution.\n" ] }, { "cell_type": "markdown", "id": "0156f40d-161e-4033-93ee-e6939d53490d", "metadata": {}, "source": [ "# Other data structures\n", "\n", "We already discussed `list`s,\n", "which are the most common data structure (container for data) in Python.\n", "In this section,\n", "we'll briefly introduce some other Python data structures you might encounter." ] }, { "cell_type": "markdown", "id": "657f7ee0-2441-4e27-8f68-f158be75b648", "metadata": {}, "source": [ "## Strings\n", "\n", "In Python,\n", "strings (type `str`) are the containers we use for storing text.\n", "We can create a string by enclosing text in single quotes `'` or double quotes `\"`." ] }, { "cell_type": "code", "execution_count": 61, "id": "4938f4b0-0cd4-48fe-91ba-f2de8f88abe1", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "message = \"Hello everyone\"\n", "type(message)" ] }, { "cell_type": "markdown", "id": "20f41526-ee09-4621-bcb9-454406812a8e", "metadata": {}, "source": [ "### String concatenation\n", "We can use string the `+` operator to concatenate (combine) two strings." ] }, { "cell_type": "code", "execution_count": 62, "id": "cacec474-dbdf-4dd2-9ae9-c2f73266ffc6", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello Julie!'" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "name = \"Julie\"\n", "message = \"Hello \" + name + \"!\"\n", "message" ] }, { "cell_type": "markdown", "id": "9b3d5460-769f-48ef-aa16-5530e471522f", "metadata": { "tags": [] }, "source": [ "### Strings behave like lists of characters\n", "\n", "You can think of the string `\"abc\"` as a list of three characters `[\"a\", \"b\", \"c\"]`.\n", "We can use list syntax to access the individual characters in the list.\n", "To illustrate this list-like behaviour of strings,\n", "let's define a string of length 26 that contains all the lowercase Latin letters." ] }, { "cell_type": "code", "execution_count": 63, "id": "b4a766d5-8f1b-4973-a6c1-8fdfc28140cc", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'abcdefghijklmnopqrstuvwxyz'" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "letters = \"abcdefghijklmnopqrstuvwxyz\"\n", "letters" ] }, { "cell_type": "code", "execution_count": 64, "id": "22b75d6b-5459-4548-81d5-50bd9f1b7dbe", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "26" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(letters) # length of the string" ] }, { "cell_type": "markdown", "id": "2a91f138-6236-48e3-9c8d-13f1c2f47c98", "metadata": {}, "source": [ "We can access the individual characters within the using the square brackets.\n", "For example, the index of the first letter in the string is `0`:" ] }, { "cell_type": "code", "execution_count": 65, "id": "6fbe60a9-7cb4-445b-8e60-51f24aaf30d3", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'a'" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "letters[0]" ] }, { "cell_type": "markdown", "id": "9b68ff0d-5e01-4cf4-8614-828208f6b91e", "metadata": {}, "source": [ "The index of the letter `\"b\"` in the string `letters` is `1`:" ] }, { "cell_type": "code", "execution_count": 66, "id": "b85fb88e-75a9-4e15-8d38-d0d035f8de4c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'b'" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "letters[1]" ] }, { "cell_type": "markdown", "id": "35f7b0f7-8095-4c36-875b-a2f781b5b3fe", "metadata": {}, "source": [ "The last element in list of 26 letters has index `25`" ] }, { "cell_type": "code", "execution_count": 67, "id": "e91868f7-65b6-4914-85fb-37f593b469b9", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'z'" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "letters[25]" ] }, { "cell_type": "markdown", "id": "ad2b5cfb-6b53-4286-b8ba-2f9d3d5e6026", "metadata": {}, "source": [ "We can use the slice syntax to extract a substring\n", "that spans a certain range of indices.\n", "For example,\n", "the first four letters of the alphabet are:" ] }, { "cell_type": "code", "execution_count": 68, "id": "1f73b115-bbca-43a0-a9bc-ce7913d28624", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'abcd'" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "letters[0:4]" ] }, { "cell_type": "markdown", "id": "eb1ac6a2-6922-4a6c-89ae-b3952caa668c", "metadata": {}, "source": [ "The syntax `0:4` is a shorthand for the expression `slice(0,4)`,\n", "which corresponds to the range of indices from `0` (inclusive) to `4` (noninclusive): `[0,1,2,3]`." ] }, { "cell_type": "markdown", "id": "1de7c85b-9f9d-478a-970c-518821e7351f", "metadata": {}, "source": [ "## Tuples\n", "\n", "A `tuple` is similar to a `list`,\n", "but with fewer features.\n", "The syntax for defining a tuples is base don the parentheses,\n", "and the elements of the tuple are separated by commas." ] }, { "cell_type": "code", "execution_count": 69, "id": "f821575c-c1db-46ea-972e-081add64c7c4", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 2, 3)" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(1, 2, 3)" ] }, { "cell_type": "markdown", "id": "354114a7-429d-42a2-bb1f-f2ef22f2a23c", "metadata": {}, "source": [ "Actually,\n", "the parentheses are optional:\n", "we can define a tuple by using writing a bunch of commas-separated values." ] }, { "cell_type": "code", "execution_count": 70, "id": "48fd3e97-7206-4aa8-a810-f8d0f559e8cf", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 2, 3)" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1, 2, 3" ] }, { "cell_type": "markdown", "id": "43173d38-3114-409f-b738-b70d233c259a", "metadata": {}, "source": [ "I want you to know about this comma syntax,\n", "because it is very convenient for printing several values\n", "at the end of a code cell in a notebook environment.\n", "For example,\n", "here is how we can display the first,\n", "second, and last characters from the string `letters` on a single line." ] }, { "cell_type": "code", "execution_count": 71, "id": "5191c2b2-e541-40aa-a25a-bfbb01b6e54e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "('a', 'b', 'z')" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "letters[0], letters[1], letters[25]" ] }, { "cell_type": "markdown", "id": "0ec6ca56-1f63-47ce-8cf5-456581cd12c8", "metadata": {}, "source": [ "## Dictionaries\n", "\n", "One of the most useful data structure when programming is the associative array,\n", "which is called a dictionary (`dict`) in Python.\n", "Associative arrays are sometimes called a *hash-tables* or *lookup tables*.\n", "A dictionary is a container of key-value pairs.\n", "The syntax for creating a dictionary is based on \n", "`\"key\": value` pairs, separated by commas,\n", "wrapped in curly braces `{` and `}`.\n", "For example,\n", "the code below defines the dictionary `profile`\n", "that contains three key-value pairs.\n", "\n", "" ] }, { "cell_type": "code", "execution_count": 72, "id": "2cbe5469-d441-4527-b8cc-df5d550f7f87", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'name': 'Julie', 'age': 31, 'score': 98}" ] }, "execution_count": 72, "metadata": {}, "output_type": "execute_result" } ], "source": [ "profile = {\"name\":\"Julie\", \"age\":31, \"score\":98}\n", "profile" ] }, { "cell_type": "markdown", "id": "1446f2ff-12b2-4282-b7ce-8d39bdc458e5", "metadata": {}, "source": [ "A dictionary is a mapping from keys to values, as illustrated below.\n", "\n", "![Visualization of the dictionary profile that shows its keys and its values side by side.](https://raw.githubusercontent.com/minireference/noBSstats/main/tutorials/src/attachments/vizualization_of_profile_dict.png)\n", "\n", "We use the keys to access the different values in the dictionary.\n", "The keys of the `profile` dictionary are `\"name\"`, `\"age\"`, and `\"score\"`." ] }, { "cell_type": "code", "execution_count": 73, "id": "7137d147-a739-4882-8795-e39770b436a9", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_keys(['name', 'age', 'score'])" ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "profile.keys()" ] }, { "cell_type": "markdown", "id": "e7dc507f-dffd-49c2-b3ae-8e20d72aad66", "metadata": {}, "source": [ "The values associated with these keys\n", "are `\"Julie\"` (a string), `31`, and `98` (integers)." ] }, { "cell_type": "code", "execution_count": 74, "id": "cf558c1f-bb8a-43c5-9159-61c2de03dc8f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_values(['Julie', 31, 98])" ] }, "execution_count": 74, "metadata": {}, "output_type": "execute_result" } ], "source": [ "profile.values()" ] }, { "cell_type": "markdown", "id": "4645a9a8-7a45-4927-abe6-7bfe6d6537b2", "metadata": {}, "source": [ "We access the value in the dictionary using the square brackets syntax.\n", "For example,\n", "to get the value associated with key `\"score\"`,\n", "we use:" ] }, { "cell_type": "code", "execution_count": 75, "id": "292445ea-72b6-4adc-a06d-08d4e5d574d0", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "98" ] }, "execution_count": 75, "metadata": {}, "output_type": "execute_result" } ], "source": [ "profile[\"score\"]" ] }, { "cell_type": "markdown", "id": "c9829ba4-fd49-4d30-be8e-78a33e5d7dbf", "metadata": {}, "source": [ "Recall, we used the square bracket syntax earlier for accessing the values within a list.\n", "Indeed, lists and dictionaries are both containers objects,\n", "and we use square brackets syntax to access elements within them." ] }, { "cell_type": "markdown", "id": "a57c81b0-085d-4276-b032-5957be335160", "metadata": {}, "source": [ "You can change the value associated with a key by assigning a new value to it,\n", "as follows:" ] }, { "cell_type": "code", "execution_count": 76, "id": "cea52a07-c70b-497a-8bd6-a13231032aef", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'name': 'Julie', 'age': 31, 'score': 77}" ] }, "execution_count": 76, "metadata": {}, "output_type": "execute_result" } ], "source": [ "profile[\"score\"] = 77\n", "profile" ] }, { "cell_type": "markdown", "id": "372c2d3b-7877-450f-9861-38896ebda440", "metadata": {}, "source": [ "You can also add a new key-value pair to the dictionary by assigning a value to a key that doesn't exist yet:" ] }, { "cell_type": "code", "execution_count": 77, "id": "24b4ddb0-0087-4bf4-a994-64f57b98e685", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'name': 'Julie', 'age': 31, 'score': 77, 'email': 'julie@site.org'}" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "profile[\"email\"] = \"julie@site.org\"\n", "profile" ] }, { "cell_type": "markdown", "id": "4c8b816f-1e00-4083-bc1d-03d9efbac403", "metadata": {}, "source": [ "Note the `profile` dictionary now has a new key `\"email\"`\n", "and the value `\"julie@site.org\"` stored under that key." ] }, { "cell_type": "markdown", "id": "0d154da6-69d9-4e1b-b50c-75ffd2ab5cda", "metadata": {}, "source": [ "### Exercise 10: creating a new profile dictionary\n", "\n", "Create a new dictionary called `profile2` with the same keys as dictionary `profile`\n", "for the user `\"Alex\"` who has age `42` and score `65`." ] }, { "cell_type": "code", "execution_count": 78, "id": "10f3f08a-12db-4fa0-b51a-c13731823284", "metadata": {}, "outputs": [], "source": [ "profile2 = ... # replace ... with your answer" ] }, { "cell_type": "markdown", "id": "e0bd5689-068b-4240-a194-0cfa4d170240", "metadata": {}, "source": [ "Click for solution.\n" ] }, { "cell_type": "markdown", "id": "fc7a0104-8902-4dd4-b293-f3efc8678859", "metadata": {}, "source": [ "## Type conversions\n", "\n", "We sometimes need to convert between variables of different types.\n", "The functions for conversing types have the same name as the object types:\n", "\n", "- `int` : converts any expression into an `int`\n", "- `float`: converts any expression into a `float`\n", "- `str`: converts any expression into a `str`\n", "\n", "\n", "\n", "For example,\n", "suppose you're given the number `\"42.5\"` as a string." ] }, { "cell_type": "code", "execution_count": 80, "id": "83c8e652-cf92-4870-b6f5-5f90a351adfd", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 80, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(\"42.5\")" ] }, { "cell_type": "markdown", "id": "cfa61b11-0762-46ec-a020-420deab18fc7", "metadata": {}, "source": [ "To convert this string to a floating point number,\n", "we can call the function `float`." ] }, { "cell_type": "code", "execution_count": 81, "id": "cf095c04-70bd-483c-a995-c7cfb3c157b5", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "42.5" ] }, "execution_count": 81, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f = float(\"42.5\")\n", "f" ] }, { "cell_type": "code", "execution_count": 82, "id": "d5233dd6-dd80-4fe4-9334-b3cbdeb3e3fa", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 82, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(f)" ] }, { "cell_type": "markdown", "id": "9a224ca8-1420-4d92-886c-4a8880a5f01d", "metadata": {}, "source": [ "Now that we have converted the string `\"42.5\"` to the float `42.5`,\n", "we can do numerical operation like `+`, `-`, and `*` with this number." ] }, { "cell_type": "markdown", "id": "fd49f7dc-c3a1-4df6-ab18-f88d703617ac", "metadata": {}, "source": [ "### Example 7: compute the sum of two numbers\n", "\n", "Suppose we're given two numbers $m$ and $n$,\n", "and we want to compute their sum $m+n$.\n", "However,\n", "the numbers $m$ and $n$ are given to us as strings,\n", "so we don't get the expected result when we add them together." ] }, { "cell_type": "code", "execution_count": 83, "id": "66eab1bc-a797-4d00-8aa2-d615eb492bec", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'2.13.4'" ] }, "execution_count": 83, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mstr = \"2.1\"\n", "nstr = \"3.4\"\n", "mstr + nstr" ] }, { "cell_type": "markdown", "id": "0604b718-3e8f-4d0d-935f-b4e3c3ad7073", "metadata": {}, "source": [ "This is because the addition operator `+` for strings means concatenate, not add.\n", "We have to manually convert the strings to numbers,\n", "if we want to add them together as numbers." ] }, { "cell_type": "code", "execution_count": 84, "id": "2e222e82-84b1-4640-8522-3693f3965a7a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5.5" ] }, "execution_count": 84, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mfloat = float(mstr)\n", "nfloat = float(nstr)\n", "mfloat + nfloat" ] }, { "cell_type": "markdown", "id": "548fd233-a485-409d-b4c8-e61bfd2ed7bb", "metadata": {}, "source": [ "### Exercise 11: type conversion and sum\n", "\n", "Write the Python code that converts values in the list `prices`\n", "to floating point numbers and adds them together.\n", "\n", "Hint: use a `for`-loop or the list comprehension syntax." ] }, { "cell_type": "code", "execution_count": 85, "id": "20ceb250-9718-4446-8c64-fe3b9defe3d8", "metadata": {}, "outputs": [], "source": [ "prices = [\"22.2\", \"10.1\", \"33.3\"]\n", "\n", "# write the code to compute the total price" ] }, { "cell_type": "markdown", "id": "8c9174d8-dff1-4b0e-b2a2-f983ecadeb34", "metadata": {}, "source": [ "Click for solution.\n" ] }, { "cell_type": "markdown", "id": "802c696b-c4f4-4e52-be4c-6b64066c7b84", "metadata": {}, "source": [ "# Python packages and modules\n", "\n", "All the code examples we showed above were using the Python built-in functions and data types,\n", "but that is only a small part of the functionality available in Python.\n", "There are hundreds of Python packages and modules\n", "that provide additional functions and data types\n", "for all kinds of applications. \n", "There are Python modules for processing different data files,\n", "making web requests,\n", "doing efficient numerical computing,\n", "calculating statistics, etc.\n", "The list is almost endless!\n", "\n", "\n", "\n", "\n" ] }, { "cell_type": "markdown", "id": "88f053a7-fe59-4b30-84d6-d4d7603ff612", "metadata": {}, "source": [ "## Import statements\n", "\n", "We use the `import` keyword to load a Python module\n", "and make it available in the current notebook environment.\n", "The command to import the module `` in the current context is:\n", "\n", "```Python\n", "import \n", "```\n", "\n", "After this import statement,\n", "we can use the all functions from the module `` by calling them using the prefix `.`,\n", "which is called the \"dot notation\" for accessing names within the `` *namespace*.\n", "For example,\n", "here is how we can import the `statistics` module\n", "and use the function `statistics.mean` to compute the mean of a list of numbers." ] }, { "cell_type": "code", "execution_count": 87, "id": "fac4bab4-c52e-49c5-9a80-ae5484649da4", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.5" ] }, "execution_count": 87, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import statistics\n", "statistics.mean([1,2,3,4])" ] }, { "cell_type": "markdown", "id": "b60fd217-1f5a-4cc8-9702-73b96a5175af", "metadata": {}, "source": [ "### Alias import statements\n", "\n", "A very common trick you'll see in many Python notebooks,\n", "is to import python modules under an \"alias\" name,\n", "which is usually a shorter name that is faster to type.\n", "The alias-import statement looks like this:\n", "\n", "```Python\n", "import as \n", "```\n", "\n", "For example,\n", "importing the `statistics` module under the alias `stats` looks like this." ] }, { "cell_type": "code", "execution_count": 88, "id": "4c382060-350b-425a-a28c-e39704949454", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.5" ] }, "execution_count": 88, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import statistics as stats\n", "stats.mean([1,2,3,4])" ] }, { "cell_type": "markdown", "id": "46d24c6d-11f5-48bd-b0c9-0795daef9681", "metadata": {}, "source": [ "The alias-import statement allows us to use the shorter names\n", "`stats.mean` and `stats.median` instead of typing out the full module name each time,\n", "like `statistics.mean` and `statistics.median`.\n", "In data science,\n", "it is very common to alias-import the `numpy` module as `np`,\n", "the `pandas` module as `pd`,\n", "and the `seaborn` module as `sns`." ] }, { "cell_type": "markdown", "id": "39a8194f-0d4b-4160-a78c-f407f9b7e572", "metadata": {}, "source": [ "### Selective import statements\n", "\n", "It is also possible to import only a specific function from a module\n", "using the following syntax.\n", "\n", "```Python\n", "from import \n", "```\n", "\n", "This allows you to use `` directly in your code,\n", "without the `.` prefix." ] }, { "cell_type": "code", "execution_count": 89, "id": "37e27fe1-763f-4850-9136-fd5598e66226", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.5" ] }, "execution_count": 89, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from statistics import mean\n", "mean([1,2,3,4])" ] }, { "cell_type": "markdown", "id": "e19f1bdb-c36d-41f9-b4b3-0422ab1288d1", "metadata": {}, "source": [ "## The Python standard library\n", "\n", "The Python [*standard library*](https://docs.python.org/3/library/)\n", "refers to the hundreds of Python modules\n", "that come bundled with every Python installation.\n", "Here are some of the most commonly used modules from the standard library:\n", "\n", "- `math`: math functions like `sqrt`, `sin`, `cos`, etc.\n", "- `random`: random number generation\n", "- `statistics`: descriptive statistics functions\n", "- `datetime`: manipulate dates and times\n", "- `urllib`: tools to manipulate URLs\n", "- `json`: read and write JSON files\n", "- `csv`: read and write CSV files\n", "- `sys`: access information about the current process\n", "- `os`: interface with the operating system and file system paths\n", "- `re`: regular expressions patterns for text processing\n", "\n", "The easy of access to all these modules with thousands of predefined functions \n", "is a big reason why Python is popular today.\n", "Python is often described as a language that comes with \"batteries included\"\n", "because of all the functionality available in the standard library.\n", "\n", "" ] }, { "cell_type": "markdown", "id": "1d269503-8bc1-46e9-ac8c-44c2dbc63345", "metadata": {}, "source": [ "## Installing packages using `pip`\n", "\n", "In addition to the standard library,\n", "the Python ecosystem includes a vast ecosystem of third-party modules,\n", "which you can install using the\n", "[`pip`](https://en.wikipedia.org/wiki/Pip_(package_manager)) command-line tool.\n", "Normally,\n", "using `pip` requires some familiarity with command-line interface tools,\n", "but when working in a JupyterLab environment,\n", "you can use the magic command `%pip` right within a notebook.\n", "To install Python package named ``,\n", "run the command `%pip install ` in a notebook code cell.\n", "For example,\n", "here is the `%pip` command for installing the `sympy` package." ] }, { "cell_type": "code", "execution_count": 90, "id": "61ff678e-c66a-4a26-bde0-76a8a0db48be", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: sympy in /Users/ivan/Projects/Minireference/STATSbook/noBSstatsnotebooks/venv/lib/python3.9/site-packages (1.11.1)\n", "Requirement already satisfied: mpmath>=0.19 in /Users/ivan/Projects/Minireference/STATSbook/noBSstatsnotebooks/venv/lib/python3.9/site-packages (from sympy) (1.2.1)\n", "Note: you may need to restart the kernel to use updated packages.\n" ] } ], "source": [ "%pip install sympy" ] }, { "cell_type": "markdown", "id": "a73bf623-47fe-4859-97c7-93d2641e9f42", "metadata": {}, "source": [ "In this case,\n", "the message \"Requirement already satisfied: sympy\"\n", "tells us that the `sympy` package is already installed,\n", "so there is nothing to do.\n", "If the `sympy` was not present,\n", "however,\n", "the `%pip` command search for the `sympy` package \n", "published on the Python Package Index website\n", "(see [https://pypi.org/project/sympy/](https://pypi.org/project/sympy/)),\n", "download the latest version,\n", "and install it so that it is available for use (e.g. `import sympy` etc.).\n", "\n", "The breadth and depth of the modules in the Python ecosystem is staggering.\n", "Whether you're an astrophysicist analyzing data from deep space,\n", "a neuroscientist who is trying to decode neural activity,\n", "or an office worker trying to process some obscure file format,\n", "there is a good chance that someone has come before you\n", "and written code you can reuse for your task.\n", "A detailed discussion of the Python third-party modules ecosystem\n", "is outside the score of this introductory tutorial,\n", "but I'd like to mention a few of the all-star, heavy-hitter libraries\n", "for scientific computing and data science.\n", "\n", "" ] }, { "cell_type": "markdown", "id": "5aed0f5a-54b2-4358-b332-7715dcdf1f86", "metadata": {}, "source": [ "## Scientific computing libraries\n", "\n", "Python was adopted by the scientific community very early on,\n", "because it is an easy language to learn.\n", "Scientific computing usually involves large-scale numerical calculations on vectors and matrices,\n", "which led to the development of the following packages for high-performance computing:\n", "\n", "- **Numerical Python** (`numpy`)\n", " is a library that provides array and matrix objects\n", " that make mathematical operations run very fast.\n", "- **Scientific Python** (`scipy`)\n", " is a library provides many functions used by scientists and engineers.\n", " The module `scipy.stats` contains all the probability models we use in statistics.\n", "- **Symbolic Python** (`sympy`)\n", " is a library for symbolic math calculations.\n", " Using SymPy you can work with math expressions\n", " just like when solving math problems using pen and paper.\n", " See the [sympy_tutorial.pdf](https://minireference.com/static/tutorials/sympy_tutorial.pdf)\n", " to learn more about SymPy.\n", "\n", "" ] }, { "cell_type": "markdown", "id": "d95ce302-0a66-4dff-b056-d24227e1dc42", "metadata": {}, "source": [ "## Data science libraries\n", "\n", "Another community where Python is very popular is among statisticians and data analysts,\n", "which is thanks to the following libraries for data handling and visualization:\n", "\n", "- **Pandas** (`pandas`)\n", " is a library for tabular data management.\n", " Pandas is the Swiss army knife of data manipulations,\n", " and provides functions for loading data from various formats,\n", " and doing data processing, data cleaning, and calculating descriptive statistics.\n", " See the [pandas_tutorial.ipynb](./pandas_tutorial.ipynb) notebook to learn more about Pandas.\n", "- **Seaborn** (`seaborn`)\n", " is a high-level library for statistical plots.\n", " Seaborn allows you to generate beautiful data visualizations\n", " like strip plots, scatter plots, histograms, bar plots, box plots, etc.\n", " See the [seaborn_tutorial.ipynb](./seaborn_tutorial.ipynb) notebook\n", " to learn more about Seaborn.\n", "- **Statsmodels** (`statsmodels`)\n", " is a statistical modelling library\n", " that includes advanced statistical models\n", " and functions for performing statistical tests." ] }, { "cell_type": "markdown", "id": "e7edf4ef-b3f1-4930-bd27-b44f53e1fc4d", "metadata": {}, "source": [ "**TODO**: closing sentence" ] }, { "cell_type": "markdown", "id": "55d6303a-20b6-433e-8d80-225bfc31ae53", "metadata": {}, "source": [ "# Getting comfortable with Python\n", "\n", "JupyterLab provides lots of tools for making learning Python easy for beginners,\n", "including documentation (help menus) and interactive ways to explore variable properties." ] }, { "cell_type": "markdown", "id": "51f9ec1d-91e5-48e4-a4b4-e1dc4094a642", "metadata": { "tags": [] }, "source": [ "## Showing the help info\n", "\n", "Every Python object has a \"docstring\" (documentation string) associated with it,\n", "which provides the help info about this object.\n", "There are three equivalent ways to view the docstring of any Python object `obj`\n", "(value, variable, function, module, etc.):\n", "\n", "- `help(obj)`: shows the docstring for the object `obj`\n", "- `obj?`: shortcut for `help(obj)`\n", "- press **SHIFT+TAB**: while the cursor is on top of a variable or function inside a code cell.\n", "\n", "There are also other methods for getting more detailed info about an object like\n", "`obj??`, `%psource obj`, `%pdef obj`, but we won't need these for now." ] }, { "cell_type": "markdown", "id": "8d25cd79-db62-4884-ad74-b05ef62400a6", "metadata": {}, "source": [ "### Example 8: learning about the `print` function\n", "\n", "Let's say want to learn more about the `print` function.\n", "Put the cursor in the middle of function and press **SHIFT+TAB**." ] }, { "cell_type": "code", "execution_count": 91, "id": "9cdc17af-bf77-41e6-96fb-7ee20cbe0143", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 91, "metadata": {}, "output_type": "execute_result" } ], "source": [ "print" ] }, { "cell_type": "markdown", "id": "f20d48c3-25ba-4645-975d-4129dffb33d0", "metadata": {}, "source": [ "You know this function accepts one or more arguments and displays them,\n", "but the help menu shows complete information about other keywords arguments (options)\n", "you can use when calling this function.\n", "You can also use the `help` function to see the same information." ] }, { "cell_type": "code", "execution_count": 92, "id": "7a02f218-c06c-415b-a169-db2eb383eb37", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on built-in function print in module builtins:\n", "\n", "print(...)\n", " print(value, ..., sep=' ', end='\\n', file=sys.stdout, flush=False)\n", " \n", " Prints the values to a stream, or to sys.stdout by default.\n", " Optional keyword arguments:\n", " file: a file-like object (stream); defaults to the current sys.stdout.\n", " sep: string inserted between values, default a space.\n", " end: string appended after the last value, default a newline.\n", " flush: whether to forcibly flush the stream.\n", "\n" ] } ], "source": [ "help(print)" ] }, { "cell_type": "markdown", "id": "858ca305-ca20-4a61-8c23-173417fabf59", "metadata": {}, "source": [ "The `...` in the doc string of the `print` function tells us it accepts multiple values.\n", "The keyword argument `sep` (separator) dictates the text that is inserted between values when they are printed,\n", "The keyword `end` controls what is printed at the end of the line (`\\n` means newline).\n", "Note we could have obtained the doc string using `print?` or using `print(print.__doc__)`." ] }, { "cell_type": "markdown", "id": "103f9b37-8980-4b21-9326-8a1aea9f80c0", "metadata": {}, "source": [ "### Exercise 12: getting help\n", "\n", "Display the doc-string of the function `len`." ] }, { "cell_type": "code", "execution_count": 93, "id": "bfba15e7-497a-4651-9df4-7ef28ee4b266", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 93, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len" ] }, { "cell_type": "markdown", "id": "fae81d0d-5ae9-4fc5-8c55-6b761e3e0372", "metadata": {}, "source": [ "Click for solution.\n" ] }, { "cell_type": "markdown", "id": "b8fca9a4-e630-471c-b6dd-9574b2b6e0bd", "metadata": { "tags": [] }, "source": [ "## Inspecting Python objects\n", "\n", "Suppose you have an unknown Python object `obj`.\n", "How can you find out what it is and learn what you can do with it?" ] }, { "cell_type": "markdown", "id": "c37a6968-8dd4-494d-9464-24f48057be11", "metadata": { "tags": [] }, "source": [ "### Displaying the object\n", "\n", "There are several ways to display information about the object `obj`.\n", "\n", "- `print(obj)`: converts the object to `str` and prints it\n", "- `type(obj)`: tells you what type of object it is\n", "- `repr(obj)`: similar to `print`, but shows the complete text representation (including quotes).\n", " The output of `repr(obj)` usually contains all the information needed to reconstruct the object `obj`." ] }, { "cell_type": "markdown", "id": "05af7b68-b2fb-4cea-85b0-997a4d5ccb4d", "metadata": { "tags": [] }, "source": [ "### Auto-complete object's methods\n", "\n", "JupyterLab code cells provide an \"autocomplete\" feature that allows us to interactively\n", "exploring what methods are available on a given object.\n", "To use the autocomplete functionality,\n", "start by writing the name of the object followed by a dot `obj.`,\n", "then press **TAB** button.\n", "You'll be presented with a list of all the object's methods.\n", "Try this out for yourself." ] }, { "cell_type": "code", "execution_count": 95, "id": "1b84eed3-8910-4bf4-9a03-a9d3c1aeae49", "metadata": {}, "outputs": [], "source": [ "message = \"Hello everyone\"\n", "# message. # place cursor after the dot and press the TAB button" ] }, { "cell_type": "markdown", "id": "bc1c823c-4c68-4ed8-b2d0-b4c015f3371a", "metadata": {}, "source": [ "This autocomplete feature is used very often by programmers to find the names of the methods they need.\n", "Examples of useful methods on the `message` object include\n", "`message.upper()`,\n", "`message.lower()`,\n", "`message.split()`, \n", "`message.replace(...)`,\n", "etc.\n", "\n", "" ] }, { "cell_type": "markdown", "id": "ba3b0dce-57cd-4b1a-ad23-39fc124499c9", "metadata": { "tags": [] }, "source": [ "## Python error messages help you fix mistakes\n", "\n", "Sometimes your code will cause an error\n", "and Python will display an error message describing the problem it encountered.\n", "You need to be mentally prepared for these errors,\n", "since they happen *a lot* and can be very discouraging to see.\n", "Examples of errors include `SyntaxError`, `NameError`, `TypeError`, etc.\n", "Error messages look scary,\n", "but really they are there to help you—if you read what the error message is telling you,\n", "you'll know exactly what you need to fix in your code.\n", "The error message literally describes the problem!\n", "\n", "Let's look at an example expression that causes a Python exception.\n", "Suppose you're trying to compute the difference `5-3`,\n", "but you forgot the minus sign." ] }, { "cell_type": "code", "execution_count": 96, "id": "99b808a8", "metadata": { "tags": [ "raises-exception" ] }, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (127674276.py, line 1)", "output_type": "error", "traceback": [ "\u001b[0;36m Cell \u001b[0;32mIn[96], line 1\u001b[0;36m\u001b[0m\n\u001b[0;31m 5 3\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], "source": [ "5 3" ] }, { "cell_type": "markdown", "id": "d89649cd-ec75-49cb-8f73-1cbc32f3c63c", "metadata": {}, "source": [ "The code cell above shows an example of a `SyntaxError` that\n", "occurs because Python expects some operator or separator between the two numbers.\n", "Python doesn't know what to do when you just put two numbers side-by-side,\n", "and it doesn't want to guess what your intention was (`53`? `5.3`?).\n", "\n", "You'll see these a threatening looking red message like this\n", "any time Python encounters an error while trying to run the commands you specified.\n", "We say the code \"raises\" or \"encountered\" an exception.\n", "This is nothing to be alarmed by.\n", "It usually means you made a typo,\n", "forgot a required syntax element,\n", "or tried to compute do something impossible.\n", "The way to read these red messages\n", "is to focus on the **explanation message that gets printed on the last line**.\n", "The error message usually tells you what you need to fix.\n", "The solution will be obvious when you have made a typo or a syntax error,\n", "but in more complicated situations,\n", "you might need to search for the error message online to find what the problem is.\n", "In the above example,\n", "the fix is simple: we just need to add the missing minus sign: `5-3`." ] }, { "cell_type": "markdown", "id": "d93987a8-ec15-4de0-8793-b87ac646d87c", "metadata": { "tags": [] }, "source": [ "The three most common Python error messages are:\n", "\n", "- `SyntaxError`: you typed in something wrong (usually missing `,`, `\"`, or `]` or some other punctuation).\n", "- `NameError`: raised when a variable is not found in the current context.\n", "- `TypeError`: raised when a function or operation is applied to an object of incorrect type.\n", "\n", "Later on you might run into:\n", "`KeyError` when a key is not found in a dictionary,\n", "`ValueError` when a function gets an argument of correct type but improper value,\n", "`ImportError` when importing modules,\n", "and `AttributeError` when trying to access an object attribute that doesn't exist." ] }, { "cell_type": "markdown", "id": "1e1ec716-7ed2-4be0-8012-9b8e58cb8c9b", "metadata": { "tags": [] }, "source": [ "### Exercise 13: let's break Python!\n", "\n", "Try typing in Python commands that causes each of these exceptions:\n", " \n", " 1. `SyntaxError` (hint: write an incomplete list expression)\n", " 2. `NameError` (hint: try referring to a variable that doesn't exist)\n", " 3. `TypeError` (hint: try to `sum` something that is not a list)" ] }, { "cell_type": "markdown", "id": "9aebce93-4124-4918-8f69-a69c457e3c9e", "metadata": {}, "source": [ "Click for solution.\n" ] }, { "cell_type": "markdown", "id": "0195c13c-1488-4bf3-ad2f-40af772de4c1", "metadata": {}, "source": [ "## Python documentation is very good\n", "\n", "The official Python documentation website is \n", "[`https://docs.python.org`](https://docs.python.org).\n", "This website provides tons of excellent learning resources and reference information for learning Python.\n", "Here are some useful links to essential topics for Python beginners:\n", "\n", "- Official Python tutorial: [`https://docs.python.org/3/tutorial/index.html`](https://docs.python.org/3/tutorial/index.html)\n", "- Built-in types: \n", " [`int`](https://docs.python.org/3/library/functions.html#int),\n", " [`float`](https://docs.python.org/3/library/functions.html#float),\n", " [`list`](https://docs.python.org/3/library/functions.html#func-list),\n", " [`bool`](https://docs.python.org/3/library/functions.html#bool),\n", " [`str`](https://docs.python.org/3/library/functions.html#func-str),\n", " [`tuple`](https://docs.python.org/3/library/functions.html#func-tuple),\n", " [`dict`](https://docs.python.org/3/library/functions.html#func-dict).\n", "- [Functions docs](https://docs.python.org/3/library/functions.html):\n", " [`abs`](https://docs.python.org/3/library/functions.html#abs),\n", " [`len`](https://docs.python.org/3/library/functions.html#len),\n", " [`type`](https://docs.python.org/3/library/functions.html#type),\n", " [`print`](https://docs.python.org/3/library/functions.html#print),\n", " [`sum`](https://docs.python.org/3/library/functions.html#sum),\n", " [`min`](https://docs.python.org/3/library/functions.html#min),\n", " [`max`](https://docs.python.org/3/library/functions.html#max).\n", "\n", "I encourage you to browse the site to familiarize yourself with the high quality information that is available in the official docs pages.\n", "\n", "When searching online, \n", "the official Python docs might now show up on the first page of results.\n", "Because of Python's popularity,\n", "there are hundreds of spammy websites that provide inferior information,\n", "wrapped in tons of advertisements and popups.\n", "These websites that are much inferior to the official documentation.\n", "When learning something new,\n", "you should always prefer the official docs,\n", "even if they don't appear first in the list of results on the page.\n", "[Stack overflow](https://stackoverflow.com/questions/tagged/python) discussions\n", "can be a good place to find answers to common Python questions.\n", "ChatGPT is also pretty good with Python code,\n", "so you can ask it to give you code examples or provide feedback on code you've written." ] }, { "cell_type": "markdown", "id": "61ccae80-5c6b-4172-b3b6-653841214241", "metadata": {}, "source": [ "# Final review\n", "\n", "Let's do a quick review of all the Python concepts we introduced in this tutorial." ] }, { "cell_type": "markdown", "id": "63d3d979-9dd8-4f2e-b895-0c3b61e3117a", "metadata": {}, "source": [ "## Python grammar and syntax review\n", "\n", "Learning Python is like learning a new language:\n", "\n", "- **nouns**: variables and values of different types\n", "- **verbs**: functions and methods, including basic operators like `+`, `-`, etc.\n", "- **grammar**: rules about how to use nouns and verbs together\n", "\n", "" ] }, { "cell_type": "markdown", "id": "fd82d654-ddde-4ba7-8a9c-4bc57f4453a1", "metadata": { "jp-MarkdownHeadingCollapsed": true }, "source": [ "## Python keywords\n", "\n", "Here is a complete list of keywords in the Python language:\n", "\n", "```\n", "False class finally is return\n", "None continue for lambda try\n", "True def from nonlocal while\n", "and del global not with\n", "as elif if or yield\n", "assert else import pass\n", "break except in raise\n", "```\n", "\n", "As you can see from the above list,\n", "there are some keywords that we didn't cover in this tutorial,\n", "because they are not essential to know at first.\n", "Here is the shortlist of the keywords you need to remember:\n", "\n", "- `def` used to define a new function and the `return` statement that defines the output of the function\n", "- `in` to check if element is part of container\n", "- `for` used in `for`-loops and list-comprehension syntax\n", "- the boolean values `True` and `False`\n", "- `or`, `and`, and `not` to create boolean expressions\n", "- `if`, `elif`, `else` used in conditional statements\n", "- `None` is a special keyword that represents the absence of a value\n", "- `import ...` and `from ... import ...` statements to import Python modules\n", "\n", "\n", "\n" ] }, { "cell_type": "markdown", "id": "022525d9-4f1e-4808-a043-3fce9a368c63", "metadata": {}, "source": [ "## Python data types\n", "\n", "Here is a reminder of the Python built-in data types:\n", "\n", "- `int`: integers\n", "- `float`: decimals\n", "- `list`: list of objects `[obj1, obj2, obj3, ...]`\n", "- `bool`: boolean values (either `True` or `False`)\n", "- `str`: text strings\n", "- `dict`: associative array between keys and values\n", "- `tuple`: similar to a list, but cannot be modified\n", "\n", "" ] }, { "cell_type": "markdown", "id": "a4cc9b01-bb31-4261-be97-08f05ade09ed", "metadata": {}, "source": [ "## Python functions\n", "\n", "Here are the essential functions you need to remember:\n", "\n", "- `print(arg1,arg2, ...)`: print `str(arg1)`, `str(arg2)`, `...` to the screen\n", "- `type(obj)`: tells you what kind of object \n", "- `len(obj)`: length of the object (only for: `str`, `list`, `dict` objs)\n", "- `range(a,b)`: creates a list of numbers `[a,a+1,...,b-1]`\n", "- `help(obj)`: display info about the object, function, or method\n", "\n", "Here is another set of build-in functions used for lists,\n", "and list-like objects:\n", "\n", "- `len(mylist)`: length of the list `mylist`\n", "- `sum(mylist)`: sum of the values in the list of numbers `mylist`\n", "- `all(mylist)`: `True` if all values in the list `mylist` are `True`\n", "- `any(mylist)`: `True` if any of the values in the list `mylist` are `True`\n", "- `enumerate(mylist)`: convert list of values to `mylist` to list of tuples `(i,mylist[i])`.\n", " Used to know the index in `for`-loop,\n", " e.g. `for i, item in enumerate(items): ...`.\n", "- `zip(list1,list2)`: joint iteration over two lists\n", "- Low-level iterator functions: `iter()` and `next()`\n", "\n", "\n", "The function for input/output (I/O) operations are:\n", "\n", "- `input`: prompt the user for text input\n", "- `print(arg1,arg2, ...)`: print `str(arg1)`, `str(arg2)`, `...` to the screen or file\n", "- `open(filepath,mode)`: open the file at `filepath` for reading or writing.\n", " Use `mode=\"r\"` for reading or `mode=\"w\"` for writing.\n", "\n", "\n", "Here are some functions you can use when \"looking around\"\n", "to find more information about objects:\n", "\n", "- `str(obj)`: display the string representation of the object.\n", "- `repr(obj)`: display the Python representation of the object.\n", " Usually, you can copy-paste the output of `repr(obj)` into a Python\n", " shell to re-create the object.\n", "- `help(obj)`: display info about the object, function, or method.\n", " This is equivalent to printing object's docstring `obj.__doc__`.\n", "- `dir(obj)`: show complete list of attributes and methods of the object `obj`\n", "- `globals()`: display all variables in the Python global namespace\n", "- `locals()`: display local variables (within current scope)\n", "\n", "\n", "Advanced functions:\n", "\n", "- Functional stuff: `map()`, `eval()`, `exec()`\n", "- Meta-programming: `hasattr()`, `getattr()`, `setattr()`\n", "- Object oriented programming: `isinstance()`, `issubclass()`, `super()`\n" ] }, { "cell_type": "markdown", "id": "d16614b2-850a-43a3-8b86-081bf71aa435", "metadata": {}, "source": [ "## Python punctuation\n", "\n", "The most difficult part of learning Python\n", "is the use of non-word punctuation characters,\n", "which have very specific meaning and take some time to get used to.\n", "Let's review and summarize how the symbols `=([{*\"'#,.:` are used in Python.\n", "\n", "\n", "\n", "- The hash symbol `#` is used for comments.\n", "\n", "
\n", "\n", "\n", "- Asterisk `*` is the multiplication operator,\n", " while the double asterisk `**` is the exponent operator.\n", "\n", "
\n", "\n", "- Equal sign `=` is used for:\n", " - assignment statements: `x = 5`\n", " - pass keyword arguments to functions: `print(\"ans\",3,sep=\":\")`\n", " - (advanced) specify default keyword argument in function definitions\n", " \n", "
\n", "\n", "- The comparison operators `<`, `>=`, `<=`, `==` (equal to), and `!=` (not equal to)\n", " are used compare relative size of numerical values: `x > 2`.\n", "\n", "
\n", "\n", "- Parentheses `()` are used for:\n", " - function definitions: `def fun(x): ...`)\n", " - calling functions: `print(3)`\n", " - enforcing order of operations: `(x + y) * z`\n", " - defining tuples: `(1, 2, 3)`\n", "\n", "
\n", "\n", "- Comma `,` is used as:\n", " - element separator in lists, tuples, sets, etc.\n", " - separator between `key:value` pairs when defining a dictionary\n", " - separate function arguments in function definitions\n", " - separate function arguments when calling functions\n", "\n", "\n", "
\n", "\n", "- Curly-brackets (accolades) `{}` are used to\n", " - define dictionaries: `mydict = {\"key\":\"value\", \"k2\":\"v2\"}`\n", " - define sets: `{1,2,3}`\n", "\n", "
\n", "\n", "- Square brackets `[]` are used for:\n", " - defining lists: `mylist = [1, 2, 3]`\n", " - accessing list elements: `mylist[2]`\n", " - list slicing: `mylist[0:2]` = first two items in `mylist`\n", " - accessing dictionary values by key: `mydict[\"key\"]`\n", "\n", "
\n", "\n", "\n", "- Quotes `\"` or `'` are used to define string literals.\n", " There are several variations you can use:\n", " - raw strings `r\"...\"` are be used for reduce escape characters\n", " - f-strings `f\"...\"` are used to include variables\n", " - triple quotes `\"\"\"` or `'''` are used to define entire paragraphs\n", "\n", "
\n", "\n", "- Colon `:` is used for:\n", " - starting an indented code block in statements\n", " like `if`, `elif`, `else`, `for`, etc.\n", " - key: value separator in dict literals\n", " - slice notation: `0:2 == slice(0,2) == [0,1]`\n", "\n", "
\n", "\n", "\n", "- Period `.` is used as:\n", " - decimal separator for floating point literals: `4.2`\n", " - access object attributes and methods: `msg.upper()`\n", " - access names within a namespace: `statistics.mean`\n", "\n", "
\n", "\n", "\n", "- Semicolon `;` can be used to put multiple commands on single line. \n", " We sometimes use `;` at the end of a line\n", " to suppress the output of a command in a notebook environment.\n", "\n", "\n", "\n", "" ] }, { "cell_type": "markdown", "id": "86279f2e-67c0-4646-8853-6717e710f92c", "metadata": { "tags": [] }, "source": [ "# Applications\n", "\n", "I want to tell you about some of the cool Python applications\n", "you can look forward to if you choose to develop your Python skills further.\n", "Python is not just a calculator,\n", "but a general-purpose programming language,\n", "so it can be used for many applications.\n", "We already mentioned Python uses in scientific computing and data science.\n", "Here are some other areas where Python programming is popular.\n", "\n", "- **Command line scripts**: you can put Python commands into a script,\n", " then run them on the command line (terminal in UNIX or `cmd.exe` in Windows).\n", " For example,\n", " you can write a simple script that downloads music videos from YouTube\n", " and saves them as .mp3 files you can listen to offline.\n", "\n", "- **Graphical user interface (GUI) programs**: many desktop applications are written in Python.\n", " An example of a graphical, point-and-click application written in Python is\n", " [Calibre](https://calibre-ebook.com/),\n", " which is a powerful eBook management library,\n", " eBook reader, and eBook converter that supports every imaginable eBook format.\n", "\n", "- **Web applications**: the [Django](https://www.djangoproject.com/)\n", " and [Flask](https://flask.palletsprojects.com/) frameworks\n", " are often used to build web applications.\n", " Many of the websites you access every day have as server component written in Python.\n", "\n", "- **Machine learning**: most of the current machine learning (a.k.a. artificial intelligence)\n", " research and development is done using Python code.\n", "\n", "- **Glue code**: whenever you have some process that needs to take data from one place\n", " and bring it into another program,\n", " Python is a good choice to automate this process.\n", " \n", "- **Cloud automation**: you can use Python scripts to automate the entire IT infrastructure of a company.\n", "\n", "\n", "I mention these examples so you'll know the other possibilities enabled by Python,\n", "beyond the basic \"use Python interactively like a calculator\" code examples\n", "that we saw in this tutorial.\n", "We're at the end of this tutorial,\n", "but just the beginning of your journey to discover\n", "all the interesting things you can do with Python." ] }, { "cell_type": "markdown", "id": "ffcfa40e-f15a-45c4-9cfd-d59c3d6bd7b5", "metadata": {}, "source": [ "# Links\n", "\n", "I've compiled a list of the best Python learning resources for you.\n", "\n", "## Python cheat sheets\n", "\n", "- Good quick reference \n", " https://gto76.github.io/python-cheatsheet/\n", "- https://ehmatthes.github.io/pcc_2e/cheat_sheets/cheat_sheets/\n", "- https://ipgp.github.io/scientific_python_cheat_sheet/\n", "- https://learnxinyminutes.com/docs/python/\n", "- https://perso.limsi.fr/pointal/_media/python:cours:mementopython3-english.pdf\n", "- https://www.pythoncheatsheet.org/\n", "- https://homepage.univie.ac.at/michael.blaschek/media/Cheatsheet_python.pdf\n", "- https://cheatsheets.quantecon.org/\n", "\n", "\n", "## Tutorials\n", "\n", "- Python tutorial by Russell A. Poldrack: \n", " https://statsthinking21.github.io/statsthinking21-python/01-IntroductionToPython.html\n", "- Programming with Python by Software Carpentry team: \n", " https://swcarpentry.github.io/python-novice-inflammation/\n", "- Official Python tutorial: \n", " https://docs.python.org/3/tutorial/\n", "- Python glossary: \n", " https://docs.python.org/3/glossary.html\n", "- Nice tutorial: \n", " https://www.pythonlikeyoumeanit.com/\n", "- Python data structures: \n", " https://devopedia.org/python-data-structures\n", "- Further reading: \n", " https://github.com/rasbt/python_reference\n", "- Online tutorial: \n", " https://www.kaggle.com/learn/python\n", "- Complete list of all the Python built-ins: \n", " https://treyhunner.com/2019/05/python-builtins-worth-learning/ \n", " [via](https://news.ycombinator.com/item?id=30621552)\n", "- Video lectures from a Python course by Chelsea Parlett-Pelleriti: \n", " https://www.youtube.com/playlist?list=PLmxpwhh4FDm460ztGwXmIUcGmfNzf4NMW\n", "\n", "\n", "\n", "## Special topics\n", "\n", "- Stats-related python functions: \n", " https://www.statology.org/python-guides/\n", "- Python types (`int`s, `float`s, and `bool`s) \n", " https://github.com/anthony-agbay/introduction-to-python/blob/main/modules/basic-python-types-ints-floats-bools/basic-python-types-ints-floats-bools.ipynb\n", "- Python string operations: \n", " https://github.com/anthony-agbay/introduction-to-python/blob/main/modules/basic-python-types-strings/basic-python-types-strings.ipynb\n", "- Scientific computing: \n", " https://devopedia.org/python-for-scientific-computing\n", "\n", "\n", "\n", "\n", "## Books\n", "\n", "- *Learn Python the Right Way* by Ritza is an excellent book for beginners \n", " https://learnpythontherightway.com/\n", "- *Automate the Boring Stuff with Python* by Al Sweigart\n", " is another excellent book with lots of examples of applications.\n", " https://automatetheboringstuff.com/\n", "- Object-Oriented Programming in Python is another free book. \n", " https://python-textbok.readthedocs.io/\n", "\n", "\n", "**TODO**: triage and convert to long URLs to tinyurls \n", "\n", "\n", "" ] } ], "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.9.4" }, "toc-autonumbering": false, "toc-showmarkdowntxt": false }, "nbformat": 4, "nbformat_minor": 5 }