From eb35cf1da807da9fba0048d16dbdd12f9d6ad2ec Mon Sep 17 00:00:00 2001 From: crimpmug <113626525+crimpmug@users.noreply.github.com> Date: Wed, 18 Jan 2023 15:09:13 -0800 Subject: [PATCH 1/7] Add files via upload --- .../L02 Variables and Expressions mine.ipynb | 2811 +++++++++++++++++ 1 file changed, 2811 insertions(+) create mode 100644 lecture_notebooks/L02 Variables and Expressions mine.ipynb diff --git a/lecture_notebooks/L02 Variables and Expressions mine.ipynb b/lecture_notebooks/L02 Variables and Expressions mine.ipynb new file mode 100644 index 0000000..0754515 --- /dev/null +++ b/lecture_notebooks/L02 Variables and Expressions mine.ipynb @@ -0,0 +1,2811 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "cjsgMup6ktUd", + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# Lecture 2 - Variables and Expressions (https://bit.ly/intro_python_02)\n", + "\n", + "Today:\n", + "* Variables\n", + "* Assignment is not equals\n", + "* Copying References\n", + "* Expressions\n", + "* Statements\n", + "* Variable Names\n", + "* Operators\n", + "* Abbreviated assignment\n", + "* Logical operators\n", + "* Order of operations\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "OkA6aiONlE7b", + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# Variables" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "executionInfo": { + "elapsed": 363, + "status": "ok", + "timestamp": 1607391378206, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "7vBJtWFslI-G", + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "# As in algebra, we use variables to 'refer' to things\n", + "\n", + "x = 1 # x is a variable referring to the int 1\n", + "\n", + "y = \"foo\" # y is a variable referring to the string \"foo\"" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "S0j3Xtd1DhKp", + "slideshow": { + "slide_type": "fragment" + } + }, + "source": [ + "Think of x as \"referring to 1\". That is x is really a reference to the object representing 1. \n", + " * x -----> 1\n", + " * y ------> \"foo\"\n", + "\n", + "Under the hood, in general, there is a piece of memory (bits) representing x \n", + "that stores the location of where the value 1 is actually stored in memory. \n", + "\n", + "Sometimes, in some languages, x or y stores the content directly, e.g. x is actually \n", + "memory representing 1. However in Python variables are references." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "s8eUIhrM9xjr", + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# Assignment is not equals" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 349, + "status": "ok", + "timestamp": 1607391380918, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "1ADmdsvtEpAw", + "outputId": "9ade660c-a1b3-47e7-d1df-cbc2143b5d70", + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "foo\n" + ] + } + ], + "source": [ + "# The consequence of this \"refers to\" notion is that \n", + "# equals sign in Python means \"assignment\" not \"equals\"\n", + "\n", + "x = 1 # x refers to 1\n", + "\n", + "# Because = means assignment, we can update assignments, without it\n", + "# being meaningless in a math sense\n", + "\n", + "x = \"foo\" # x refers to \"foo\"\n", + "\n", + "#What's the value of x?\n", + "print(x)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "toK-UKQ198jr", + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# Copying references" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 373, + "status": "ok", + "timestamp": 1607391393919, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "FndrQqcuFho6", + "outputId": "3f7bfc04-874e-4ad0-e328-ef219333c8c5", + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5\n" + ] + } + ], + "source": [ + "# We can also copy variables\n", + "\n", + "x = 5\n", + "\n", + "y = x # Now x and y refer to the same thing (5)\n", + "\n", + "print(y)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 411, + "status": "ok", + "timestamp": 1607391407291, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "4ous2ZHC6Z1h", + "outputId": "7b6a6f09-34a4-42b3-a068-5232da47c35d", + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10\n", + "5\n" + ] + } + ], + "source": [ + "# But note, reassignment and copying can be co-mingled:\n", + "\n", + "x = 5\n", + "\n", + "y = x # Now x and y refer to the same thing (5)\n", + "\n", + "x = 10 # But now we change what x refers to, this doesn't change y!\n", + "\n", + "print(x) \n", + "print(y)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "sQLo2H1qGadW", + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# Variable Names\n", + "\n", + "Variables don't have to be single letters (in fact, that's a terrible idea), but can be **any combination of alpha-numeric characters and the '_' symbol, providing they don't start with a number and aren't already a reserved keyword**. Remember this!" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 424, + "status": "ok", + "timestamp": 1607391414262, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "p92pX5KtHHHk", + "outputId": "3f342de4-5e6a-4e09-ffca-46fd608459dd", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4\n" + ] + } + ], + "source": [ + "# Variable names\n", + "\n", + "foo = 1 # Legit variable name\n", + "\n", + "bar2 = 2 # Also legit\n", + "\n", + "_boo = 3 # Legit\n", + "\n", + "super_helpful_variable_name = 4 # Legit\n", + "\n", + "somePeopleLikeCamelCase = 5 # Fine variable name\n", + "\n", + "print(super_helpful_variable_name)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 130 + }, + "executionInfo": { + "elapsed": 381, + "status": "error", + "timestamp": 1607391419362, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "wkwn4Z1PHcjy", + "outputId": "03ea9788-5615-4905-91de-02ae1765e4b7", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "ignored", + "output_type": "error", + "traceback": [ + "\u001B[0;36m File \u001B[0;32m\"\"\u001B[0;36m, line \u001B[0;32m3\u001B[0m\n\u001B[0;31m 3l33t = 4 # Not okay\u001B[0m\n\u001B[0m ^\u001B[0m\n\u001B[0;31mSyntaxError\u001B[0m\u001B[0;31m:\u001B[0m invalid syntax\n" + ] + } + ], + "source": [ + "# Not okay\n", + "\n", + "3l33t = 4 # Not okay, starts with a number\n", + "\n", + "not^okay = 5 # Not okay, contains an illegal character\n", + "\n", + "and = 6 # Not okay, is a reserved keyword\n", + "\n", + "# Etc." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "U9VGvqwEHQtf", + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "Reserved key words, see Chapter 2 of the open textbook:\n", + "\n", + "http://openbookproject.net/thinkcs/python/english3e/variables_expressions_statements.html" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "bHeo34G5IWAN", + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# Expressions" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 349, + "status": "ok", + "timestamp": 1607391427113, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "dhSAuuO-Ich7", + "outputId": "4207f127-b0be-4ba3-c85e-813ab30a4ae7", + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Now we have basic types and variables, we can do stuff with them\n", + "\n", + "x = 1\n", + "\n", + "y = 2\n", + "\n", + "x + y - 2 # This is an expression, the interpreter\n", + "# has to \"evaluate it\" to figure out the value by \n", + "# combining variables (x, y), \n", + "# values (2) and operators (+, -)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 373, + "status": "ok", + "timestamp": 1607391431652, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "ega2_raeI6uc", + "outputId": "2e98e03f-f8e3-4a04-f134-52f3c027ba67", + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "float" + ] + }, + "execution_count": 10, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "# When we're calling a function (we'll learn about functions soon) (here the \"type\" function), \n", + "# we're also evaluating an expression\n", + "\n", + "type(3.14)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "OlhlFm06JFmO", + "slideshow": { + "slide_type": "fragment" + } + }, + "source": [ + "**Definition:** An expression is a combination of values, variables, operators, and calls to functions. Expressions need to be evaluated and result in a value (could be number, a string, an object, a list, etc.) \n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "# Challenge 1" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "# Write an expression that uses x and y and evaluates to 3\n", + "x = 1\n", + "y = 2" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# Statements" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 614, + "status": "ok", + "timestamp": 1607391462358, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "MhVakYkCJ0wv", + "outputId": "75f12273-51e2-4c97-91be-b93aa1d4c604", + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "boo\n" + ] + } + ], + "source": [ + "# Statements are instructions to the interpreter\n", + "\n", + "x = 1 # This is a statement, you're telling Python, make x refer to the value 1\n", + "\n", + "if x > 0: # Conditionals (like if (and others we'll see soon, \n", + " # are also \"statements\"))\n", + " print(\"boo\")" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "invalid syntax (450256079.py, line 5)", + "output_type": "error", + "traceback": [ + "\u001B[0;36m Cell \u001B[0;32mIn[5], line 5\u001B[0;36m\u001B[0m\n\u001B[0;31m print(x-=1)\u001B[0m\n\u001B[0m ^\u001B[0m\n\u001B[0;31mSyntaxError\u001B[0m\u001B[0;31m:\u001B[0m invalid syntax\n" + ] + } + ], + "source": [ + "# However, assignment statements, such as x -= y will not print\n", + "\n", + "# This is because assignment statements don't return a value for python to print\n", + "\n", + "# In this way we can differentiate from expressions\n", + "\n", + "print(x-=1)" + ], + "metadata": { + "collapsed": false + } + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Fmbh2g2FKdNd", + "slideshow": { + "slide_type": "fragment" + } + }, + "source": [ + "* A statement is everything that can make up a line of Python code.\n", + "\n", + "* A statement does something.\n", + "\n", + "Note, therefore, that expressions are statements as well.\n", + "\n", + "* All expressions are statements.\n", + "* Not all statements are expressions.\n", + "\n", + "\n", + "Note: you may see slightly different definitions in textbooks, but I like these" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "mNIC7nQkOxEP", + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# Operators" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 395, + "status": "ok", + "timestamp": 1607391472811, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "o-rCwk1oOwM1", + "outputId": "755ba91e-de2a-4182-ce62-687d269f404b", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "17" + ] + }, + "execution_count": 13, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "# Operators perform basic operations on objects\n", + "\n", + "# Let's start with arithmetic operators\n", + "\n", + "x = 12\n", + "y = 5\n", + "\n", + "x + y # + is the addition operator, duh" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 386, + "status": "ok", + "timestamp": 1607391476046, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "RTsa3LB9PzAK", + "outputId": "2cb111a5-d282-41f3-b67f-69058e3f23b5", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "60" + ] + }, + "execution_count": 14, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "x * y # multiplication" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 372, + "status": "ok", + "timestamp": 1607391477584, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "vZaiIhnOP4UW", + "outputId": "aaf74190-452c-44ac-8faa-70270bac0384", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x ** y # exponentiation " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "source": [ + "Note: The ** operator may be new to you, so try and remember it and difference with the * operator." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 383, + "status": "ok", + "timestamp": 1607391479027, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "tpccI3SiP7Ol", + "outputId": "9214331e-4f89-4374-cf52-b994b7c0b2c2", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x / y # What's the value of this one ?" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 389, + "status": "ok", + "timestamp": 1607391480313, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "BFUrcwMXQEL3", + "outputId": "3bd6b58a-c8dc-435d-9a27-ad1f03930f60", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 17, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "x // y # And this one?" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "source": [ + "Note: learn and remember the difference between the integer division (//) and division (/) operators!" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 378, + "status": "ok", + "timestamp": 1607391482032, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "tHknzzHjp-wF", + "outputId": "2e373b3c-1bfb-4825-d4c7-4a02db28efc8", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "-12" + ] + }, + "execution_count": 18, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "-x # Yup, we have negation" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "pTCYcOf4qDrm", + "slideshow": { + "slide_type": "fragment" + } + }, + "source": [ + "Note: \n", + " * Negation is an example of a unary operator (it takes one operand)\n", + "\n", + " * The other operators are binary (they take two operands)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 364, + "status": "ok", + "timestamp": 1607391485528, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "wKJ17ugFsXo1", + "outputId": "359750e3-3454-4c56-f20d-40a0c665c67c", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 19, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "5 % 2 # The modulus operator, it returns the \"remainder\"" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 335, + "status": "ok", + "timestamp": 1607391487065, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "-d4YQzH1slYv", + "outputId": "2047f716-b6da-4148-b2c9-308ad5297796", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# It's a good way to determine if a number is divisible by another,\n", + "# because if it is, the expression value will be 0\n", + "4 % 2" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "# Challenge 2" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "x = 5\n", + "y = 2\n", + "# Write a statement that divides x by y, forgetting the remainder, storing the result in a variable z" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "-_noHbEBglV5", + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# Operator Overloading" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 367, + "status": "ok", + "timestamp": 1607391491626, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "VM61C-mG8z10", + "outputId": "eb800a43-f593-4dd0-b149-c9c40f311a1c", + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "27" + ] + }, + "execution_count": 21, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "5 + 10 + 12 # We can use \"+\" to add together strings of numbers" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "executionInfo": { + "elapsed": 344, + "status": "ok", + "timestamp": 1607391522629, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "zsIwvpbFRzvf", + "outputId": "2bc48222-e07d-4f25-ef67-6e3be2cd7aab", + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "('This', 'is', 'contrived')" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Some arithmetic operators are also \"overloaded\" to work with strings\n", + "# (This is an example of \"polymorphism\", that we'll meet again much later)\n", + "\n", + "\"This\" + \"is\" + \"contrived\" # The '+' concatenates strings" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 198 + }, + "executionInfo": { + "elapsed": 319, + "status": "error", + "timestamp": 1607391525883, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "cggT1iCJN7AI", + "outputId": "57c8840f-f089-4328-e414-5a97914ce860", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "ename": "TypeError", + "evalue": "ignored", + "output_type": "error", + "traceback": [ + "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m", + "\u001B[0;31mTypeError\u001B[0m Traceback (most recent call last)", + "\u001B[0;32m\u001B[0m in \u001B[0;36m\u001B[0;34m()\u001B[0m\n\u001B[1;32m 2\u001B[0m \u001B[0;31m# a number together\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[1;32m 3\u001B[0m \u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0;32m----> 4\u001B[0;31m \u001B[0;34m\"this\"\u001B[0m \u001B[0;34m+\u001B[0m \u001B[0;36m5\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0m", + "\u001B[0;31mTypeError\u001B[0m: must be str, not int" + ] + } + ], + "source": [ + "# Note, this doesn't work because Python doesn't know how to add a string and\n", + "# a number together\n", + "\n", + "\"this\" + 5" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "9XAjll9RUokG", + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# Abbreviated Assignment\n", + "\n", + "You will see this a lot:" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 343, + "status": "ok", + "timestamp": 1607391531326, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "Ls-IKDqWUu0u", + "outputId": "ccf895e0-81d1-4501-c0a6-a08dddb0dbe2", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "11" + ] + }, + "execution_count": 26, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "x = 1\n", + "\n", + "# Instead of writing\n", + "\n", + "x = x + 5\n", + "\n", + "# you can use the shorthand:\n", + "\n", + "x += 5 # This means \"add 5 to value x refers to\"\n", + "\n", + "x" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "1zKtAWt7Uvzo", + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "This works for all the obvious math operators:" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 370, + "status": "ok", + "timestamp": 1607391536440, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "DWhES0_nU0xS", + "outputId": "93b32e09-3dff-49b0-ed65-0121ead40033", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "22\n" + ] + } + ], + "source": [ + "\n", + "\n", + "x *= 2 # multiply x by 2, aka x = x * 2\n", + "\n", + "print(x)" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 357, + "status": "ok", + "timestamp": 1607391540567, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "536MyapkU21H", + "outputId": "d9cf7d10-91f6-412e-f8ab-355a8488bdb4", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "19\n" + ] + } + ], + "source": [ + "x -= 3 # subtract three, aka x = x - 3\n", + "\n", + "print(x)" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 372, + "status": "ok", + "timestamp": 1607391542854, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "GonNznhrU6E1", + "outputId": "092d1fb9-e1c9-4786-d001-36518e69dd74", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "9.5\n" + ] + } + ], + "source": [ + "x /= 2 # divide by 2 \n", + "\n", + "print(x)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 358, + "status": "ok", + "timestamp": 1607391544853, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "Sm7tItanU8WA", + "outputId": "c5bbbc82-a61d-4cf8-cc55-cc1161489f4a", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n" + ] + } + ], + "source": [ + "x //= 2 # divide by 2 and forget fraction\n", + "\n", + "print(x)" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 387, + "status": "ok", + "timestamp": 1607391549191, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "WRp0r1tAU_Kg", + "outputId": "877fa5c8-c703-4696-a147-da34228da6f3", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0.0\n" + ] + } + ], + "source": [ + "x %= 2 # Take x mod 2 and store the result in x, i.e. x = x % 2\n", + "\n", + "print(x)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "# Challenge 3" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "x = 7 \n", + "y = 2\n", + "# Use abbreviated assignment to subtract y from x, storing the result as x" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "6oYe0AgQl7kl", + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# Boolean Type\n", + "\n", + "Python has a special boolean (binary) type, which can either be 'True' or 'False'. \n", + "\n", + "This is essential for logical expressions.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 342, + "status": "ok", + "timestamp": 1607391563443, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "12mmMhhGmEmz", + "outputId": "b52afd9e-f599-4cd7-e635-58f70bff9a18", + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "bool" + ] + }, + "execution_count": 32, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "type(True)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 387, + "status": "ok", + "timestamp": 1607391564807, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "IN_9Y3UdmPVi", + "outputId": "fe44676e-cbf3-4151-b441-2bf6bb4447a3", + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "bool" + ] + }, + "execution_count": 33, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "type(False)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "SjGU71_1QWqx", + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# Logical Operators\n", + "\n", + " Booleans are used for making decisions in your program.\n", + " \n", + " To do this we use logical operators, which do, err, logic and evaluate to booleans." + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 601, + "status": "ok", + "timestamp": 1607391567269, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "bKqHlRGZ6Vyz", + "outputId": "989aeb11-c58f-4f9a-b43c-9ca6f21ebe34", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 34, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "x = 5\n", + "y = 10\n", + "\n", + "x > y # The greater than operator compares two things" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 361, + "status": "ok", + "timestamp": 1607391568662, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "WowJC26dmoPX", + "outputId": "fd4ea3be-d279-45e5-b6e2-5aeb14e4fb6f", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "bool" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# To see the relationship with Booleans\n", + "x = 5\n", + "y = 10\n", + "\n", + "\n", + "type(x > y)" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 385, + "status": "ok", + "timestamp": 1607391570353, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "SJcNUseq6VzG", + "outputId": "e12c144f-3997-416b-9cd5-66c3b31a5a02", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 36, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "# There are a bunch of these\n", + "\n", + "x >= y # Is x greater than or equal to y?" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 403, + "status": "ok", + "timestamp": 1607391571943, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "hDYP9S1J6VzI", + "outputId": "1c178c9c-9aa6-41a2-eb98-de7169e6cba9", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 37, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "x < y # Is x less than y?" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 343, + "status": "ok", + "timestamp": 1607391579454, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "i_JdqLQi6VzK", + "outputId": "9d69d1b9-25c8-4bd3-b678-436670d83b96", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 38, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "x <= y # Less than or equals?" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 321, + "status": "ok", + "timestamp": 1607391580841, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "fXbNwOfd6VzM", + "outputId": "878ea4b5-c993-4510-ce61-bb94482ba231", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# What about this one?\n", + "\n", + "x == y" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "oEu8nmKg6VzR", + "slideshow": { + "slide_type": "fragment" + } + }, + "source": [ + "* As we discussed earlier, in Python (and many languages) '=' is the assignment operator\n", + "* Logical equals is '=='. Some people find this weird, but you get over it.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 336, + "status": "ok", + "timestamp": 1607391583102, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "hkZW2hBN_F9k", + "outputId": "b95dc957-8a08-4321-fc66-b2e182f523db", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 40, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "# Python also has the not logical equals operator !=\n", + "\n", + "x != y # Read this as \"does x not equal y?\"" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 344, + "status": "ok", + "timestamp": 1607391585169, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "IGoHXsjM6VzR", + "outputId": "49896c73-c773-4e13-b826-19ff54a18be8", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 41, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "# We can compose logical statements into complex expressions \n", + "# using logical 'and' and 'or'\n", + "\n", + "x = 5\n", + "y = 10\n", + "z = 7\n", + "\n", + "x >= y or z > x # Says: True if x is greater than or equal to y or z is greater than x" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 366, + "status": "ok", + "timestamp": 1607391587116, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "BI0J-9Bt6VzV", + "outputId": "17a0145a-4299-4342-fe62-f20df8342b62", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 42, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "# Similarly\n", + "\n", + "y > x and y > z # Says: True if y is greater than x AND y is greater than z" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 344, + "status": "ok", + "timestamp": 1607391588715, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "fYlNC0fz6VzY", + "outputId": "abf04d8b-c83b-4ee8-ee70-ec4fc25269ed", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 43, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "# There is also the unary negation operator: not\n", + "\n", + "not True" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 345, + "status": "ok", + "timestamp": 1607391590335, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "ffSYgAtK6Vze", + "outputId": "cdf7bcbc-8c10-4244-b435-c49b1bbc592e", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 44, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "# Use it to switch True to False and vice versa:\n", + "\n", + "y = 0\n", + "x = 1\n", + "\n", + "not y > x" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "cNDSDbL86Vzh", + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "Logical comparisons also work with strings" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 338, + "status": "ok", + "timestamp": 1607391592411, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "NEN92T0s6Vzk", + "outputId": "9442c91d-f428-463a-fea0-0343ce7b5acd", + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 45, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "# String comparison\n", + "\n", + "\"cats\" > \"dogs\" # ?" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "# Challenge 4" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "x = int(input(\"Enter a number\"))\n", + "y = int(input(\"Enter a second number\"))\n", + "# Write a logical expression that is True if and only if \n", + "# x is greater than y or x is divisible by y" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# Truthy and Falsy Values\n", + "\n", + "Values can evaluate as either True or False in logical expressions despite not being of a boolean type" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "bool(\"\") # The empty string is false" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "bool(\"hello\") # A non-empty string is true" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "source": [ + "We'll see more examples of how different types map to True and False later (but see the note for a summary, looking ahead)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "notes" + } + }, + "source": [ + "This becomes increasingly helpful as we start to deal with more complex data structures. \n", + "\n", + "#### The values that will evaluate as false are:\n", + "- Empty lists []\n", + "- Empty tuples ()\n", + "- Empty dictionaries {}\n", + "- Empty sets set()\n", + "- Empty strings \"\"\n", + "- Empty ranges range(0)\n", + "- And any numeric type that is 0; Int, Float, Complex\n", + "\n", + "In contrast the values that evaluate as true are:\n", + "- Non-Empty strings \"hello\"\n", + "- Non-Empty tuples (\"apple\", \"banana\")\n", + "- Non-Empty dictionaries { id: 10, name: \"Aiden\"}\n", + "- No\n", + "- Any Non zero numeric type" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "CLy5-zuqmV6k", + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# Order of operations\n", + "\n", + "This is a boring topic, but if you don't understand it, you'll write lots of bugs" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "source": [ + "* Just like math, Python follows PEMDAS - or PE(MD)(AS). \n", + " * That is: Parentheses, Exponentiation, Multiplication and Division (left to right), and Addition and Subtraction(left to right)\n", + "* After the math operators, in order of presidence, are the relational operators (e.g. greater than, less than, etc.), and lastly the logical operators “not”, “and”, & “or” in that order." + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 342, + "status": "ok", + "timestamp": 1607391600951, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "g0g2n8E7mkSf", + "outputId": "df09bf20-fa8c-4ed9-e670-97503a94867d", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "8" + ] + }, + "execution_count": 47, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "# e.g.\n", + "\n", + "x = 2\n", + "y = 3\n", + "\n", + "x * y + x # Yup, this works just like math" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 366, + "status": "ok", + "timestamp": 1607391602661, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "kcKJf8C0nGIQ", + "outputId": "d4675860-1131-433d-c9dc-78a447ca072b", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "10" + ] + }, + "execution_count": 48, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "x * (y + x) # If you want to force the addition \n", + "# before the multiplication you can use brackets, like in math\n", + "\n", + "# Technically, the brackets have highest precedence of any operator" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 350, + "status": "ok", + "timestamp": 1607391604199, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "Y7Zjv-vPoEjQ", + "outputId": "5780e325-2091-4cd1-8a89-00f26eccda95", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "8" + ] + }, + "execution_count": 49, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "# Exponents have higher precedence than division/multiplication\n", + "\n", + "2 * 2**2 # The exponent happens first" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 372, + "status": "ok", + "timestamp": 1607391605731, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "QO7dKEBPnsoH", + "outputId": "b5fe6c37-fe98-46f4-ba9c-f7c117422efc", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 50, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "# Arithmetic operators have precidence over logical operators\n", + "\n", + "x * y > x + y # This could also be written (x * y) > (x + y)" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 499, + "status": "ok", + "timestamp": 1607391607443, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "AZ32yKDSsARP", + "outputId": "320ef43f-b98f-4d1f-8f1d-c694a9e08aa1", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 51, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "# Sometimes it is helpful to use brackets, because it's hard to remember\n", + "# the order of operations, consider this..\n", + "\n", + "not 3 * 5 > 10 and 2 > 1" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 541, + "status": "ok", + "timestamp": 1607391609440, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "1hIvAe9PtOFQ", + "outputId": "d0f2cc99-323d-4411-eaea-f3f107cd5bb2", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 52, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "# Which I think is much clearer as..\n", + "\n", + "(not 3 * 5 > 10) and (2 > 1) # The brackets are just there for clarity" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 363, + "status": "ok", + "timestamp": 1607391611076, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "HDtWTEzotec7", + "outputId": "bcb4c84f-1088-46d7-f8c3-0eb85f438f36", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 53, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "# Or maybe even.. \n", + "\n", + "(not (3 * 5 > 10)) or (2 > 1)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "# Suggested exercise: play with >, <, >=, <=, not, and, or, () \n", + "# and see if they do what you expect\n", + "\n", + "## Also definitely read text book on this for more thorough treatment" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "# Simplifying Complex Expressions" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Sometimes you're faced with a complex expression like:\n", + "\n", + "x = \"cats\"\n", + "y = \"dogs\"\n", + "\n", + "not (x > y or not x < y) or x == y # This is not very easy to parse!\n", + "\n", + "# To understand it try breaking it down into pieces by substituting the sub-expressions for True or False\n", + "# step by step, applying the presidence rules\n", + "\n", + "not (False or not True) or False # Here I substituted sub-expressions with True or False\n", + "\n", + "not (False or False) or False # More substitution\n", + "\n", + "not False or False # And again\n", + "\n", + "True or False # Nearly there\n", + "\n", + "True ## Done\n", + " \n", + "# Obviously, the compute has no problem evaluating this...\n", + "\n", + "not (x > y or not x < y) or x == y # Yup, it's True" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "source": [ + "Suggested exercise: try making up a complex expression and then simplify it by hand. You can use the Python interpretor to check that your result is correct." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "# Challenge 5" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "# Change the expression below so that the 'not' is applied after the 'or' \n", + "(not (3 * 5 > 10)) or (2 > 1)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "# Challenge 6" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "# Remove the parentheses in the expression and see what would it print \n", + "x = 3 \n", + "y = 2 \n", + "print((not x > y and not x < y) or x//y == 2)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ZwejXNuJaC-g", + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# Reading\n", + "\n", + "Openbook:\n", + "\n", + "* Read Chapter 2 on expressions, variables, statements and operators:\n", + " * http://openbookproject.net/thinkcs/python/english3e/variables_expressions_statements.html\n", + "\n", + " \n", + "# Homework\n", + "\n", + "* Go to Canvas and complete the second lecture quiz, which involves completing each challenge problem\n", + "* See \"Reading 2\" in Zybooks" + ] + } + ], + "metadata": { + "celltoolbar": "Slideshow", + "colab": { + "collapsed_sections": [ + "cjsgMup6ktUd" + ], + "name": "L02 Variables and Expressions.ipynb", + "provenance": [ + { + "file_id": "17GPUwfjMAW-SkqDI2qb9LangNzYzgNH_", + "timestamp": 1547074807405 + } + ] + }, + "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.9" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} From 50abd675fc97c32f6109bc9e93458da7b63b46c0 Mon Sep 17 00:00:00 2001 From: crimpmug <113626525+crimpmug@users.noreply.github.com> Date: Wed, 18 Jan 2023 15:09:49 -0800 Subject: [PATCH 2/7] Delete L02 Variables and Expressions.ipynb --- .../L02 Variables and Expressions.ipynb | 2789 ----------------- 1 file changed, 2789 deletions(-) delete mode 100644 lecture_notebooks/L02 Variables and Expressions.ipynb diff --git a/lecture_notebooks/L02 Variables and Expressions.ipynb b/lecture_notebooks/L02 Variables and Expressions.ipynb deleted file mode 100644 index f13908e..0000000 --- a/lecture_notebooks/L02 Variables and Expressions.ipynb +++ /dev/null @@ -1,2789 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "id": "cjsgMup6ktUd", - "slideshow": { - "slide_type": "slide" - } - }, - "source": [ - "# Lecture 2 - Variables and Expressions (https://bit.ly/intro_python_02)\n", - "\n", - "Today:\n", - "* Variables\n", - "* Assignment is not equals\n", - "* Copying References\n", - "* Expressions\n", - "* Statements\n", - "* Variable Names\n", - "* Operators\n", - "* Abbreviated assignment\n", - "* Logical operators\n", - "* Order of operations\n" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "OkA6aiONlE7b", - "slideshow": { - "slide_type": "slide" - } - }, - "source": [ - "# Variables" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": { - "executionInfo": { - "elapsed": 363, - "status": "ok", - "timestamp": 1607391378206, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "7vBJtWFslI-G", - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [], - "source": [ - "# As in algebra, we use variables to 'refer' to things\n", - "\n", - "x = 1 # x is a variable referring to the int 1\n", - "\n", - "y = \"foo\" # y is a variable referring to the string \"foo\"" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "S0j3Xtd1DhKp", - "slideshow": { - "slide_type": "fragment" - } - }, - "source": [ - "Think of x as \"referring to 1\". That is x is really a reference to the object representing 1. \n", - " * x -----> 1\n", - " * y ------> \"foo\"\n", - "\n", - "Under the hood, in general, there is a piece of memory (bits) representing x \n", - "that stores the location of where the value 1 is actually stored in memory. \n", - "\n", - "Sometimes, in some languages, x or y stores the content directly, e.g. x is actually \n", - "memory representing 1. However in Python variables are references." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "s8eUIhrM9xjr", - "slideshow": { - "slide_type": "slide" - } - }, - "source": [ - "# Assignment is not equals" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 349, - "status": "ok", - "timestamp": 1607391380918, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "1ADmdsvtEpAw", - "outputId": "9ade660c-a1b3-47e7-d1df-cbc2143b5d70", - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "foo\n" - ] - } - ], - "source": [ - "# The consequence of this \"refers to\" notion is that \n", - "# equals sign in Python means \"assignment\" not \"equals\"\n", - "\n", - "x = 1 # x refers to 1\n", - "\n", - "# Because = means assignment, we can update assignments, without it\n", - "# being meaningless in a math sense\n", - "\n", - "x = \"foo\" # x refers to \"foo\"\n", - "\n", - "#What's the value of x?\n", - "print(x)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "toK-UKQ198jr", - "slideshow": { - "slide_type": "slide" - } - }, - "source": [ - "# Copying references" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 373, - "status": "ok", - "timestamp": 1607391393919, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "FndrQqcuFho6", - "outputId": "3f7bfc04-874e-4ad0-e328-ef219333c8c5", - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "5\n" - ] - } - ], - "source": [ - "# We can also copy variables\n", - "\n", - "x = 5\n", - "\n", - "y = x # Now x and y refer to the same thing (5)\n", - "\n", - "print(y)" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 411, - "status": "ok", - "timestamp": 1607391407291, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "4ous2ZHC6Z1h", - "outputId": "7b6a6f09-34a4-42b3-a068-5232da47c35d", - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "10\n", - "5\n" - ] - } - ], - "source": [ - "# But note, reassignment and copying can be co-mingled:\n", - "\n", - "x = 5\n", - "\n", - "y = x # Now x and y refer to the same thing (5)\n", - "\n", - "x = 10 # But now we change what x refers to, this doesn't change y!\n", - "\n", - "print(x) \n", - "print(y)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "sQLo2H1qGadW", - "slideshow": { - "slide_type": "slide" - } - }, - "source": [ - "# Variable Names\n", - "\n", - "Variables don't have to be single letters (in fact, that's a terrible idea), but can be **any combination of alpha-numeric characters and the '_' symbol, providing they don't start with a number and aren't already a reserved keyword**. Remember this!" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 424, - "status": "ok", - "timestamp": 1607391414262, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "p92pX5KtHHHk", - "outputId": "3f342de4-5e6a-4e09-ffca-46fd608459dd", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "4\n" - ] - } - ], - "source": [ - "# Variable names\n", - "\n", - "foo = 1 # Legit variable name\n", - "\n", - "bar2 = 2 # Also legit\n", - "\n", - "_boo = 3 # Legit\n", - "\n", - "super_helpful_variable_name = 4 # Legit\n", - "\n", - "somePeopleLikeCamelCase = 5 # Fine variable name\n", - "\n", - "print(super_helpful_variable_name)" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 130 - }, - "executionInfo": { - "elapsed": 381, - "status": "error", - "timestamp": 1607391419362, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "wkwn4Z1PHcjy", - "outputId": "03ea9788-5615-4905-91de-02ae1765e4b7", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "ename": "SyntaxError", - "evalue": "ignored", - "output_type": "error", - "traceback": [ - "\u001B[0;36m File \u001B[0;32m\"\"\u001B[0;36m, line \u001B[0;32m3\u001B[0m\n\u001B[0;31m 3l33t = 4 # Not okay\u001B[0m\n\u001B[0m ^\u001B[0m\n\u001B[0;31mSyntaxError\u001B[0m\u001B[0;31m:\u001B[0m invalid syntax\n" - ] - } - ], - "source": [ - "# Not okay\n", - "\n", - "3l33t = 4 # Not okay, starts with a number\n", - "\n", - "not^okay = 5 # Not okay, contains an illegal character\n", - "\n", - "and = 6 # Not okay, is a reserved keyword\n", - "\n", - "# Etc." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "U9VGvqwEHQtf", - "slideshow": { - "slide_type": "subslide" - } - }, - "source": [ - "Reserved key words, see Chapter 2 of the open textbook:\n", - "\n", - "http://openbookproject.net/thinkcs/python/english3e/variables_expressions_statements.html" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "bHeo34G5IWAN", - "slideshow": { - "slide_type": "slide" - } - }, - "source": [ - "# Expressions" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 349, - "status": "ok", - "timestamp": 1607391427113, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "dhSAuuO-Ich7", - "outputId": "4207f127-b0be-4ba3-c85e-813ab30a4ae7", - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "1" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# Now we have basic types and variables, we can do stuff with them\n", - "\n", - "x = 1\n", - "\n", - "y = 2\n", - "\n", - "x + y - 2 # This is an expression, the interpreter\n", - "# has to \"evaluate it\" to figure out the value by \n", - "# combining variables (x, y), \n", - "# values (2) and operators (+, -)" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 373, - "status": "ok", - "timestamp": 1607391431652, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "ega2_raeI6uc", - "outputId": "2e98e03f-f8e3-4a04-f134-52f3c027ba67", - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "float" - ] - }, - "execution_count": 10, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "# When we're calling a function (we'll learn about functions soon) (here the \"type\" function), \n", - "# we're also evaluating an expression\n", - "\n", - "type(3.14)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "OlhlFm06JFmO", - "slideshow": { - "slide_type": "fragment" - } - }, - "source": [ - "**Definition:** An expression is a combination of values, variables, operators, and calls to functions. Expressions need to be evaluated and result in a value (could be number, a string, an object, a list, etc.) \n" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "slideshow": { - "slide_type": "subslide" - } - }, - "source": [ - "# Challenge 1" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": { - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [], - "source": [ - "# Write an expression that uses x and y and evaluates to 3\n", - "x = 1\n", - "y = 2" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "slideshow": { - "slide_type": "slide" - } - }, - "source": [ - "# Statements" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 614, - "status": "ok", - "timestamp": 1607391462358, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "MhVakYkCJ0wv", - "outputId": "75f12273-51e2-4c97-91be-b93aa1d4c604", - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "boo\n" - ] - } - ], - "source": [ - "# Statements are instructions to the interpreter\n", - "\n", - "x = 1 # This is a statement, you're telling Python, make x refer to the value 1\n", - "\n", - "if x > 0: # Conditionals (like if (and others we'll see soon, \n", - " # are also \"statements\"))\n", - " print(\"boo\")" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "Fmbh2g2FKdNd", - "slideshow": { - "slide_type": "fragment" - } - }, - "source": [ - "* A statement is everything that can make up a line of Python code.\n", - "\n", - "* A statement does something.\n", - "\n", - "Note, therefore, that expressions are statements as well.\n", - "\n", - "* All expressions are statements.\n", - "* Not all statements are expressions.\n", - "\n", - "\n", - "Note: you may see slightly different definitions in textbooks, but I like these" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "mNIC7nQkOxEP", - "slideshow": { - "slide_type": "slide" - } - }, - "source": [ - "# Operators" - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 395, - "status": "ok", - "timestamp": 1607391472811, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "o-rCwk1oOwM1", - "outputId": "755ba91e-de2a-4182-ce62-687d269f404b", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "17" - ] - }, - "execution_count": 13, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "# Operators perform basic operations on objects\n", - "\n", - "# Let's start with arithmetic operators\n", - "\n", - "x = 12\n", - "y = 5\n", - "\n", - "x + y # + is the addition operator, duh" - ] - }, - { - "cell_type": "code", - "execution_count": 14, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 386, - "status": "ok", - "timestamp": 1607391476046, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "RTsa3LB9PzAK", - "outputId": "2cb111a5-d282-41f3-b67f-69058e3f23b5", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "60" - ] - }, - "execution_count": 14, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "x * y # multiplication" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 372, - "status": "ok", - "timestamp": 1607391477584, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "vZaiIhnOP4UW", - "outputId": "aaf74190-452c-44ac-8faa-70270bac0384", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "1" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "x ** y # exponentiation " - ] - }, - { - "cell_type": "markdown", - "metadata": { - "slideshow": { - "slide_type": "fragment" - } - }, - "source": [ - "Note: The ** operator may be new to you, so try and remember it and difference with the * operator." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 383, - "status": "ok", - "timestamp": 1607391479027, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "tpccI3SiP7Ol", - "outputId": "9214331e-4f89-4374-cf52-b994b7c0b2c2", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "3.5" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "x = 7\n", - "y = 2\n", - "\n", - "x / y # What's the value of this one ?" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 389, - "status": "ok", - "timestamp": 1607391480313, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "BFUrcwMXQEL3", - "outputId": "3bd6b58a-c8dc-435d-9a27-ad1f03930f60", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "3" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "x // y # And this one?" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "pycharm": { - "name": "#%% md\n" - }, - "slideshow": { - "slide_type": "fragment" - } - }, - "source": [ - "Note: learn and remember the difference between the integer division (//) and division (/) operators!" - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 378, - "status": "ok", - "timestamp": 1607391482032, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "tHknzzHjp-wF", - "outputId": "2e373b3c-1bfb-4825-d4c7-4a02db28efc8", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "-12" - ] - }, - "execution_count": 18, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "-x # Yup, we have negation" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "pTCYcOf4qDrm", - "slideshow": { - "slide_type": "fragment" - } - }, - "source": [ - "Note: \n", - " * Negation is an example of a unary operator (it takes one operand)\n", - "\n", - " * The other operators are binary (they take two operands)" - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 364, - "status": "ok", - "timestamp": 1607391485528, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "wKJ17ugFsXo1", - "outputId": "359750e3-3454-4c56-f20d-40a0c665c67c", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "1" - ] - }, - "execution_count": 19, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "5 % 2 # The modulus operator, it returns the \"remainder\"" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 335, - "status": "ok", - "timestamp": 1607391487065, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "-d4YQzH1slYv", - "outputId": "2047f716-b6da-4148-b2c9-308ad5297796", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "0" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# It's a good way to determine if a number is divisible by another,\n", - "# because if it is, the expression value will be 0\n", - "4 % 2" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "slideshow": { - "slide_type": "subslide" - } - }, - "source": [ - "# Challenge 2" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [], - "source": [ - "x = 5\n", - "y = 2\n", - "# Write a statement that divides x by y, forgetting the remainder, storing the result in a variable z" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "-_noHbEBglV5", - "slideshow": { - "slide_type": "slide" - } - }, - "source": [ - "# Operator Overloading" - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 367, - "status": "ok", - "timestamp": 1607391491626, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "VM61C-mG8z10", - "outputId": "eb800a43-f593-4dd0-b149-c9c40f311a1c", - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "27" - ] - }, - "execution_count": 21, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "5 + 10 + 12 # We can use \"+\" to add together strings of numbers" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 35 - }, - "executionInfo": { - "elapsed": 344, - "status": "ok", - "timestamp": 1607391522629, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "zsIwvpbFRzvf", - "outputId": "2bc48222-e07d-4f25-ef67-6e3be2cd7aab", - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'Thisiscontrived'" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# Some arithmetic operators are also \"overloaded\" to work with strings\n", - "# (This is an example of \"polymorphism\", that we'll meet again much later)\n", - "\n", - "\"This\" + \"is\" + \"contrived\" # The '+' concatenates strings" - ] - }, - { - "cell_type": "code", - "execution_count": 25, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 198 - }, - "executionInfo": { - "elapsed": 319, - "status": "error", - "timestamp": 1607391525883, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "cggT1iCJN7AI", - "outputId": "57c8840f-f089-4328-e414-5a97914ce860", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "ename": "TypeError", - "evalue": "ignored", - "output_type": "error", - "traceback": [ - "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m", - "\u001B[0;31mTypeError\u001B[0m Traceback (most recent call last)", - "\u001B[0;32m\u001B[0m in \u001B[0;36m\u001B[0;34m()\u001B[0m\n\u001B[1;32m 2\u001B[0m \u001B[0;31m# a number together\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[1;32m 3\u001B[0m \u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0;32m----> 4\u001B[0;31m \u001B[0;34m\"this\"\u001B[0m \u001B[0;34m+\u001B[0m \u001B[0;36m5\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0m", - "\u001B[0;31mTypeError\u001B[0m: must be str, not int" - ] - } - ], - "source": [ - "# Note, this doesn't work because Python doesn't know how to add a string and\n", - "# a number together\n", - "\n", - "\"this\" + 5" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "9XAjll9RUokG", - "slideshow": { - "slide_type": "slide" - } - }, - "source": [ - "# Abbreviated Assignment\n", - "\n", - "You will see this a lot:" - ] - }, - { - "cell_type": "code", - "execution_count": 26, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 343, - "status": "ok", - "timestamp": 1607391531326, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "Ls-IKDqWUu0u", - "outputId": "ccf895e0-81d1-4501-c0a6-a08dddb0dbe2", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "11" - ] - }, - "execution_count": 26, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "x = 1\n", - "\n", - "# Instead of writing\n", - "\n", - "x = x + 5\n", - "\n", - "# you can use the shorthand:\n", - "\n", - "x += 5 # This means \"add 5 to value x refers to\"\n", - "\n", - "x" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "1zKtAWt7Uvzo", - "slideshow": { - "slide_type": "subslide" - } - }, - "source": [ - "This works for all the obvious math operators:" - ] - }, - { - "cell_type": "code", - "execution_count": 27, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 370, - "status": "ok", - "timestamp": 1607391536440, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "DWhES0_nU0xS", - "outputId": "93b32e09-3dff-49b0-ed65-0121ead40033", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "22\n" - ] - } - ], - "source": [ - "\n", - "\n", - "x *= 2 # multiply x by 2, aka x = x * 2\n", - "\n", - "print(x)" - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 357, - "status": "ok", - "timestamp": 1607391540567, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "536MyapkU21H", - "outputId": "d9cf7d10-91f6-412e-f8ab-355a8488bdb4", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "19\n" - ] - } - ], - "source": [ - "x -= 3 # subtract three, aka x = x - 3\n", - "\n", - "print(x)" - ] - }, - { - "cell_type": "code", - "execution_count": 29, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 372, - "status": "ok", - "timestamp": 1607391542854, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "GonNznhrU6E1", - "outputId": "092d1fb9-e1c9-4786-d001-36518e69dd74", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "9.5\n" - ] - } - ], - "source": [ - "x /= 2 # divide by 2 \n", - "\n", - "print(x)" - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 358, - "status": "ok", - "timestamp": 1607391544853, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "Sm7tItanU8WA", - "outputId": "c5bbbc82-a61d-4cf8-cc55-cc1161489f4a", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "2\n" - ] - } - ], - "source": [ - "x //= 2 # divide by 2 and forget fraction\n", - "\n", - "print(x)" - ] - }, - { - "cell_type": "code", - "execution_count": 31, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 387, - "status": "ok", - "timestamp": 1607391549191, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "WRp0r1tAU_Kg", - "outputId": "877fa5c8-c703-4696-a147-da34228da6f3", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "0.0\n" - ] - } - ], - "source": [ - "x %= 2 # Take x mod 2 and store the result in x, i.e. x = x % 2\n", - "\n", - "print(x)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "slideshow": { - "slide_type": "subslide" - } - }, - "source": [ - "# Challenge 3" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [], - "source": [ - "x = 7 \n", - "y = 2\n", - "# Use abbreviated assignment to subtract y from x, storing the result as x" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "6oYe0AgQl7kl", - "slideshow": { - "slide_type": "slide" - } - }, - "source": [ - "# Boolean Type\n", - "\n", - "Python has a special boolean (binary) type, which can either be 'True' or 'False'. \n", - "\n", - "This is essential for logical expressions.\n" - ] - }, - { - "cell_type": "code", - "execution_count": 32, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 342, - "status": "ok", - "timestamp": 1607391563443, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "12mmMhhGmEmz", - "outputId": "b52afd9e-f599-4cd7-e635-58f70bff9a18", - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "bool" - ] - }, - "execution_count": 32, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "type(True)" - ] - }, - { - "cell_type": "code", - "execution_count": 33, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 387, - "status": "ok", - "timestamp": 1607391564807, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "IN_9Y3UdmPVi", - "outputId": "fe44676e-cbf3-4151-b441-2bf6bb4447a3", - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "bool" - ] - }, - "execution_count": 33, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "type(False)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "SjGU71_1QWqx", - "slideshow": { - "slide_type": "slide" - } - }, - "source": [ - "# Logical Operators\n", - "\n", - " Booleans are used for making decisions in your program.\n", - " \n", - " To do this we use logical operators, which do, err, logic and evaluate to booleans." - ] - }, - { - "cell_type": "code", - "execution_count": 34, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 601, - "status": "ok", - "timestamp": 1607391567269, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "bKqHlRGZ6Vyz", - "outputId": "989aeb11-c58f-4f9a-b43c-9ca6f21ebe34", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "False" - ] - }, - "execution_count": 34, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "x = 5\n", - "y = 10\n", - "\n", - "x > y # The greater than operator compares two things" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 361, - "status": "ok", - "timestamp": 1607391568662, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "WowJC26dmoPX", - "outputId": "fd4ea3be-d279-45e5-b6e2-5aeb14e4fb6f", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "bool" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# To see the relationship with Booleans\n", - "x = 5\n", - "y = 10\n", - "\n", - "\n", - "type(x > y)" - ] - }, - { - "cell_type": "code", - "execution_count": 36, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 385, - "status": "ok", - "timestamp": 1607391570353, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "SJcNUseq6VzG", - "outputId": "e12c144f-3997-416b-9cd5-66c3b31a5a02", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "False" - ] - }, - "execution_count": 36, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "# There are a bunch of these\n", - "\n", - "x >= y # Is x greater than or equal to y?" - ] - }, - { - "cell_type": "code", - "execution_count": 37, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 403, - "status": "ok", - "timestamp": 1607391571943, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "hDYP9S1J6VzI", - "outputId": "1c178c9c-9aa6-41a2-eb98-de7169e6cba9", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 37, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "x < y # Is x less than y?" - ] - }, - { - "cell_type": "code", - "execution_count": 38, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 343, - "status": "ok", - "timestamp": 1607391579454, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "i_JdqLQi6VzK", - "outputId": "9d69d1b9-25c8-4bd3-b678-436670d83b96", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 38, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "x <= y # Less than or equals?" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 321, - "status": "ok", - "timestamp": 1607391580841, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "fXbNwOfd6VzM", - "outputId": "878ea4b5-c993-4510-ce61-bb94482ba231", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "False" - ] - }, - "execution_count": 8, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# What about this one?\n", - "\n", - "x == y" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "oEu8nmKg6VzR", - "slideshow": { - "slide_type": "fragment" - } - }, - "source": [ - "* As we discussed earlier, in Python (and many languages) '=' is the assignment operator\n", - "* Logical equals is '=='. Some people find this weird, but you get over it.\n" - ] - }, - { - "cell_type": "code", - "execution_count": 40, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 336, - "status": "ok", - "timestamp": 1607391583102, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "hkZW2hBN_F9k", - "outputId": "b95dc957-8a08-4321-fc66-b2e182f523db", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 40, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "# Python also has the not logical equals operator !=\n", - "\n", - "x != y # Read this as \"does x not equal y?\"" - ] - }, - { - "cell_type": "code", - "execution_count": 41, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 344, - "status": "ok", - "timestamp": 1607391585169, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "IGoHXsjM6VzR", - "outputId": "49896c73-c773-4e13-b826-19ff54a18be8", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 41, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "# We can compose logical statements into complex expressions \n", - "# using logical 'and' and 'or'\n", - "\n", - "x = 5\n", - "y = 10\n", - "z = 7\n", - "\n", - "x >= y or z > x # Says: True if x is greater than or equal to y or z is greater than x" - ] - }, - { - "cell_type": "code", - "execution_count": 42, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 366, - "status": "ok", - "timestamp": 1607391587116, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "BI0J-9Bt6VzV", - "outputId": "17a0145a-4299-4342-fe62-f20df8342b62", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 42, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "# Similarly\n", - "\n", - "y > x and y > z # Says: True if y is greater than x AND y is greater than z" - ] - }, - { - "cell_type": "code", - "execution_count": 43, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 344, - "status": "ok", - "timestamp": 1607391588715, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "fYlNC0fz6VzY", - "outputId": "abf04d8b-c83b-4ee8-ee70-ec4fc25269ed", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "False" - ] - }, - "execution_count": 43, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "# There is also the unary negation operator: not\n", - "\n", - "not True" - ] - }, - { - "cell_type": "code", - "execution_count": 44, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 345, - "status": "ok", - "timestamp": 1607391590335, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "ffSYgAtK6Vze", - "outputId": "cdf7bcbc-8c10-4244-b435-c49b1bbc592e", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 44, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "# Use it to switch True to False and vice versa:\n", - "\n", - "y = 0\n", - "x = 1\n", - "\n", - "not y > x" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "cNDSDbL86Vzh", - "slideshow": { - "slide_type": "subslide" - } - }, - "source": [ - "Logical comparisons also work with strings" - ] - }, - { - "cell_type": "code", - "execution_count": 45, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 338, - "status": "ok", - "timestamp": 1607391592411, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "NEN92T0s6Vzk", - "outputId": "9442c91d-f428-463a-fea0-0343ce7b5acd", - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "False" - ] - }, - "execution_count": 45, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "# String comparison\n", - "\n", - "\"cats\" > \"dogs\" # ?" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "slideshow": { - "slide_type": "subslide" - } - }, - "source": [ - "# Challenge 4" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [], - "source": [ - "x = int(input(\"Enter a number\"))\n", - "y = int(input(\"Enter a second number\"))\n", - "# Write a logical expression that is True if and only if \n", - "# x is greater than y or x is divisible by y" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "slideshow": { - "slide_type": "slide" - } - }, - "source": [ - "# Truthy and Falsy Values\n", - "\n", - "Values can evaluate as either True or False in logical expressions despite not being of a boolean type" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": { - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "False" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "bool(\"\") # The empty string is false" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": { - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "bool(\"hello\") # A non-empty string is true" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "slideshow": { - "slide_type": "fragment" - } - }, - "source": [ - "We'll see more examples of how different types map to True and False later (but see the note for a summary, looking ahead)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "slideshow": { - "slide_type": "notes" - } - }, - "source": [ - "This becomes increasingly helpful as we start to deal with more complex data structures. \n", - "\n", - "#### The values that will evaluate as false are:\n", - "- Empty lists []\n", - "- Empty tuples ()\n", - "- Empty dictionaries {}\n", - "- Empty sets set()\n", - "- Empty strings \"\"\n", - "- Empty ranges range(0)\n", - "- And any numeric type that is 0; Int, Float, Complex\n", - "\n", - "In contrast the values that evaluate as true are:\n", - "- Non-Empty strings \"hello\"\n", - "- Non-Empty tuples (\"apple\", \"banana\")\n", - "- Non-Empty dictionaries { id: 10, name: \"Aiden\"}\n", - "- No\n", - "- Any Non zero numeric type" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "CLy5-zuqmV6k", - "slideshow": { - "slide_type": "slide" - } - }, - "source": [ - "# Order of operations\n", - "\n", - "This is a boring topic, but if you don't understand it, you'll write lots of bugs" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "slideshow": { - "slide_type": "fragment" - } - }, - "source": [ - "* Just like math, Python follows PEMDAS - or PE(MD)(AS). \n", - " * That is: Parentheses, Exponentiation, Multiplication and Division (left to right), and Addition and Subtraction(left to right)\n", - "* After the math operators, in order of presidence, are the relational operators (e.g. greater than, less than, etc.), and lastly the logical operators “not”, “and”, & “or” in that order." - ] - }, - { - "cell_type": "code", - "execution_count": 47, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 342, - "status": "ok", - "timestamp": 1607391600951, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "g0g2n8E7mkSf", - "outputId": "df09bf20-fa8c-4ed9-e670-97503a94867d", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "8" - ] - }, - "execution_count": 47, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "# e.g.\n", - "\n", - "x = 2\n", - "y = 3\n", - "\n", - "x * y + x # Yup, this works just like math" - ] - }, - { - "cell_type": "code", - "execution_count": 48, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 366, - "status": "ok", - "timestamp": 1607391602661, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "kcKJf8C0nGIQ", - "outputId": "d4675860-1131-433d-c9dc-78a447ca072b", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "10" - ] - }, - "execution_count": 48, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "x * (y + x) # If you want to force the addition \n", - "# before the multiplication you can use brackets, like in math\n", - "\n", - "# Technically, the brackets have highest precedence of any operator" - ] - }, - { - "cell_type": "code", - "execution_count": 49, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 350, - "status": "ok", - "timestamp": 1607391604199, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "Y7Zjv-vPoEjQ", - "outputId": "5780e325-2091-4cd1-8a89-00f26eccda95", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "8" - ] - }, - "execution_count": 49, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "# Exponents have higher precedence than division/multiplication\n", - "\n", - "2 * 2**2 # The exponent happens first" - ] - }, - { - "cell_type": "code", - "execution_count": 50, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 372, - "status": "ok", - "timestamp": 1607391605731, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "QO7dKEBPnsoH", - "outputId": "b5fe6c37-fe98-46f4-ba9c-f7c117422efc", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 50, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "# Arithmetic operators have precidence over logical operators\n", - "\n", - "x * y > x + y # This could also be written (x * y) > (x + y)" - ] - }, - { - "cell_type": "code", - "execution_count": 51, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 499, - "status": "ok", - "timestamp": 1607391607443, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "AZ32yKDSsARP", - "outputId": "320ef43f-b98f-4d1f-8f1d-c694a9e08aa1", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "False" - ] - }, - "execution_count": 51, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "# Sometimes it is helpful to use brackets, because it's hard to remember\n", - "# the order of operations, consider this..\n", - "\n", - "not 3 * 5 > 10 and 2 > 1" - ] - }, - { - "cell_type": "code", - "execution_count": 52, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 541, - "status": "ok", - "timestamp": 1607391609440, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "1hIvAe9PtOFQ", - "outputId": "d0f2cc99-323d-4411-eaea-f3f107cd5bb2", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "False" - ] - }, - "execution_count": 52, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "# Which I think is much clearer as..\n", - "\n", - "(not 3 * 5 > 10) and (2 > 1) # The brackets are just there for clarity" - ] - }, - { - "cell_type": "code", - "execution_count": 53, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 363, - "status": "ok", - "timestamp": 1607391611076, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "HDtWTEzotec7", - "outputId": "bcb4c84f-1088-46d7-f8c3-0eb85f438f36", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 53, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "# Or maybe even.. \n", - "\n", - "(not (3 * 5 > 10)) or (2 > 1)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [], - "source": [ - "# Suggested exercise: play with >, <, >=, <=, not, and, or, () \n", - "# and see if they do what you expect\n", - "\n", - "## Also definitely read text book on this for more thorough treatment" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "slideshow": { - "slide_type": "subslide" - } - }, - "source": [ - "# Simplifying Complex Expressions" - ] - }, - { - "cell_type": "code", - "execution_count": 38, - "metadata": { - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 38, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# Sometimes you're faced with a complex expression like:\n", - "\n", - "x = \"cats\"\n", - "y = \"dogs\"\n", - "\n", - "not (x > y or not x < y) or x == y # This is not very easy to parse!\n", - "\n", - "# To understand it try breaking it down into pieces by substituting the sub-expressions for True or False\n", - "# step by step, applying the presidence rules\n", - "\n", - "not (False or not True) or False # Here I substituted sub-expressions with True or False\n", - "\n", - "not (False or False) or False # More substitution\n", - "\n", - "not False or False # And again\n", - "\n", - "True or False # Nearly there\n", - "\n", - "True ## Done\n", - " \n", - "# Obviously, the compute has no problem evaluating this...\n", - "\n", - "not (x > y or not x < y) or x == y # Yup, it's True" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "slideshow": { - "slide_type": "fragment" - } - }, - "source": [ - "Suggested exercise: try making up a complex expression and then simplify it by hand. You can use the Python interpretor to check that your result is correct." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "slideshow": { - "slide_type": "subslide" - } - }, - "source": [ - "# Challenge 5" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [], - "source": [ - "# Change the expression below so that the 'not' is applied after the 'or' \n", - "(not (3 * 5 > 10)) or (2 > 1)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "slideshow": { - "slide_type": "subslide" - } - }, - "source": [ - "# Challenge 6" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [], - "source": [ - "# Remove the parentheses in the expression and see what would it print \n", - "x = 3 \n", - "y = 2 \n", - "print((not x > y and not x < y) or x//y == 2)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "ZwejXNuJaC-g", - "slideshow": { - "slide_type": "slide" - } - }, - "source": [ - "# Reading\n", - "\n", - "Openbook:\n", - "\n", - "* Read Chapter 2 on expressions, variables, statements and operators:\n", - " * http://openbookproject.net/thinkcs/python/english3e/variables_expressions_statements.html\n", - "\n", - " \n", - "# Homework\n", - "\n", - "* Go to Canvas and complete the second lecture quiz, which involves completing each challenge problem\n", - "* See \"Reading 2\" in Zybooks" - ] - } - ], - "metadata": { - "celltoolbar": "Slideshow", - "colab": { - "collapsed_sections": [ - "cjsgMup6ktUd" - ], - "name": "L02 Variables and Expressions.ipynb", - "provenance": [ - { - "file_id": "17GPUwfjMAW-SkqDI2qb9LangNzYzgNH_", - "timestamp": 1547074807405 - } - ] - }, - "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.15" - } - }, - "nbformat": 4, - "nbformat_minor": 1 -} \ No newline at end of file From 408b43b1a5a3fb18754febebc540b615acd9a05e Mon Sep 17 00:00:00 2001 From: crimpmug <113626525+crimpmug@users.noreply.github.com> Date: Wed, 18 Jan 2023 15:10:48 -0800 Subject: [PATCH 3/7] Delete L02 Variables and Expressions mine.ipynb --- .../L02 Variables and Expressions mine.ipynb | 2811 ----------------- 1 file changed, 2811 deletions(-) delete mode 100644 lecture_notebooks/L02 Variables and Expressions mine.ipynb diff --git a/lecture_notebooks/L02 Variables and Expressions mine.ipynb b/lecture_notebooks/L02 Variables and Expressions mine.ipynb deleted file mode 100644 index 0754515..0000000 --- a/lecture_notebooks/L02 Variables and Expressions mine.ipynb +++ /dev/null @@ -1,2811 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "id": "cjsgMup6ktUd", - "slideshow": { - "slide_type": "slide" - } - }, - "source": [ - "# Lecture 2 - Variables and Expressions (https://bit.ly/intro_python_02)\n", - "\n", - "Today:\n", - "* Variables\n", - "* Assignment is not equals\n", - "* Copying References\n", - "* Expressions\n", - "* Statements\n", - "* Variable Names\n", - "* Operators\n", - "* Abbreviated assignment\n", - "* Logical operators\n", - "* Order of operations\n" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "OkA6aiONlE7b", - "slideshow": { - "slide_type": "slide" - } - }, - "source": [ - "# Variables" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": { - "executionInfo": { - "elapsed": 363, - "status": "ok", - "timestamp": 1607391378206, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "7vBJtWFslI-G", - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [], - "source": [ - "# As in algebra, we use variables to 'refer' to things\n", - "\n", - "x = 1 # x is a variable referring to the int 1\n", - "\n", - "y = \"foo\" # y is a variable referring to the string \"foo\"" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "S0j3Xtd1DhKp", - "slideshow": { - "slide_type": "fragment" - } - }, - "source": [ - "Think of x as \"referring to 1\". That is x is really a reference to the object representing 1. \n", - " * x -----> 1\n", - " * y ------> \"foo\"\n", - "\n", - "Under the hood, in general, there is a piece of memory (bits) representing x \n", - "that stores the location of where the value 1 is actually stored in memory. \n", - "\n", - "Sometimes, in some languages, x or y stores the content directly, e.g. x is actually \n", - "memory representing 1. However in Python variables are references." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "s8eUIhrM9xjr", - "slideshow": { - "slide_type": "slide" - } - }, - "source": [ - "# Assignment is not equals" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 349, - "status": "ok", - "timestamp": 1607391380918, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "1ADmdsvtEpAw", - "outputId": "9ade660c-a1b3-47e7-d1df-cbc2143b5d70", - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "foo\n" - ] - } - ], - "source": [ - "# The consequence of this \"refers to\" notion is that \n", - "# equals sign in Python means \"assignment\" not \"equals\"\n", - "\n", - "x = 1 # x refers to 1\n", - "\n", - "# Because = means assignment, we can update assignments, without it\n", - "# being meaningless in a math sense\n", - "\n", - "x = \"foo\" # x refers to \"foo\"\n", - "\n", - "#What's the value of x?\n", - "print(x)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "toK-UKQ198jr", - "slideshow": { - "slide_type": "slide" - } - }, - "source": [ - "# Copying references" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 373, - "status": "ok", - "timestamp": 1607391393919, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "FndrQqcuFho6", - "outputId": "3f7bfc04-874e-4ad0-e328-ef219333c8c5", - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "5\n" - ] - } - ], - "source": [ - "# We can also copy variables\n", - "\n", - "x = 5\n", - "\n", - "y = x # Now x and y refer to the same thing (5)\n", - "\n", - "print(y)" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 411, - "status": "ok", - "timestamp": 1607391407291, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "4ous2ZHC6Z1h", - "outputId": "7b6a6f09-34a4-42b3-a068-5232da47c35d", - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "10\n", - "5\n" - ] - } - ], - "source": [ - "# But note, reassignment and copying can be co-mingled:\n", - "\n", - "x = 5\n", - "\n", - "y = x # Now x and y refer to the same thing (5)\n", - "\n", - "x = 10 # But now we change what x refers to, this doesn't change y!\n", - "\n", - "print(x) \n", - "print(y)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "sQLo2H1qGadW", - "slideshow": { - "slide_type": "slide" - } - }, - "source": [ - "# Variable Names\n", - "\n", - "Variables don't have to be single letters (in fact, that's a terrible idea), but can be **any combination of alpha-numeric characters and the '_' symbol, providing they don't start with a number and aren't already a reserved keyword**. Remember this!" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 424, - "status": "ok", - "timestamp": 1607391414262, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "p92pX5KtHHHk", - "outputId": "3f342de4-5e6a-4e09-ffca-46fd608459dd", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "4\n" - ] - } - ], - "source": [ - "# Variable names\n", - "\n", - "foo = 1 # Legit variable name\n", - "\n", - "bar2 = 2 # Also legit\n", - "\n", - "_boo = 3 # Legit\n", - "\n", - "super_helpful_variable_name = 4 # Legit\n", - "\n", - "somePeopleLikeCamelCase = 5 # Fine variable name\n", - "\n", - "print(super_helpful_variable_name)" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 130 - }, - "executionInfo": { - "elapsed": 381, - "status": "error", - "timestamp": 1607391419362, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "wkwn4Z1PHcjy", - "outputId": "03ea9788-5615-4905-91de-02ae1765e4b7", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "ename": "SyntaxError", - "evalue": "ignored", - "output_type": "error", - "traceback": [ - "\u001B[0;36m File \u001B[0;32m\"\"\u001B[0;36m, line \u001B[0;32m3\u001B[0m\n\u001B[0;31m 3l33t = 4 # Not okay\u001B[0m\n\u001B[0m ^\u001B[0m\n\u001B[0;31mSyntaxError\u001B[0m\u001B[0;31m:\u001B[0m invalid syntax\n" - ] - } - ], - "source": [ - "# Not okay\n", - "\n", - "3l33t = 4 # Not okay, starts with a number\n", - "\n", - "not^okay = 5 # Not okay, contains an illegal character\n", - "\n", - "and = 6 # Not okay, is a reserved keyword\n", - "\n", - "# Etc." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "U9VGvqwEHQtf", - "slideshow": { - "slide_type": "subslide" - } - }, - "source": [ - "Reserved key words, see Chapter 2 of the open textbook:\n", - "\n", - "http://openbookproject.net/thinkcs/python/english3e/variables_expressions_statements.html" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "bHeo34G5IWAN", - "slideshow": { - "slide_type": "slide" - } - }, - "source": [ - "# Expressions" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 349, - "status": "ok", - "timestamp": 1607391427113, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "dhSAuuO-Ich7", - "outputId": "4207f127-b0be-4ba3-c85e-813ab30a4ae7", - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "1" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# Now we have basic types and variables, we can do stuff with them\n", - "\n", - "x = 1\n", - "\n", - "y = 2\n", - "\n", - "x + y - 2 # This is an expression, the interpreter\n", - "# has to \"evaluate it\" to figure out the value by \n", - "# combining variables (x, y), \n", - "# values (2) and operators (+, -)" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 373, - "status": "ok", - "timestamp": 1607391431652, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "ega2_raeI6uc", - "outputId": "2e98e03f-f8e3-4a04-f134-52f3c027ba67", - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "float" - ] - }, - "execution_count": 10, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "# When we're calling a function (we'll learn about functions soon) (here the \"type\" function), \n", - "# we're also evaluating an expression\n", - "\n", - "type(3.14)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "OlhlFm06JFmO", - "slideshow": { - "slide_type": "fragment" - } - }, - "source": [ - "**Definition:** An expression is a combination of values, variables, operators, and calls to functions. Expressions need to be evaluated and result in a value (could be number, a string, an object, a list, etc.) \n" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "slideshow": { - "slide_type": "subslide" - } - }, - "source": [ - "# Challenge 1" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": { - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [], - "source": [ - "# Write an expression that uses x and y and evaluates to 3\n", - "x = 1\n", - "y = 2" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "slideshow": { - "slide_type": "slide" - } - }, - "source": [ - "# Statements" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 614, - "status": "ok", - "timestamp": 1607391462358, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "MhVakYkCJ0wv", - "outputId": "75f12273-51e2-4c97-91be-b93aa1d4c604", - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "boo\n" - ] - } - ], - "source": [ - "# Statements are instructions to the interpreter\n", - "\n", - "x = 1 # This is a statement, you're telling Python, make x refer to the value 1\n", - "\n", - "if x > 0: # Conditionals (like if (and others we'll see soon, \n", - " # are also \"statements\"))\n", - " print(\"boo\")" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "outputs": [ - { - "ename": "SyntaxError", - "evalue": "invalid syntax (450256079.py, line 5)", - "output_type": "error", - "traceback": [ - "\u001B[0;36m Cell \u001B[0;32mIn[5], line 5\u001B[0;36m\u001B[0m\n\u001B[0;31m print(x-=1)\u001B[0m\n\u001B[0m ^\u001B[0m\n\u001B[0;31mSyntaxError\u001B[0m\u001B[0;31m:\u001B[0m invalid syntax\n" - ] - } - ], - "source": [ - "# However, assignment statements, such as x -= y will not print\n", - "\n", - "# This is because assignment statements don't return a value for python to print\n", - "\n", - "# In this way we can differentiate from expressions\n", - "\n", - "print(x-=1)" - ], - "metadata": { - "collapsed": false - } - }, - { - "cell_type": "markdown", - "metadata": { - "id": "Fmbh2g2FKdNd", - "slideshow": { - "slide_type": "fragment" - } - }, - "source": [ - "* A statement is everything that can make up a line of Python code.\n", - "\n", - "* A statement does something.\n", - "\n", - "Note, therefore, that expressions are statements as well.\n", - "\n", - "* All expressions are statements.\n", - "* Not all statements are expressions.\n", - "\n", - "\n", - "Note: you may see slightly different definitions in textbooks, but I like these" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "mNIC7nQkOxEP", - "slideshow": { - "slide_type": "slide" - } - }, - "source": [ - "# Operators" - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 395, - "status": "ok", - "timestamp": 1607391472811, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "o-rCwk1oOwM1", - "outputId": "755ba91e-de2a-4182-ce62-687d269f404b", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "17" - ] - }, - "execution_count": 13, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "# Operators perform basic operations on objects\n", - "\n", - "# Let's start with arithmetic operators\n", - "\n", - "x = 12\n", - "y = 5\n", - "\n", - "x + y # + is the addition operator, duh" - ] - }, - { - "cell_type": "code", - "execution_count": 14, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 386, - "status": "ok", - "timestamp": 1607391476046, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "RTsa3LB9PzAK", - "outputId": "2cb111a5-d282-41f3-b67f-69058e3f23b5", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "60" - ] - }, - "execution_count": 14, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "x * y # multiplication" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 372, - "status": "ok", - "timestamp": 1607391477584, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "vZaiIhnOP4UW", - "outputId": "aaf74190-452c-44ac-8faa-70270bac0384", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "1" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "x ** y # exponentiation " - ] - }, - { - "cell_type": "markdown", - "metadata": { - "slideshow": { - "slide_type": "fragment" - } - }, - "source": [ - "Note: The ** operator may be new to you, so try and remember it and difference with the * operator." - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 383, - "status": "ok", - "timestamp": 1607391479027, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "tpccI3SiP7Ol", - "outputId": "9214331e-4f89-4374-cf52-b994b7c0b2c2", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "2" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "x / y # What's the value of this one ?" - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 389, - "status": "ok", - "timestamp": 1607391480313, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "BFUrcwMXQEL3", - "outputId": "3bd6b58a-c8dc-435d-9a27-ad1f03930f60", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "2" - ] - }, - "execution_count": 17, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "x // y # And this one?" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "slideshow": { - "slide_type": "fragment" - } - }, - "source": [ - "Note: learn and remember the difference between the integer division (//) and division (/) operators!" - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 378, - "status": "ok", - "timestamp": 1607391482032, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "tHknzzHjp-wF", - "outputId": "2e373b3c-1bfb-4825-d4c7-4a02db28efc8", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "-12" - ] - }, - "execution_count": 18, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "-x # Yup, we have negation" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "pTCYcOf4qDrm", - "slideshow": { - "slide_type": "fragment" - } - }, - "source": [ - "Note: \n", - " * Negation is an example of a unary operator (it takes one operand)\n", - "\n", - " * The other operators are binary (they take two operands)" - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 364, - "status": "ok", - "timestamp": 1607391485528, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "wKJ17ugFsXo1", - "outputId": "359750e3-3454-4c56-f20d-40a0c665c67c", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "1" - ] - }, - "execution_count": 19, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "5 % 2 # The modulus operator, it returns the \"remainder\"" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 335, - "status": "ok", - "timestamp": 1607391487065, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "-d4YQzH1slYv", - "outputId": "2047f716-b6da-4148-b2c9-308ad5297796", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "0" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# It's a good way to determine if a number is divisible by another,\n", - "# because if it is, the expression value will be 0\n", - "4 % 2" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "slideshow": { - "slide_type": "subslide" - } - }, - "source": [ - "# Challenge 2" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [], - "source": [ - "x = 5\n", - "y = 2\n", - "# Write a statement that divides x by y, forgetting the remainder, storing the result in a variable z" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "-_noHbEBglV5", - "slideshow": { - "slide_type": "slide" - } - }, - "source": [ - "# Operator Overloading" - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 367, - "status": "ok", - "timestamp": 1607391491626, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "VM61C-mG8z10", - "outputId": "eb800a43-f593-4dd0-b149-c9c40f311a1c", - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "27" - ] - }, - "execution_count": 21, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "5 + 10 + 12 # We can use \"+\" to add together strings of numbers" - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 35 - }, - "executionInfo": { - "elapsed": 344, - "status": "ok", - "timestamp": 1607391522629, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "zsIwvpbFRzvf", - "outputId": "2bc48222-e07d-4f25-ef67-6e3be2cd7aab", - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "('This', 'is', 'contrived')" - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# Some arithmetic operators are also \"overloaded\" to work with strings\n", - "# (This is an example of \"polymorphism\", that we'll meet again much later)\n", - "\n", - "\"This\" + \"is\" + \"contrived\" # The '+' concatenates strings" - ] - }, - { - "cell_type": "code", - "execution_count": 25, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 198 - }, - "executionInfo": { - "elapsed": 319, - "status": "error", - "timestamp": 1607391525883, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "cggT1iCJN7AI", - "outputId": "57c8840f-f089-4328-e414-5a97914ce860", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "ename": "TypeError", - "evalue": "ignored", - "output_type": "error", - "traceback": [ - "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m", - "\u001B[0;31mTypeError\u001B[0m Traceback (most recent call last)", - "\u001B[0;32m\u001B[0m in \u001B[0;36m\u001B[0;34m()\u001B[0m\n\u001B[1;32m 2\u001B[0m \u001B[0;31m# a number together\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[1;32m 3\u001B[0m \u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0;32m----> 4\u001B[0;31m \u001B[0;34m\"this\"\u001B[0m \u001B[0;34m+\u001B[0m \u001B[0;36m5\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0m", - "\u001B[0;31mTypeError\u001B[0m: must be str, not int" - ] - } - ], - "source": [ - "# Note, this doesn't work because Python doesn't know how to add a string and\n", - "# a number together\n", - "\n", - "\"this\" + 5" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "9XAjll9RUokG", - "slideshow": { - "slide_type": "slide" - } - }, - "source": [ - "# Abbreviated Assignment\n", - "\n", - "You will see this a lot:" - ] - }, - { - "cell_type": "code", - "execution_count": 26, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 343, - "status": "ok", - "timestamp": 1607391531326, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "Ls-IKDqWUu0u", - "outputId": "ccf895e0-81d1-4501-c0a6-a08dddb0dbe2", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "11" - ] - }, - "execution_count": 26, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "x = 1\n", - "\n", - "# Instead of writing\n", - "\n", - "x = x + 5\n", - "\n", - "# you can use the shorthand:\n", - "\n", - "x += 5 # This means \"add 5 to value x refers to\"\n", - "\n", - "x" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "1zKtAWt7Uvzo", - "slideshow": { - "slide_type": "subslide" - } - }, - "source": [ - "This works for all the obvious math operators:" - ] - }, - { - "cell_type": "code", - "execution_count": 27, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 370, - "status": "ok", - "timestamp": 1607391536440, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "DWhES0_nU0xS", - "outputId": "93b32e09-3dff-49b0-ed65-0121ead40033", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "22\n" - ] - } - ], - "source": [ - "\n", - "\n", - "x *= 2 # multiply x by 2, aka x = x * 2\n", - "\n", - "print(x)" - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 357, - "status": "ok", - "timestamp": 1607391540567, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "536MyapkU21H", - "outputId": "d9cf7d10-91f6-412e-f8ab-355a8488bdb4", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "19\n" - ] - } - ], - "source": [ - "x -= 3 # subtract three, aka x = x - 3\n", - "\n", - "print(x)" - ] - }, - { - "cell_type": "code", - "execution_count": 29, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 372, - "status": "ok", - "timestamp": 1607391542854, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "GonNznhrU6E1", - "outputId": "092d1fb9-e1c9-4786-d001-36518e69dd74", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "9.5\n" - ] - } - ], - "source": [ - "x /= 2 # divide by 2 \n", - "\n", - "print(x)" - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 358, - "status": "ok", - "timestamp": 1607391544853, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "Sm7tItanU8WA", - "outputId": "c5bbbc82-a61d-4cf8-cc55-cc1161489f4a", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "2\n" - ] - } - ], - "source": [ - "x //= 2 # divide by 2 and forget fraction\n", - "\n", - "print(x)" - ] - }, - { - "cell_type": "code", - "execution_count": 31, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 387, - "status": "ok", - "timestamp": 1607391549191, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "WRp0r1tAU_Kg", - "outputId": "877fa5c8-c703-4696-a147-da34228da6f3", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "0.0\n" - ] - } - ], - "source": [ - "x %= 2 # Take x mod 2 and store the result in x, i.e. x = x % 2\n", - "\n", - "print(x)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "slideshow": { - "slide_type": "subslide" - } - }, - "source": [ - "# Challenge 3" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [], - "source": [ - "x = 7 \n", - "y = 2\n", - "# Use abbreviated assignment to subtract y from x, storing the result as x" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "6oYe0AgQl7kl", - "slideshow": { - "slide_type": "slide" - } - }, - "source": [ - "# Boolean Type\n", - "\n", - "Python has a special boolean (binary) type, which can either be 'True' or 'False'. \n", - "\n", - "This is essential for logical expressions.\n" - ] - }, - { - "cell_type": "code", - "execution_count": 32, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 342, - "status": "ok", - "timestamp": 1607391563443, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "12mmMhhGmEmz", - "outputId": "b52afd9e-f599-4cd7-e635-58f70bff9a18", - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "bool" - ] - }, - "execution_count": 32, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "type(True)" - ] - }, - { - "cell_type": "code", - "execution_count": 33, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 387, - "status": "ok", - "timestamp": 1607391564807, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "IN_9Y3UdmPVi", - "outputId": "fe44676e-cbf3-4151-b441-2bf6bb4447a3", - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "bool" - ] - }, - "execution_count": 33, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "type(False)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "SjGU71_1QWqx", - "slideshow": { - "slide_type": "slide" - } - }, - "source": [ - "# Logical Operators\n", - "\n", - " Booleans are used for making decisions in your program.\n", - " \n", - " To do this we use logical operators, which do, err, logic and evaluate to booleans." - ] - }, - { - "cell_type": "code", - "execution_count": 34, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 601, - "status": "ok", - "timestamp": 1607391567269, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "bKqHlRGZ6Vyz", - "outputId": "989aeb11-c58f-4f9a-b43c-9ca6f21ebe34", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "False" - ] - }, - "execution_count": 34, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "x = 5\n", - "y = 10\n", - "\n", - "x > y # The greater than operator compares two things" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 361, - "status": "ok", - "timestamp": 1607391568662, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "WowJC26dmoPX", - "outputId": "fd4ea3be-d279-45e5-b6e2-5aeb14e4fb6f", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "bool" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# To see the relationship with Booleans\n", - "x = 5\n", - "y = 10\n", - "\n", - "\n", - "type(x > y)" - ] - }, - { - "cell_type": "code", - "execution_count": 36, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 385, - "status": "ok", - "timestamp": 1607391570353, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "SJcNUseq6VzG", - "outputId": "e12c144f-3997-416b-9cd5-66c3b31a5a02", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "False" - ] - }, - "execution_count": 36, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "# There are a bunch of these\n", - "\n", - "x >= y # Is x greater than or equal to y?" - ] - }, - { - "cell_type": "code", - "execution_count": 37, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 403, - "status": "ok", - "timestamp": 1607391571943, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "hDYP9S1J6VzI", - "outputId": "1c178c9c-9aa6-41a2-eb98-de7169e6cba9", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 37, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "x < y # Is x less than y?" - ] - }, - { - "cell_type": "code", - "execution_count": 38, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 343, - "status": "ok", - "timestamp": 1607391579454, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "i_JdqLQi6VzK", - "outputId": "9d69d1b9-25c8-4bd3-b678-436670d83b96", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 38, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "x <= y # Less than or equals?" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 321, - "status": "ok", - "timestamp": 1607391580841, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "fXbNwOfd6VzM", - "outputId": "878ea4b5-c993-4510-ce61-bb94482ba231", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "False" - ] - }, - "execution_count": 8, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# What about this one?\n", - "\n", - "x == y" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "oEu8nmKg6VzR", - "slideshow": { - "slide_type": "fragment" - } - }, - "source": [ - "* As we discussed earlier, in Python (and many languages) '=' is the assignment operator\n", - "* Logical equals is '=='. Some people find this weird, but you get over it.\n" - ] - }, - { - "cell_type": "code", - "execution_count": 40, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 336, - "status": "ok", - "timestamp": 1607391583102, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "hkZW2hBN_F9k", - "outputId": "b95dc957-8a08-4321-fc66-b2e182f523db", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 40, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "# Python also has the not logical equals operator !=\n", - "\n", - "x != y # Read this as \"does x not equal y?\"" - ] - }, - { - "cell_type": "code", - "execution_count": 41, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 344, - "status": "ok", - "timestamp": 1607391585169, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "IGoHXsjM6VzR", - "outputId": "49896c73-c773-4e13-b826-19ff54a18be8", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 41, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "# We can compose logical statements into complex expressions \n", - "# using logical 'and' and 'or'\n", - "\n", - "x = 5\n", - "y = 10\n", - "z = 7\n", - "\n", - "x >= y or z > x # Says: True if x is greater than or equal to y or z is greater than x" - ] - }, - { - "cell_type": "code", - "execution_count": 42, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 366, - "status": "ok", - "timestamp": 1607391587116, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "BI0J-9Bt6VzV", - "outputId": "17a0145a-4299-4342-fe62-f20df8342b62", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 42, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "# Similarly\n", - "\n", - "y > x and y > z # Says: True if y is greater than x AND y is greater than z" - ] - }, - { - "cell_type": "code", - "execution_count": 43, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 344, - "status": "ok", - "timestamp": 1607391588715, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "fYlNC0fz6VzY", - "outputId": "abf04d8b-c83b-4ee8-ee70-ec4fc25269ed", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "False" - ] - }, - "execution_count": 43, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "# There is also the unary negation operator: not\n", - "\n", - "not True" - ] - }, - { - "cell_type": "code", - "execution_count": 44, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 345, - "status": "ok", - "timestamp": 1607391590335, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "ffSYgAtK6Vze", - "outputId": "cdf7bcbc-8c10-4244-b435-c49b1bbc592e", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 44, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "# Use it to switch True to False and vice versa:\n", - "\n", - "y = 0\n", - "x = 1\n", - "\n", - "not y > x" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "cNDSDbL86Vzh", - "slideshow": { - "slide_type": "subslide" - } - }, - "source": [ - "Logical comparisons also work with strings" - ] - }, - { - "cell_type": "code", - "execution_count": 45, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 338, - "status": "ok", - "timestamp": 1607391592411, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "NEN92T0s6Vzk", - "outputId": "9442c91d-f428-463a-fea0-0343ce7b5acd", - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "False" - ] - }, - "execution_count": 45, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "# String comparison\n", - "\n", - "\"cats\" > \"dogs\" # ?" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "slideshow": { - "slide_type": "subslide" - } - }, - "source": [ - "# Challenge 4" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [], - "source": [ - "x = int(input(\"Enter a number\"))\n", - "y = int(input(\"Enter a second number\"))\n", - "# Write a logical expression that is True if and only if \n", - "# x is greater than y or x is divisible by y" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "slideshow": { - "slide_type": "slide" - } - }, - "source": [ - "# Truthy and Falsy Values\n", - "\n", - "Values can evaluate as either True or False in logical expressions despite not being of a boolean type" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": { - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "False" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "bool(\"\") # The empty string is false" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": { - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "bool(\"hello\") # A non-empty string is true" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "slideshow": { - "slide_type": "fragment" - } - }, - "source": [ - "We'll see more examples of how different types map to True and False later (but see the note for a summary, looking ahead)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "slideshow": { - "slide_type": "notes" - } - }, - "source": [ - "This becomes increasingly helpful as we start to deal with more complex data structures. \n", - "\n", - "#### The values that will evaluate as false are:\n", - "- Empty lists []\n", - "- Empty tuples ()\n", - "- Empty dictionaries {}\n", - "- Empty sets set()\n", - "- Empty strings \"\"\n", - "- Empty ranges range(0)\n", - "- And any numeric type that is 0; Int, Float, Complex\n", - "\n", - "In contrast the values that evaluate as true are:\n", - "- Non-Empty strings \"hello\"\n", - "- Non-Empty tuples (\"apple\", \"banana\")\n", - "- Non-Empty dictionaries { id: 10, name: \"Aiden\"}\n", - "- No\n", - "- Any Non zero numeric type" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "CLy5-zuqmV6k", - "slideshow": { - "slide_type": "slide" - } - }, - "source": [ - "# Order of operations\n", - "\n", - "This is a boring topic, but if you don't understand it, you'll write lots of bugs" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "slideshow": { - "slide_type": "fragment" - } - }, - "source": [ - "* Just like math, Python follows PEMDAS - or PE(MD)(AS). \n", - " * That is: Parentheses, Exponentiation, Multiplication and Division (left to right), and Addition and Subtraction(left to right)\n", - "* After the math operators, in order of presidence, are the relational operators (e.g. greater than, less than, etc.), and lastly the logical operators “not”, “and”, & “or” in that order." - ] - }, - { - "cell_type": "code", - "execution_count": 47, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 342, - "status": "ok", - "timestamp": 1607391600951, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "g0g2n8E7mkSf", - "outputId": "df09bf20-fa8c-4ed9-e670-97503a94867d", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "8" - ] - }, - "execution_count": 47, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "# e.g.\n", - "\n", - "x = 2\n", - "y = 3\n", - "\n", - "x * y + x # Yup, this works just like math" - ] - }, - { - "cell_type": "code", - "execution_count": 48, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 366, - "status": "ok", - "timestamp": 1607391602661, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "kcKJf8C0nGIQ", - "outputId": "d4675860-1131-433d-c9dc-78a447ca072b", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "10" - ] - }, - "execution_count": 48, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "x * (y + x) # If you want to force the addition \n", - "# before the multiplication you can use brackets, like in math\n", - "\n", - "# Technically, the brackets have highest precedence of any operator" - ] - }, - { - "cell_type": "code", - "execution_count": 49, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 350, - "status": "ok", - "timestamp": 1607391604199, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "Y7Zjv-vPoEjQ", - "outputId": "5780e325-2091-4cd1-8a89-00f26eccda95", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "8" - ] - }, - "execution_count": 49, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "# Exponents have higher precedence than division/multiplication\n", - "\n", - "2 * 2**2 # The exponent happens first" - ] - }, - { - "cell_type": "code", - "execution_count": 50, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 372, - "status": "ok", - "timestamp": 1607391605731, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "QO7dKEBPnsoH", - "outputId": "b5fe6c37-fe98-46f4-ba9c-f7c117422efc", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 50, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "# Arithmetic operators have precidence over logical operators\n", - "\n", - "x * y > x + y # This could also be written (x * y) > (x + y)" - ] - }, - { - "cell_type": "code", - "execution_count": 51, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 499, - "status": "ok", - "timestamp": 1607391607443, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "AZ32yKDSsARP", - "outputId": "320ef43f-b98f-4d1f-8f1d-c694a9e08aa1", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "False" - ] - }, - "execution_count": 51, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "# Sometimes it is helpful to use brackets, because it's hard to remember\n", - "# the order of operations, consider this..\n", - "\n", - "not 3 * 5 > 10 and 2 > 1" - ] - }, - { - "cell_type": "code", - "execution_count": 52, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 541, - "status": "ok", - "timestamp": 1607391609440, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "1hIvAe9PtOFQ", - "outputId": "d0f2cc99-323d-4411-eaea-f3f107cd5bb2", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "False" - ] - }, - "execution_count": 52, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "# Which I think is much clearer as..\n", - "\n", - "(not 3 * 5 > 10) and (2 > 1) # The brackets are just there for clarity" - ] - }, - { - "cell_type": "code", - "execution_count": 53, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 363, - "status": "ok", - "timestamp": 1607391611076, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "HDtWTEzotec7", - "outputId": "bcb4c84f-1088-46d7-f8c3-0eb85f438f36", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 53, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "# Or maybe even.. \n", - "\n", - "(not (3 * 5 > 10)) or (2 > 1)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [], - "source": [ - "# Suggested exercise: play with >, <, >=, <=, not, and, or, () \n", - "# and see if they do what you expect\n", - "\n", - "## Also definitely read text book on this for more thorough treatment" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "slideshow": { - "slide_type": "subslide" - } - }, - "source": [ - "# Simplifying Complex Expressions" - ] - }, - { - "cell_type": "code", - "execution_count": 38, - "metadata": { - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 38, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# Sometimes you're faced with a complex expression like:\n", - "\n", - "x = \"cats\"\n", - "y = \"dogs\"\n", - "\n", - "not (x > y or not x < y) or x == y # This is not very easy to parse!\n", - "\n", - "# To understand it try breaking it down into pieces by substituting the sub-expressions for True or False\n", - "# step by step, applying the presidence rules\n", - "\n", - "not (False or not True) or False # Here I substituted sub-expressions with True or False\n", - "\n", - "not (False or False) or False # More substitution\n", - "\n", - "not False or False # And again\n", - "\n", - "True or False # Nearly there\n", - "\n", - "True ## Done\n", - " \n", - "# Obviously, the compute has no problem evaluating this...\n", - "\n", - "not (x > y or not x < y) or x == y # Yup, it's True" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "slideshow": { - "slide_type": "fragment" - } - }, - "source": [ - "Suggested exercise: try making up a complex expression and then simplify it by hand. You can use the Python interpretor to check that your result is correct." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "slideshow": { - "slide_type": "subslide" - } - }, - "source": [ - "# Challenge 5" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [], - "source": [ - "# Change the expression below so that the 'not' is applied after the 'or' \n", - "(not (3 * 5 > 10)) or (2 > 1)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "slideshow": { - "slide_type": "subslide" - } - }, - "source": [ - "# Challenge 6" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [], - "source": [ - "# Remove the parentheses in the expression and see what would it print \n", - "x = 3 \n", - "y = 2 \n", - "print((not x > y and not x < y) or x//y == 2)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "ZwejXNuJaC-g", - "slideshow": { - "slide_type": "slide" - } - }, - "source": [ - "# Reading\n", - "\n", - "Openbook:\n", - "\n", - "* Read Chapter 2 on expressions, variables, statements and operators:\n", - " * http://openbookproject.net/thinkcs/python/english3e/variables_expressions_statements.html\n", - "\n", - " \n", - "# Homework\n", - "\n", - "* Go to Canvas and complete the second lecture quiz, which involves completing each challenge problem\n", - "* See \"Reading 2\" in Zybooks" - ] - } - ], - "metadata": { - "celltoolbar": "Slideshow", - "colab": { - "collapsed_sections": [ - "cjsgMup6ktUd" - ], - "name": "L02 Variables and Expressions.ipynb", - "provenance": [ - { - "file_id": "17GPUwfjMAW-SkqDI2qb9LangNzYzgNH_", - "timestamp": 1547074807405 - } - ] - }, - "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.9" - } - }, - "nbformat": 4, - "nbformat_minor": 1 -} From 8d6eb90958ab27bb6149395538a62f814231a908 Mon Sep 17 00:00:00 2001 From: crimpmug <113626525+crimpmug@users.noreply.github.com> Date: Wed, 18 Jan 2023 15:10:55 -0800 Subject: [PATCH 4/7] Add files via upload --- .../L02 Variables and Expressions.ipynb | 2811 +++++++++++++++++ 1 file changed, 2811 insertions(+) create mode 100644 lecture_notebooks/L02 Variables and Expressions.ipynb diff --git a/lecture_notebooks/L02 Variables and Expressions.ipynb b/lecture_notebooks/L02 Variables and Expressions.ipynb new file mode 100644 index 0000000..0754515 --- /dev/null +++ b/lecture_notebooks/L02 Variables and Expressions.ipynb @@ -0,0 +1,2811 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "cjsgMup6ktUd", + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# Lecture 2 - Variables and Expressions (https://bit.ly/intro_python_02)\n", + "\n", + "Today:\n", + "* Variables\n", + "* Assignment is not equals\n", + "* Copying References\n", + "* Expressions\n", + "* Statements\n", + "* Variable Names\n", + "* Operators\n", + "* Abbreviated assignment\n", + "* Logical operators\n", + "* Order of operations\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "OkA6aiONlE7b", + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# Variables" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "executionInfo": { + "elapsed": 363, + "status": "ok", + "timestamp": 1607391378206, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "7vBJtWFslI-G", + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "# As in algebra, we use variables to 'refer' to things\n", + "\n", + "x = 1 # x is a variable referring to the int 1\n", + "\n", + "y = \"foo\" # y is a variable referring to the string \"foo\"" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "S0j3Xtd1DhKp", + "slideshow": { + "slide_type": "fragment" + } + }, + "source": [ + "Think of x as \"referring to 1\". That is x is really a reference to the object representing 1. \n", + " * x -----> 1\n", + " * y ------> \"foo\"\n", + "\n", + "Under the hood, in general, there is a piece of memory (bits) representing x \n", + "that stores the location of where the value 1 is actually stored in memory. \n", + "\n", + "Sometimes, in some languages, x or y stores the content directly, e.g. x is actually \n", + "memory representing 1. However in Python variables are references." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "s8eUIhrM9xjr", + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# Assignment is not equals" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 349, + "status": "ok", + "timestamp": 1607391380918, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "1ADmdsvtEpAw", + "outputId": "9ade660c-a1b3-47e7-d1df-cbc2143b5d70", + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "foo\n" + ] + } + ], + "source": [ + "# The consequence of this \"refers to\" notion is that \n", + "# equals sign in Python means \"assignment\" not \"equals\"\n", + "\n", + "x = 1 # x refers to 1\n", + "\n", + "# Because = means assignment, we can update assignments, without it\n", + "# being meaningless in a math sense\n", + "\n", + "x = \"foo\" # x refers to \"foo\"\n", + "\n", + "#What's the value of x?\n", + "print(x)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "toK-UKQ198jr", + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# Copying references" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 373, + "status": "ok", + "timestamp": 1607391393919, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "FndrQqcuFho6", + "outputId": "3f7bfc04-874e-4ad0-e328-ef219333c8c5", + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5\n" + ] + } + ], + "source": [ + "# We can also copy variables\n", + "\n", + "x = 5\n", + "\n", + "y = x # Now x and y refer to the same thing (5)\n", + "\n", + "print(y)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 411, + "status": "ok", + "timestamp": 1607391407291, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "4ous2ZHC6Z1h", + "outputId": "7b6a6f09-34a4-42b3-a068-5232da47c35d", + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10\n", + "5\n" + ] + } + ], + "source": [ + "# But note, reassignment and copying can be co-mingled:\n", + "\n", + "x = 5\n", + "\n", + "y = x # Now x and y refer to the same thing (5)\n", + "\n", + "x = 10 # But now we change what x refers to, this doesn't change y!\n", + "\n", + "print(x) \n", + "print(y)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "sQLo2H1qGadW", + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# Variable Names\n", + "\n", + "Variables don't have to be single letters (in fact, that's a terrible idea), but can be **any combination of alpha-numeric characters and the '_' symbol, providing they don't start with a number and aren't already a reserved keyword**. Remember this!" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 424, + "status": "ok", + "timestamp": 1607391414262, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "p92pX5KtHHHk", + "outputId": "3f342de4-5e6a-4e09-ffca-46fd608459dd", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4\n" + ] + } + ], + "source": [ + "# Variable names\n", + "\n", + "foo = 1 # Legit variable name\n", + "\n", + "bar2 = 2 # Also legit\n", + "\n", + "_boo = 3 # Legit\n", + "\n", + "super_helpful_variable_name = 4 # Legit\n", + "\n", + "somePeopleLikeCamelCase = 5 # Fine variable name\n", + "\n", + "print(super_helpful_variable_name)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 130 + }, + "executionInfo": { + "elapsed": 381, + "status": "error", + "timestamp": 1607391419362, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "wkwn4Z1PHcjy", + "outputId": "03ea9788-5615-4905-91de-02ae1765e4b7", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "ignored", + "output_type": "error", + "traceback": [ + "\u001B[0;36m File \u001B[0;32m\"\"\u001B[0;36m, line \u001B[0;32m3\u001B[0m\n\u001B[0;31m 3l33t = 4 # Not okay\u001B[0m\n\u001B[0m ^\u001B[0m\n\u001B[0;31mSyntaxError\u001B[0m\u001B[0;31m:\u001B[0m invalid syntax\n" + ] + } + ], + "source": [ + "# Not okay\n", + "\n", + "3l33t = 4 # Not okay, starts with a number\n", + "\n", + "not^okay = 5 # Not okay, contains an illegal character\n", + "\n", + "and = 6 # Not okay, is a reserved keyword\n", + "\n", + "# Etc." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "U9VGvqwEHQtf", + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "Reserved key words, see Chapter 2 of the open textbook:\n", + "\n", + "http://openbookproject.net/thinkcs/python/english3e/variables_expressions_statements.html" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "bHeo34G5IWAN", + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# Expressions" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 349, + "status": "ok", + "timestamp": 1607391427113, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "dhSAuuO-Ich7", + "outputId": "4207f127-b0be-4ba3-c85e-813ab30a4ae7", + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Now we have basic types and variables, we can do stuff with them\n", + "\n", + "x = 1\n", + "\n", + "y = 2\n", + "\n", + "x + y - 2 # This is an expression, the interpreter\n", + "# has to \"evaluate it\" to figure out the value by \n", + "# combining variables (x, y), \n", + "# values (2) and operators (+, -)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 373, + "status": "ok", + "timestamp": 1607391431652, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "ega2_raeI6uc", + "outputId": "2e98e03f-f8e3-4a04-f134-52f3c027ba67", + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "float" + ] + }, + "execution_count": 10, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "# When we're calling a function (we'll learn about functions soon) (here the \"type\" function), \n", + "# we're also evaluating an expression\n", + "\n", + "type(3.14)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "OlhlFm06JFmO", + "slideshow": { + "slide_type": "fragment" + } + }, + "source": [ + "**Definition:** An expression is a combination of values, variables, operators, and calls to functions. Expressions need to be evaluated and result in a value (could be number, a string, an object, a list, etc.) \n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "# Challenge 1" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "# Write an expression that uses x and y and evaluates to 3\n", + "x = 1\n", + "y = 2" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# Statements" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 614, + "status": "ok", + "timestamp": 1607391462358, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "MhVakYkCJ0wv", + "outputId": "75f12273-51e2-4c97-91be-b93aa1d4c604", + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "boo\n" + ] + } + ], + "source": [ + "# Statements are instructions to the interpreter\n", + "\n", + "x = 1 # This is a statement, you're telling Python, make x refer to the value 1\n", + "\n", + "if x > 0: # Conditionals (like if (and others we'll see soon, \n", + " # are also \"statements\"))\n", + " print(\"boo\")" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "invalid syntax (450256079.py, line 5)", + "output_type": "error", + "traceback": [ + "\u001B[0;36m Cell \u001B[0;32mIn[5], line 5\u001B[0;36m\u001B[0m\n\u001B[0;31m print(x-=1)\u001B[0m\n\u001B[0m ^\u001B[0m\n\u001B[0;31mSyntaxError\u001B[0m\u001B[0;31m:\u001B[0m invalid syntax\n" + ] + } + ], + "source": [ + "# However, assignment statements, such as x -= y will not print\n", + "\n", + "# This is because assignment statements don't return a value for python to print\n", + "\n", + "# In this way we can differentiate from expressions\n", + "\n", + "print(x-=1)" + ], + "metadata": { + "collapsed": false + } + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Fmbh2g2FKdNd", + "slideshow": { + "slide_type": "fragment" + } + }, + "source": [ + "* A statement is everything that can make up a line of Python code.\n", + "\n", + "* A statement does something.\n", + "\n", + "Note, therefore, that expressions are statements as well.\n", + "\n", + "* All expressions are statements.\n", + "* Not all statements are expressions.\n", + "\n", + "\n", + "Note: you may see slightly different definitions in textbooks, but I like these" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "mNIC7nQkOxEP", + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# Operators" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 395, + "status": "ok", + "timestamp": 1607391472811, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "o-rCwk1oOwM1", + "outputId": "755ba91e-de2a-4182-ce62-687d269f404b", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "17" + ] + }, + "execution_count": 13, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "# Operators perform basic operations on objects\n", + "\n", + "# Let's start with arithmetic operators\n", + "\n", + "x = 12\n", + "y = 5\n", + "\n", + "x + y # + is the addition operator, duh" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 386, + "status": "ok", + "timestamp": 1607391476046, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "RTsa3LB9PzAK", + "outputId": "2cb111a5-d282-41f3-b67f-69058e3f23b5", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "60" + ] + }, + "execution_count": 14, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "x * y # multiplication" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 372, + "status": "ok", + "timestamp": 1607391477584, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "vZaiIhnOP4UW", + "outputId": "aaf74190-452c-44ac-8faa-70270bac0384", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x ** y # exponentiation " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "source": [ + "Note: The ** operator may be new to you, so try and remember it and difference with the * operator." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 383, + "status": "ok", + "timestamp": 1607391479027, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "tpccI3SiP7Ol", + "outputId": "9214331e-4f89-4374-cf52-b994b7c0b2c2", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x / y # What's the value of this one ?" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 389, + "status": "ok", + "timestamp": 1607391480313, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "BFUrcwMXQEL3", + "outputId": "3bd6b58a-c8dc-435d-9a27-ad1f03930f60", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 17, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "x // y # And this one?" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "source": [ + "Note: learn and remember the difference between the integer division (//) and division (/) operators!" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 378, + "status": "ok", + "timestamp": 1607391482032, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "tHknzzHjp-wF", + "outputId": "2e373b3c-1bfb-4825-d4c7-4a02db28efc8", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "-12" + ] + }, + "execution_count": 18, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "-x # Yup, we have negation" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "pTCYcOf4qDrm", + "slideshow": { + "slide_type": "fragment" + } + }, + "source": [ + "Note: \n", + " * Negation is an example of a unary operator (it takes one operand)\n", + "\n", + " * The other operators are binary (they take two operands)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 364, + "status": "ok", + "timestamp": 1607391485528, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "wKJ17ugFsXo1", + "outputId": "359750e3-3454-4c56-f20d-40a0c665c67c", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 19, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "5 % 2 # The modulus operator, it returns the \"remainder\"" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 335, + "status": "ok", + "timestamp": 1607391487065, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "-d4YQzH1slYv", + "outputId": "2047f716-b6da-4148-b2c9-308ad5297796", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# It's a good way to determine if a number is divisible by another,\n", + "# because if it is, the expression value will be 0\n", + "4 % 2" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "# Challenge 2" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "x = 5\n", + "y = 2\n", + "# Write a statement that divides x by y, forgetting the remainder, storing the result in a variable z" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "-_noHbEBglV5", + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# Operator Overloading" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 367, + "status": "ok", + "timestamp": 1607391491626, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "VM61C-mG8z10", + "outputId": "eb800a43-f593-4dd0-b149-c9c40f311a1c", + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "27" + ] + }, + "execution_count": 21, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "5 + 10 + 12 # We can use \"+\" to add together strings of numbers" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "executionInfo": { + "elapsed": 344, + "status": "ok", + "timestamp": 1607391522629, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "zsIwvpbFRzvf", + "outputId": "2bc48222-e07d-4f25-ef67-6e3be2cd7aab", + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "('This', 'is', 'contrived')" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Some arithmetic operators are also \"overloaded\" to work with strings\n", + "# (This is an example of \"polymorphism\", that we'll meet again much later)\n", + "\n", + "\"This\" + \"is\" + \"contrived\" # The '+' concatenates strings" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 198 + }, + "executionInfo": { + "elapsed": 319, + "status": "error", + "timestamp": 1607391525883, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "cggT1iCJN7AI", + "outputId": "57c8840f-f089-4328-e414-5a97914ce860", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "ename": "TypeError", + "evalue": "ignored", + "output_type": "error", + "traceback": [ + "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m", + "\u001B[0;31mTypeError\u001B[0m Traceback (most recent call last)", + "\u001B[0;32m\u001B[0m in \u001B[0;36m\u001B[0;34m()\u001B[0m\n\u001B[1;32m 2\u001B[0m \u001B[0;31m# a number together\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[1;32m 3\u001B[0m \u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0;32m----> 4\u001B[0;31m \u001B[0;34m\"this\"\u001B[0m \u001B[0;34m+\u001B[0m \u001B[0;36m5\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0m", + "\u001B[0;31mTypeError\u001B[0m: must be str, not int" + ] + } + ], + "source": [ + "# Note, this doesn't work because Python doesn't know how to add a string and\n", + "# a number together\n", + "\n", + "\"this\" + 5" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "9XAjll9RUokG", + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# Abbreviated Assignment\n", + "\n", + "You will see this a lot:" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 343, + "status": "ok", + "timestamp": 1607391531326, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "Ls-IKDqWUu0u", + "outputId": "ccf895e0-81d1-4501-c0a6-a08dddb0dbe2", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "11" + ] + }, + "execution_count": 26, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "x = 1\n", + "\n", + "# Instead of writing\n", + "\n", + "x = x + 5\n", + "\n", + "# you can use the shorthand:\n", + "\n", + "x += 5 # This means \"add 5 to value x refers to\"\n", + "\n", + "x" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "1zKtAWt7Uvzo", + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "This works for all the obvious math operators:" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 370, + "status": "ok", + "timestamp": 1607391536440, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "DWhES0_nU0xS", + "outputId": "93b32e09-3dff-49b0-ed65-0121ead40033", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "22\n" + ] + } + ], + "source": [ + "\n", + "\n", + "x *= 2 # multiply x by 2, aka x = x * 2\n", + "\n", + "print(x)" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 357, + "status": "ok", + "timestamp": 1607391540567, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "536MyapkU21H", + "outputId": "d9cf7d10-91f6-412e-f8ab-355a8488bdb4", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "19\n" + ] + } + ], + "source": [ + "x -= 3 # subtract three, aka x = x - 3\n", + "\n", + "print(x)" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 372, + "status": "ok", + "timestamp": 1607391542854, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "GonNznhrU6E1", + "outputId": "092d1fb9-e1c9-4786-d001-36518e69dd74", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "9.5\n" + ] + } + ], + "source": [ + "x /= 2 # divide by 2 \n", + "\n", + "print(x)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 358, + "status": "ok", + "timestamp": 1607391544853, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "Sm7tItanU8WA", + "outputId": "c5bbbc82-a61d-4cf8-cc55-cc1161489f4a", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n" + ] + } + ], + "source": [ + "x //= 2 # divide by 2 and forget fraction\n", + "\n", + "print(x)" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 387, + "status": "ok", + "timestamp": 1607391549191, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "WRp0r1tAU_Kg", + "outputId": "877fa5c8-c703-4696-a147-da34228da6f3", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0.0\n" + ] + } + ], + "source": [ + "x %= 2 # Take x mod 2 and store the result in x, i.e. x = x % 2\n", + "\n", + "print(x)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "# Challenge 3" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "x = 7 \n", + "y = 2\n", + "# Use abbreviated assignment to subtract y from x, storing the result as x" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "6oYe0AgQl7kl", + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# Boolean Type\n", + "\n", + "Python has a special boolean (binary) type, which can either be 'True' or 'False'. \n", + "\n", + "This is essential for logical expressions.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 342, + "status": "ok", + "timestamp": 1607391563443, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "12mmMhhGmEmz", + "outputId": "b52afd9e-f599-4cd7-e635-58f70bff9a18", + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "bool" + ] + }, + "execution_count": 32, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "type(True)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 387, + "status": "ok", + "timestamp": 1607391564807, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "IN_9Y3UdmPVi", + "outputId": "fe44676e-cbf3-4151-b441-2bf6bb4447a3", + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "bool" + ] + }, + "execution_count": 33, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "type(False)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "SjGU71_1QWqx", + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# Logical Operators\n", + "\n", + " Booleans are used for making decisions in your program.\n", + " \n", + " To do this we use logical operators, which do, err, logic and evaluate to booleans." + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 601, + "status": "ok", + "timestamp": 1607391567269, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "bKqHlRGZ6Vyz", + "outputId": "989aeb11-c58f-4f9a-b43c-9ca6f21ebe34", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 34, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "x = 5\n", + "y = 10\n", + "\n", + "x > y # The greater than operator compares two things" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 361, + "status": "ok", + "timestamp": 1607391568662, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "WowJC26dmoPX", + "outputId": "fd4ea3be-d279-45e5-b6e2-5aeb14e4fb6f", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "bool" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# To see the relationship with Booleans\n", + "x = 5\n", + "y = 10\n", + "\n", + "\n", + "type(x > y)" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 385, + "status": "ok", + "timestamp": 1607391570353, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "SJcNUseq6VzG", + "outputId": "e12c144f-3997-416b-9cd5-66c3b31a5a02", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 36, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "# There are a bunch of these\n", + "\n", + "x >= y # Is x greater than or equal to y?" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 403, + "status": "ok", + "timestamp": 1607391571943, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "hDYP9S1J6VzI", + "outputId": "1c178c9c-9aa6-41a2-eb98-de7169e6cba9", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 37, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "x < y # Is x less than y?" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 343, + "status": "ok", + "timestamp": 1607391579454, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "i_JdqLQi6VzK", + "outputId": "9d69d1b9-25c8-4bd3-b678-436670d83b96", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 38, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "x <= y # Less than or equals?" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 321, + "status": "ok", + "timestamp": 1607391580841, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "fXbNwOfd6VzM", + "outputId": "878ea4b5-c993-4510-ce61-bb94482ba231", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# What about this one?\n", + "\n", + "x == y" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "oEu8nmKg6VzR", + "slideshow": { + "slide_type": "fragment" + } + }, + "source": [ + "* As we discussed earlier, in Python (and many languages) '=' is the assignment operator\n", + "* Logical equals is '=='. Some people find this weird, but you get over it.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 336, + "status": "ok", + "timestamp": 1607391583102, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "hkZW2hBN_F9k", + "outputId": "b95dc957-8a08-4321-fc66-b2e182f523db", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 40, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "# Python also has the not logical equals operator !=\n", + "\n", + "x != y # Read this as \"does x not equal y?\"" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 344, + "status": "ok", + "timestamp": 1607391585169, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "IGoHXsjM6VzR", + "outputId": "49896c73-c773-4e13-b826-19ff54a18be8", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 41, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "# We can compose logical statements into complex expressions \n", + "# using logical 'and' and 'or'\n", + "\n", + "x = 5\n", + "y = 10\n", + "z = 7\n", + "\n", + "x >= y or z > x # Says: True if x is greater than or equal to y or z is greater than x" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 366, + "status": "ok", + "timestamp": 1607391587116, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "BI0J-9Bt6VzV", + "outputId": "17a0145a-4299-4342-fe62-f20df8342b62", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 42, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "# Similarly\n", + "\n", + "y > x and y > z # Says: True if y is greater than x AND y is greater than z" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 344, + "status": "ok", + "timestamp": 1607391588715, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "fYlNC0fz6VzY", + "outputId": "abf04d8b-c83b-4ee8-ee70-ec4fc25269ed", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 43, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "# There is also the unary negation operator: not\n", + "\n", + "not True" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 345, + "status": "ok", + "timestamp": 1607391590335, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "ffSYgAtK6Vze", + "outputId": "cdf7bcbc-8c10-4244-b435-c49b1bbc592e", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 44, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "# Use it to switch True to False and vice versa:\n", + "\n", + "y = 0\n", + "x = 1\n", + "\n", + "not y > x" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "cNDSDbL86Vzh", + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "Logical comparisons also work with strings" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 338, + "status": "ok", + "timestamp": 1607391592411, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "NEN92T0s6Vzk", + "outputId": "9442c91d-f428-463a-fea0-0343ce7b5acd", + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 45, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "# String comparison\n", + "\n", + "\"cats\" > \"dogs\" # ?" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "# Challenge 4" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "x = int(input(\"Enter a number\"))\n", + "y = int(input(\"Enter a second number\"))\n", + "# Write a logical expression that is True if and only if \n", + "# x is greater than y or x is divisible by y" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# Truthy and Falsy Values\n", + "\n", + "Values can evaluate as either True or False in logical expressions despite not being of a boolean type" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "bool(\"\") # The empty string is false" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "bool(\"hello\") # A non-empty string is true" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "source": [ + "We'll see more examples of how different types map to True and False later (but see the note for a summary, looking ahead)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "notes" + } + }, + "source": [ + "This becomes increasingly helpful as we start to deal with more complex data structures. \n", + "\n", + "#### The values that will evaluate as false are:\n", + "- Empty lists []\n", + "- Empty tuples ()\n", + "- Empty dictionaries {}\n", + "- Empty sets set()\n", + "- Empty strings \"\"\n", + "- Empty ranges range(0)\n", + "- And any numeric type that is 0; Int, Float, Complex\n", + "\n", + "In contrast the values that evaluate as true are:\n", + "- Non-Empty strings \"hello\"\n", + "- Non-Empty tuples (\"apple\", \"banana\")\n", + "- Non-Empty dictionaries { id: 10, name: \"Aiden\"}\n", + "- No\n", + "- Any Non zero numeric type" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "CLy5-zuqmV6k", + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# Order of operations\n", + "\n", + "This is a boring topic, but if you don't understand it, you'll write lots of bugs" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "source": [ + "* Just like math, Python follows PEMDAS - or PE(MD)(AS). \n", + " * That is: Parentheses, Exponentiation, Multiplication and Division (left to right), and Addition and Subtraction(left to right)\n", + "* After the math operators, in order of presidence, are the relational operators (e.g. greater than, less than, etc.), and lastly the logical operators “not”, “and”, & “or” in that order." + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 342, + "status": "ok", + "timestamp": 1607391600951, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "g0g2n8E7mkSf", + "outputId": "df09bf20-fa8c-4ed9-e670-97503a94867d", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "8" + ] + }, + "execution_count": 47, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "# e.g.\n", + "\n", + "x = 2\n", + "y = 3\n", + "\n", + "x * y + x # Yup, this works just like math" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 366, + "status": "ok", + "timestamp": 1607391602661, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "kcKJf8C0nGIQ", + "outputId": "d4675860-1131-433d-c9dc-78a447ca072b", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "10" + ] + }, + "execution_count": 48, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "x * (y + x) # If you want to force the addition \n", + "# before the multiplication you can use brackets, like in math\n", + "\n", + "# Technically, the brackets have highest precedence of any operator" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 350, + "status": "ok", + "timestamp": 1607391604199, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "Y7Zjv-vPoEjQ", + "outputId": "5780e325-2091-4cd1-8a89-00f26eccda95", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "8" + ] + }, + "execution_count": 49, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "# Exponents have higher precedence than division/multiplication\n", + "\n", + "2 * 2**2 # The exponent happens first" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 372, + "status": "ok", + "timestamp": 1607391605731, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "QO7dKEBPnsoH", + "outputId": "b5fe6c37-fe98-46f4-ba9c-f7c117422efc", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 50, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "# Arithmetic operators have precidence over logical operators\n", + "\n", + "x * y > x + y # This could also be written (x * y) > (x + y)" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 499, + "status": "ok", + "timestamp": 1607391607443, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "AZ32yKDSsARP", + "outputId": "320ef43f-b98f-4d1f-8f1d-c694a9e08aa1", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 51, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "# Sometimes it is helpful to use brackets, because it's hard to remember\n", + "# the order of operations, consider this..\n", + "\n", + "not 3 * 5 > 10 and 2 > 1" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 541, + "status": "ok", + "timestamp": 1607391609440, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "1hIvAe9PtOFQ", + "outputId": "d0f2cc99-323d-4411-eaea-f3f107cd5bb2", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 52, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "# Which I think is much clearer as..\n", + "\n", + "(not 3 * 5 > 10) and (2 > 1) # The brackets are just there for clarity" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 363, + "status": "ok", + "timestamp": 1607391611076, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "HDtWTEzotec7", + "outputId": "bcb4c84f-1088-46d7-f8c3-0eb85f438f36", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 53, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "# Or maybe even.. \n", + "\n", + "(not (3 * 5 > 10)) or (2 > 1)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "# Suggested exercise: play with >, <, >=, <=, not, and, or, () \n", + "# and see if they do what you expect\n", + "\n", + "## Also definitely read text book on this for more thorough treatment" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "# Simplifying Complex Expressions" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Sometimes you're faced with a complex expression like:\n", + "\n", + "x = \"cats\"\n", + "y = \"dogs\"\n", + "\n", + "not (x > y or not x < y) or x == y # This is not very easy to parse!\n", + "\n", + "# To understand it try breaking it down into pieces by substituting the sub-expressions for True or False\n", + "# step by step, applying the presidence rules\n", + "\n", + "not (False or not True) or False # Here I substituted sub-expressions with True or False\n", + "\n", + "not (False or False) or False # More substitution\n", + "\n", + "not False or False # And again\n", + "\n", + "True or False # Nearly there\n", + "\n", + "True ## Done\n", + " \n", + "# Obviously, the compute has no problem evaluating this...\n", + "\n", + "not (x > y or not x < y) or x == y # Yup, it's True" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "source": [ + "Suggested exercise: try making up a complex expression and then simplify it by hand. You can use the Python interpretor to check that your result is correct." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "# Challenge 5" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "# Change the expression below so that the 'not' is applied after the 'or' \n", + "(not (3 * 5 > 10)) or (2 > 1)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "# Challenge 6" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "# Remove the parentheses in the expression and see what would it print \n", + "x = 3 \n", + "y = 2 \n", + "print((not x > y and not x < y) or x//y == 2)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ZwejXNuJaC-g", + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# Reading\n", + "\n", + "Openbook:\n", + "\n", + "* Read Chapter 2 on expressions, variables, statements and operators:\n", + " * http://openbookproject.net/thinkcs/python/english3e/variables_expressions_statements.html\n", + "\n", + " \n", + "# Homework\n", + "\n", + "* Go to Canvas and complete the second lecture quiz, which involves completing each challenge problem\n", + "* See \"Reading 2\" in Zybooks" + ] + } + ], + "metadata": { + "celltoolbar": "Slideshow", + "colab": { + "collapsed_sections": [ + "cjsgMup6ktUd" + ], + "name": "L02 Variables and Expressions.ipynb", + "provenance": [ + { + "file_id": "17GPUwfjMAW-SkqDI2qb9LangNzYzgNH_", + "timestamp": 1547074807405 + } + ] + }, + "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.9" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} From 8c666610d41e5bc61d594464ef26d0e6ab977a8d Mon Sep 17 00:00:00 2001 From: crimpmug <113626525+crimpmug@users.noreply.github.com> Date: Wed, 18 Jan 2023 15:33:42 -0800 Subject: [PATCH 5/7] Add files via upload --- L03 More Types.ipynb | 2815 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 2815 insertions(+) create mode 100644 L03 More Types.ipynb diff --git a/L03 More Types.ipynb b/L03 More Types.ipynb new file mode 100644 index 0000000..f0dd832 --- /dev/null +++ b/L03 More Types.ipynb @@ -0,0 +1,2815 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "S4CkZnM_4FvB", + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# Lecture 3 - More Types (https://bit.ly/intro_python_03)\n", + "\n", + "Today:\n", + "* More strings\n", + "* F strings and number formatting\n", + "* Type Conversions\n", + "* Automatic Type Conversion\n", + "* More Input\n", + "* List basics\n", + "* Dictionary Basics" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "odfPyGzkdUIJ", + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# More Strings\n", + "\n", + "In Python3.0 strings are sequences of symbols from an alphabet called Unicode: https://unicode.org/ \n", + "\n", + "* Unicode allows a very wide range of symbols for many languages and encodings. Much more than ASCII (http://www.asciitable.com/), which is what Python used to use. This requires more memory to store the strings, but prevents weird syntax translation issues." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "qEy9bWvx5QHt", + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "![alt text](https://pediaa.com/wp-content/uploads/2018/07/Difference-Between-ASCII-and-Unicode-Comparison-Summary.jpg)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# String Syntax" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "executionInfo": { + "elapsed": 584, + "status": "ok", + "timestamp": 1569539469038, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "", + "userId": "02468802669323291213" + }, + "user_tz": 420 + }, + "id": "z5hWOCXYcrFt", + "outputId": "694e6e4c-17e5-429d-d48a-8bc0552cff97", + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "str" + ] + }, + "execution_count": 6, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "# Strings can be expressed a bunch of ways:\n", + "\n", + "type(\"hello\") # Double quotes" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "executionInfo": { + "elapsed": 693, + "status": "ok", + "timestamp": 1554323886402, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh6.googleusercontent.com/-KEXp34y2Ra8/AAAAAAAAAAI/AAAAAAAAAHs/xzPsOsCpcaU/s64/photo.jpg", + "userId": "06399644931392855882" + }, + "user_tz": 420 + }, + "id": "13L6yoJ2f5_F", + "outputId": "17e82f98-e82e-4f31-e2a0-3bc2c856471c", + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "str" + ] + }, + "execution_count": 7, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "type('hello') # Single quotes" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "executionInfo": { + "elapsed": 378, + "status": "ok", + "timestamp": 1554323904826, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh6.googleusercontent.com/-KEXp34y2Ra8/AAAAAAAAAAI/AAAAAAAAAHs/xzPsOsCpcaU/s64/photo.jpg", + "userId": "06399644931392855882" + }, + "user_tz": 420 + }, + "id": "xG8NQ3iLf7m4", + "outputId": "cdf6c8d7-bfc9-46e3-8cb2-f8b64a39fc59", + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "str" + ] + }, + "execution_count": 8, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "type(\"\"\"hello\"\"\") # Triple double quotes" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "executionInfo": { + "elapsed": 539, + "status": "ok", + "timestamp": 1554324028562, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh6.googleusercontent.com/-KEXp34y2Ra8/AAAAAAAAAAI/AAAAAAAAAHs/xzPsOsCpcaU/s64/photo.jpg", + "userId": "06399644931392855882" + }, + "user_tz": 420 + }, + "id": "ospXwmAEf9cn", + "outputId": "13ca371a-b6ce-48f6-afcc-3df1628a2531", + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "str" + ] + }, + "execution_count": 14, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "type('''hello''') # Triple single quotes" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ND2UHQJkgCCH", + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "We have these different syntax so we can embed quotes and whitespace (tabs, spaces and new lines) in strings. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "executionInfo": { + "elapsed": 437, + "status": "ok", + "timestamp": 1554324081443, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh6.googleusercontent.com/-KEXp34y2Ra8/AAAAAAAAAAI/AAAAAAAAAHs/xzPsOsCpcaU/s64/photo.jpg", + "userId": "06399644931392855882" + }, + "user_tz": 420 + }, + "id": "_kq0Ugs2Gc8_", + "outputId": "3ec14f59-7aa5-44c7-8d0d-474b02e1450f", + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "str" + ] + }, + "execution_count": 15, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "# We can embed single quotes in double quotes:\n", + "type(\"This is a string containing a 'single quoted substring'\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 130 + }, + "executionInfo": { + "elapsed": 501, + "status": "error", + "timestamp": 1554324108166, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh6.googleusercontent.com/-KEXp34y2Ra8/AAAAAAAAAAI/AAAAAAAAAHs/xzPsOsCpcaU/s64/photo.jpg", + "userId": "06399644931392855882" + }, + "user_tz": 420 + }, + "id": "J5UDSxu6gnm8", + "outputId": "b27a3768-0b1f-4524-c772-bee9b7a8b837", + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "ignored", + "output_type": "error", + "traceback": [ + "\u001B[0;36m File \u001B[0;32m\"\"\u001B[0;36m, line \u001B[0;32m1\u001B[0m\n\u001B[0;31m type('This is a string containing a 'single quoted substring'')\u001B[0m\n\u001B[0m ^\u001B[0m\n\u001B[0;31mSyntaxError\u001B[0m\u001B[0;31m:\u001B[0m invalid syntax\n" + ] + } + ], + "source": [ + "# Trying to embed single quotes \n", + "# in a single quoted string naively does not work\n", + "type('This is a string containing a 'single quoted substring'')" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "executionInfo": { + "elapsed": 432, + "status": "ok", + "timestamp": 1554324181369, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh6.googleusercontent.com/-KEXp34y2Ra8/AAAAAAAAAAI/AAAAAAAAAHs/xzPsOsCpcaU/s64/photo.jpg", + "userId": "06399644931392855882" + }, + "user_tz": 420 + }, + "id": "nvgVKUi2g7yI", + "outputId": "49f5539f-bdd5-4965-c235-f0fe8bea4316", + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "str" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# And vice-versa, we can embed double quotes in single quotes:\n", + "type('This is a string containing a \"double quoted substring\"')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 131 + }, + "executionInfo": { + "elapsed": 1099, + "status": "error", + "timestamp": 1569539635155, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "", + "userId": "02468802669323291213" + }, + "user_tz": 420 + }, + "id": "mZ-JcBF9jkay", + "outputId": "aac1e383-898b-4b0f-95a0-a5592e4b20c3", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "ignored", + "output_type": "error", + "traceback": [ + "\u001B[0;36m File \u001B[0;32m\"\"\u001B[0;36m, line \u001B[0;32m1\u001B[0m\n\u001B[0;31m type('This is an\u001B[0m\n\u001B[0m ^\u001B[0m\n\u001B[0;31mSyntaxError\u001B[0m\u001B[0;31m:\u001B[0m EOL while scanning string literal\n" + ] + } + ], + "source": [ + "# Single and double quoted strings can't span multiple lines:\n", + "type('This is an \n", + " attempt to have a string on two lines')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "executionInfo": { + "elapsed": 417, + "status": "ok", + "timestamp": 1554324236518, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh6.googleusercontent.com/-KEXp34y2Ra8/AAAAAAAAAAI/AAAAAAAAAHs/xzPsOsCpcaU/s64/photo.jpg", + "userId": "06399644931392855882" + }, + "user_tz": 420 + }, + "id": "tPVSUn-biK47", + "outputId": "d6dda436-f9f3-4bb3-dd71-5213c27942b7", + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "str" + ] + }, + "execution_count": 19, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "# Triple quotes (either single or double), allow embedding essentially anything, \n", + "# including spanning multiple lines:\n", + "\n", + "type(\"\"\"Boom, now\n", + " we can make crazy strings that ' nest quotes in quotes \" \" ' etc.\n", + " and which span multiple lines\"\"\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 54 + }, + "executionInfo": { + "elapsed": 850, + "status": "ok", + "timestamp": 1554324365093, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh6.googleusercontent.com/-KEXp34y2Ra8/AAAAAAAAAAI/AAAAAAAAAHs/xzPsOsCpcaU/s64/photo.jpg", + "userId": "06399644931392855882" + }, + "user_tz": 420 + }, + "id": "2NARKErpkhsr", + "outputId": "062c975d-aed6-4de7-c18b-4be0692e3bc8", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "\" \\nA comment string. \\n\\nThese can be used to document some code\\nacross multiple lines. We'll later see this at the top\\nof classes and functions as a standard way of giving\\na description string.\\n\"" + ] + }, + "execution_count": 20, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "# You will also see this a lot in Python code:\n", + "\n", + "\"\"\" \n", + "A comment string. \n", + "\n", + "These can be used to document some code\n", + "across multiple lines. We'll later see this at the top\n", + "of classes and functions as a standard way of giving\n", + "a description string.\n", + "\"\"\"\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "# Challenge 1" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "# Create a string containing single quotes, double quotes and which spans multiple lines" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "JPxEMRO9bFgc", + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# Whitespace Characters\n", + "\n", + "* There is a difference between the contents of a Python string you type (termed a string literal) and what gets printed to the screen.\n", + "\n", + "* In general, Python and most other programming languages use reserved ASCII or unicode characters to encode whitespace in string literals. \n", + "\n", + "* Lets cover tabs and newlines" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "executionInfo": { + "elapsed": 872, + "status": "ok", + "timestamp": 1571351313722, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AAuE7mBMnWy8dDR7jyTHNy9tPaRx6DCyA3QKrIcuQ7R4=s64", + "userId": "06399644931392855882" + }, + "user_tz": 420 + }, + "id": "lwuNwoa1kPb1", + "outputId": "f77eb9ac-60af-4b1b-c933-4c4a1e47eb17", + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "This\tis\ttab\tseparated\n" + ] + } + ], + "source": [ + "s = \"This\\tis\\ttab\\tseparated\" # Tabs are encoded with '\\t' \n", + "\n", + "print(s)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 87 + }, + "executionInfo": { + "elapsed": 484, + "status": "ok", + "timestamp": 1571351375970, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AAuE7mBMnWy8dDR7jyTHNy9tPaRx6DCyA3QKrIcuQ7R4=s64", + "userId": "06399644931392855882" + }, + "user_tz": 420 + }, + "id": "-S3ED-nHk4uA", + "outputId": "12ed2be0-97e4-4141-dcb7-37cf24e25d10", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "This\n", + "is\n", + "newline\n", + "separated\n" + ] + } + ], + "source": [ + "s = \"This\\nis\\nnewline\\nseparated\" # Newline characters: '\\n' specify new lines\n", + "\n", + "print(s)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "executionInfo": { + "elapsed": 929, + "status": "ok", + "timestamp": 1571351438223, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AAuE7mBMnWy8dDR7jyTHNy9tPaRx6DCyA3QKrIcuQ7R4=s64", + "userId": "06399644931392855882" + }, + "user_tz": 420 + }, + "id": "UlraTeHQlUvU", + "outputId": "b42aef5a-099a-4c51-e513-4711fc29a452", + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], + "source": [ + "s2 = \"\"\"This\n", + "is\n", + "newline\n", + "separated\"\"\" # This is the equivalent way of specifying s by using \n", + "# explicit white space in your code\n", + "\n", + "print(s == s2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 87 + }, + "executionInfo": { + "elapsed": 476, + "status": "ok", + "timestamp": 1571351461721, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AAuE7mBMnWy8dDR7jyTHNy9tPaRx6DCyA3QKrIcuQ7R4=s64", + "userId": "06399644931392855882" + }, + "user_tz": 420 + }, + "id": "JYS9BkO6lg2I", + "outputId": "9dbc2e01-4047-4bea-c45f-b1dd45974af4", + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "This\n", + "is\n", + "newline\n", + "separated\n" + ] + } + ], + "source": [ + "print(s)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 87 + }, + "executionInfo": { + "elapsed": 485, + "status": "ok", + "timestamp": 1571351463892, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AAuE7mBMnWy8dDR7jyTHNy9tPaRx6DCyA3QKrIcuQ7R4=s64", + "userId": "06399644931392855882" + }, + "user_tz": 420 + }, + "id": "p1PTjHmMliDv", + "outputId": "dc867dbb-f667-4b5a-9039-a2f1b089c974", + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "This\n", + "is\n", + "newline\n", + "separated\n" + ] + } + ], + "source": [ + "print(s2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 69 + }, + "executionInfo": { + "elapsed": 874, + "status": "ok", + "timestamp": 1571351511757, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AAuE7mBMnWy8dDR7jyTHNy9tPaRx6DCyA3QKrIcuQ7R4=s64", + "userId": "06399644931392855882" + }, + "user_tz": 420 + }, + "id": "Wk43tEb0l550", + "outputId": "b9239e54-5105-472e-de52-6ae4ddd5092d", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Welcome to my program.\n", + "\tHere is an indent.\n", + "\t\tHere is a double indent\n" + ] + } + ], + "source": [ + "# It is often convenient to use tab and newline characters in your string \n", + "# formatting to make the strings do what you want, e.g.\n", + "\n", + "print(\"Welcome to my program.\\n\\tHere is an indent.\\n\\t\\tHere is a double indent\")" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Mdhal66NmQHu", + "slideshow": { + "slide_type": "fragment" + } + }, + "source": [ + "Doing the above with triple quoted strings is less compact, and generally more error prone,\n", + "because the way the editor encodes tabs depends on how the code formatting is setup:\n", + "e.g. are blocks of spaces tabs? how many spaces? etc." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 69 + }, + "executionInfo": { + "elapsed": 626, + "status": "ok", + "timestamp": 1571351574595, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AAuE7mBMnWy8dDR7jyTHNy9tPaRx6DCyA3QKrIcuQ7R4=s64", + "userId": "06399644931392855882" + }, + "user_tz": 420 + }, + "id": "MqDnWyC_mXwN", + "outputId": "66396915-41fa-4076-c1e9-ccdbaae24efa", + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Welcome to my program.\n", + " Here is an indent.\n", + " Here is a double indent\n" + ] + } + ], + "source": [ + "print(\"\"\"Welcome to my program.\n", + " Here is an indent.\n", + " Here is a double indent\"\"\")" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "# Escape Characters\n", + "\n", + "**The use of \"\\t\" and \"\\n\" are examples of escape characters.**\n", + "\n", + "* In general, the **backslash** “\\” or the “**escape**” character is used to insert whitespace character into strings. \n", + "* Some of these include...\n", + "\n", + " 1. \\t - Horizontal tab\n", + " 2. \\v - Vertical tab\n", + " 3. \\n - Newline\n", + " 4. \\r - Carriage return\n", + " 5. \\f - Feed\n", + " #\n", + " \n", + "(As an aside, I never use the vertical tab, feed or carriage return, but YMMV) \n", + " \n", + " \n", + "* The “***escape***” character can also be used to utilize special characters when using strings\n", + "* Some of these include (and are useful to remember):\n", + "\n", + " 1. \\\\' - Single Quote\n", + " 2. \\\\\\ - Backslash\n" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "This is a string with a double quote in it: \", and a single quote: ' and a backslash \\n \n" + ] + } + ], + "source": [ + "# Use escape characters to include the symbols you want in a string:\n", + "\n", + "x = \"This is a string with a double quote in it: \\\", and a single quote: \\' and a backslash with an n \\\\n \"\n", + "\n", + "print(x)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "slideshow": { + "slide_type": "notes" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "This is a string with a \r", + " carriage return\n" + ] + } + ], + "source": [ + "# Using carriage return to write over what was already printed!\n", + "\n", + "print(\"This is a string with a \\r carriage return\")" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "# Challenge 2" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "# Rewrite the following string to use single quotes and escape characters\n", + "\"\"\"this is\n", + "a contrived example\n", + " to test \n", + " you!\"\"\"" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# F-strings" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "source": [ + "Often we want to substitute values into strings. Python's f-strings are a nice way to do this (we'll see other ways to do this later)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "I went to 10 Downing St, London, SW1A, UK to see the prime minister\n" + ] + } + ], + "source": [ + "# Suppose we have these two variables\n", + "\n", + "address=\"10 Downing St, London, SW1A, UK\"\n", + "person=\"prime minister\"\n", + "\n", + "# We can introduce them into a f-string as follows\n", + "print(f\"I went to {address} to see the {person}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "outputs": [], + "source": [ + "F-strings were introduced in Python 3.6. They provide a convenient alternative to the format method (see below)" + ], + "metadata": { + "collapsed": false + } + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "I went to 10 Downing St, London, SW1A, UK to see the prime minister\n", + "I went to 10 Downing St, London, SW1A, UK to see the prime minister\n", + "I went to 10 Downing St, London, SW1A, UK to see the prime minister\n", + "I went to 10 Downing St, London, SW1A, UK to see the prime minister\n" + ] + } + ], + "source": [ + "# The general syntax allows either F or f as the prefix. \n", + "# They work with single quotes, double quotes and triple quotes, just as with vanilla strings\n", + "\n", + "print(f\"I went to {address} to see the {person}\")\n", + "print(F\"I went to {address} to see the {person}\")\n", + "print(f'I went to {address} to see the {person}')\n", + "print(F\"\"\"I went to {address} to see the {person}\"\"\")\n", + "# etc..." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "This is math: 25, and this is string multiplication: hellohello\n" + ] + } + ], + "source": [ + "# You can embed arbitrary expressions within the f-string:\n", + "print(f\"This is math: { 5 * 5 }, and this is string multiplication: { 'hello' * 2 }\")" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "source": [ + "Note in the above example we must embed single quoted strings in the double quoted string." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "SfuZ30qgzSDd", + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# Formatting Numbers\n", + "\n", + "It is important to be able to control the format of numbers during printing!\n", + "\n", + "This is one of those things I always have to look up to get right, this table is useful as a cheat sheet:\n", + "\n", + "https://mkaz.blog/code/python-string-format-cookbook/" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "executionInfo": { + "elapsed": 615, + "status": "ok", + "timestamp": 1571354377395, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AAuE7mBMnWy8dDR7jyTHNy9tPaRx6DCyA3QKrIcuQ7R4=s64", + "userId": "06399644931392855882" + }, + "user_tz": 420 + }, + "id": "Kej77oYdI540", + "outputId": "555063a7-033f-4690-98b0-18e43461d0ae", + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'The number is: 0.9496503'" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x = 0.9745\n", + "\n", + "f\"The number is: {x ** 2:.7f}\" # The : indicates that you're issuing a formatting command\n", + "# the .7 says you want to seven decimal places, \n", + "# f says the thing you're printing is a float" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'The number is: 00.975'" + ] + }, + "execution_count": 52, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# You can control the total number of digits printed as follows:\n", + "\n", + "f\"The number is: {x:06.3f}\" # The same\n", + "\n", + "## Here {:0x.yf} x (the 6) is the total number of symbols to be printed \n", + "## including the decimal place and y is the number of digits after\n", + "## the decimal. The 0 instructs the formatting command to prepend 0s\n", + "## to the number to get the right number of digits (without it it inserts spaces)" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "executionInfo": { + "elapsed": 562, + "status": "ok", + "timestamp": 1571354409251, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AAuE7mBMnWy8dDR7jyTHNy9tPaRx6DCyA3QKrIcuQ7R4=s64", + "userId": "06399644931392855882" + }, + "user_tz": 420 + }, + "id": "EWKu3b0pKLkC", + "outputId": "dd0d4171-b215-4e0f-8530-813730e61b86", + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'+000.97'" + ] + }, + "execution_count": 54, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "f\"{x:+07.2f}\" # Add + indicates to print a sign (either + or -)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'The number is: 0.9745000'" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# We can also embed the number directly\n", + "\n", + "f\"The number is: {0.9745:.7f}\" # The same" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'The number is: 0.9745000'" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# You can also use the number formatting commands with the format method\n", + "\n", + "\"The number is: {:.7f}\".format(0.9745) # The same" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "For integers..." + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "executionInfo": { + "elapsed": 959, + "status": "ok", + "timestamp": 1571354441635, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AAuE7mBMnWy8dDR7jyTHNy9tPaRx6DCyA3QKrIcuQ7R4=s64", + "userId": "06399644931392855882" + }, + "user_tz": 420 + }, + "id": "XOOfIWhVJwUI", + "outputId": "6d9057b2-65f4-4a20-eea3-068752474ab9", + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'00657'" + ] + }, + "execution_count": 57, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x = 657\n", + "\n", + "f\"{x:05d}\" # the d indicates an integer\n", + "# the 05 says pad the integer with 0s to make it have at least 5 characters" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "source": [ + "There is a lot more to learn about formatting numbers for printing, so refer to the cheat sheet and play with the standard to learn it better." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "# Challenge 3" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0005.69\n" + ] + } + ], + "source": [ + "x = 5.6932\n", + "\n", + "# Write a command to print '0005.69'" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "nx4iryO296kf", + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# Type Conversion" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "executionInfo": { + "elapsed": 469, + "status": "ok", + "timestamp": 1569539824065, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "", + "userId": "02468802669323291213" + }, + "user_tz": 420 + }, + "id": "6wtzEpU6hH4D", + "outputId": "47fd552f-33fd-44cf-c5b4-5f91d608fdd7", + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "int" + ] + }, + "execution_count": 9, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "# It is useful to know that int, float and string types can be cross converted\n", + "\n", + "type( int('5') ) # This int() turns the string into an integer " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "executionInfo": { + "elapsed": 486, + "status": "ok", + "timestamp": 1569539847816, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "", + "userId": "02468802669323291213" + }, + "user_tz": 420 + }, + "id": "3_CzAiTvhzXQ", + "outputId": "748b4210-804e-404c-b4b0-ba24889cb259", + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "str" + ] + }, + "execution_count": 10, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "type( str(5) ) # We can also go back the other way" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 166 + }, + "executionInfo": { + "elapsed": 466, + "status": "error", + "timestamp": 1569539868997, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "", + "userId": "02468802669323291213" + }, + "user_tz": 420 + }, + "id": "7galkn84iCsf", + "outputId": "ed75be1f-2c32-429e-a29a-3f5bb02eaf45", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "ename": "ValueError", + "evalue": "ignored", + "output_type": "error", + "traceback": [ + "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m", + "\u001B[0;31mValueError\u001B[0m Traceback (most recent call last)", + "\u001B[0;32m\u001B[0m in \u001B[0;36m\u001B[0;34m()\u001B[0m\n\u001B[0;32m----> 1\u001B[0;31m \u001B[0mint\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0;34m\"foo\"\u001B[0m\u001B[0;34m)\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0m", + "\u001B[0;31mValueError\u001B[0m: invalid literal for int() with base 10: 'foo'" + ] + } + ], + "source": [ + "# Of course, this doesn't work because int() doesn't know how\n", + "# to interpret 'foo'\n", + "int(\"foo\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "executionInfo": { + "elapsed": 535, + "status": "ok", + "timestamp": 1569539921108, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "", + "userId": "02468802669323291213" + }, + "user_tz": 420 + }, + "id": "b-su-iJhiVUg", + "outputId": "f5aef519-2a67-447a-b758-0e95e30ad25d", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "5" + ] + }, + "execution_count": 12, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "# We can also convert between floats and ints, and \n", + "# dropping the decimal part as we go float --> int\n", + "int(5.999)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "executionInfo": { + "elapsed": 497, + "status": "ok", + "timestamp": 1569539932138, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "", + "userId": "02468802669323291213" + }, + "user_tz": 420 + }, + "id": "z6ISL0_Hif7J", + "outputId": "ae78ec2d-ec9a-4f19-cfde-5368aa9e7771", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "5.0" + ] + }, + "execution_count": 13, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "float(5)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "MiqSwuSkggbP", + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# Automatic Type Conversion" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 457, + "status": "ok", + "timestamp": 1607393155959, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "-tRC_KvLQznp", + "outputId": "0af55f0a-757a-4b36-a73b-93e00d1358cb", + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "int" + ] + }, + "execution_count": 6, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "# Remember everything in Python has a type, discoverable with \"type()\"\n", + "\n", + "# What's the type of x?\n", + "x = 6\n", + "\n", + "type(x)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 383, + "status": "ok", + "timestamp": 1607393157453, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "PZLtFsLwQ1Xk", + "outputId": "7abf453e-57db-4be9-8fc4-0e70c1356a04", + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "int" + ] + }, + "execution_count": 7, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "y = 5\n", + "\n", + "type(y)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 383, + "status": "ok", + "timestamp": 1607393158678, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "vvlSITbvQmp8", + "outputId": "c0c32394-6952-46c9-c01b-1630872dabc9", + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "int" + ] + }, + "execution_count": 8, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "# So what's the type of our expression result?\n", + "\n", + "type(x // y)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 386, + "status": "ok", + "timestamp": 1607393163405, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "fL7D8cPVQu7T", + "outputId": "b7289a02-c419-4530-95d8-2e842547dff3", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "float" + ] + }, + "execution_count": 9, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "type(x / y) # And this one ?" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 452, + "status": "ok", + "timestamp": 1607393165002, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "ak9CmTIJREvA", + "outputId": "33470a33-d35a-47af-a912-a653813f78d9", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "float" + ] + }, + "execution_count": 10, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "type(x + 0.0) # Python will generally automatically switch\n", + "# into floating point to avoid losing the decimal" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "VNemVpofRKmr", + "slideshow": { + "slide_type": "fragment" + } + }, + "source": [ + "In general, Python will try to convert into floating point if needed. However, you can use float() and int() to make it do what you want." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "# Challenge 4" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "# What is the type and value of?:\n", + "5 % 2\n", + "# And ?\n", + "5.6 % 2" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "GFggsPxW6lqW", + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# More Input" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 2440, + "status": "ok", + "timestamp": 1607393280148, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "Kbsy1ieS6lqY", + "outputId": "5c1b3879-f9c3-4c8e-b153-bb66d6e75229", + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "What's your name? Benedict\n", + "Hello Benedict\n" + ] + } + ], + "source": [ + "# Recall:\n", + "\n", + "name = input(\"What's your name? \")\n", + "\n", + "print(\"Hello\", name)" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 2705, + "status": "ok", + "timestamp": 1610495174610, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "olkcXHHz6lqg", + "outputId": "5efa2d35-6c2f-4f23-a0bd-265c3507cd40", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter a number: 5\n", + "Enter another number: 10\n", + "The sum of your two numbers is: 15\n" + ] + } + ], + "source": [ + "# Input always creates a string.\n", + "\n", + "# We can use type conversion to create numbers\n", + "\n", + "value1 = int(input(\"Enter a number: \")) # Read a first number\n", + "# from the user\n", + "\n", + "value2 = int(input(\"Enter another number: \")) # Read a second number\n", + "# from the user\n", + "\n", + "print(\"The sum of your two numbers is: \", value1 + value2)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "V4a91aOq6lql", + "slideshow": { + "slide_type": "fragment" + } + }, + "source": [ + "* Note: input(), like print(), str(), int(), type() is an example of a function, we'll come back to how these work soon" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "# Challenge 5" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter a number: 5\n", + "Enter another number: 10\n", + "The sum of your two numbers is: 15\n" + ] + } + ], + "source": [ + "# Adapt the below program to read in floating point numbers:\n", + "\n", + "value1 = int(input(\"Enter a number: \")) # Read a first number\n", + "# from the user\n", + "\n", + "value2 = int(input(\"Enter another number: \")) # Read a second number\n", + "# from the user\n", + "\n", + "print(\"The sum of your two numbers is: \", value1 + value2)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "MID_5W3JHkY8", + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# List Basics\n", + "\n", + "Python lists are sequences of objects" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "7jpmuhZ2H9Mj", + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "x = [ 1, 8, \"hello\"] # Python lists are sequences of objects\n", + "\n", + "# They are specified using this square bracket notation" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 394, + "status": "ok", + "timestamp": 1610495395337, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "L404R-wpIJNB", + "outputId": "d53d4067-5f3c-4142-c3e4-6580d28b6143", + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 8, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "len(x) # The length of the list" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 338, + "status": "ok", + "timestamp": 1607393851269, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "P8ZaQvcFIMJn", + "outputId": "5b08d31e-3b34-4d5a-9072-d171382ed479", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 8, 'hello']\n" + ] + } + ], + "source": [ + "print(x) # Printing a list shows it contents" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 338, + "status": "ok", + "timestamp": 1607393852471, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "z7kWX_iuIXnf", + "outputId": "e20c4700-f52f-4208-ea21-2c018f108ede", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 23, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "x[0] # The first element of the list, Python lists are INDEXED FROM 0 " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 326, + "status": "ok", + "timestamp": 1607393853731, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "4zxPY5pvIdcT", + "outputId": "cd79b1fd-379a-449b-99f7-0327fe4c87ed", + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "8" + ] + }, + "execution_count": 24, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "x[1] # The second" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "executionInfo": { + "elapsed": 353, + "status": "ok", + "timestamp": 1607393855019, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "j5sUzBpxIfJy", + "outputId": "b36d07aa-349f-47c7-82e3-865c3d510fc3", + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "string" + }, + "text/plain": [ + "'hello'" + ] + }, + "execution_count": 25, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "x[2] # The third" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 343, + "status": "ok", + "timestamp": 1607393856573, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "Mal2A87YImp2", + "outputId": "cb527017-7c8a-45e6-9c17-e52eb6d5f9cc", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Before [1, 8, 'hello']\n", + "After [2, 8, 'hello']\n" + ] + } + ], + "source": [ + "print(\"Before\", x)\n", + "\n", + "x[0] = 2 # Replace a member of the list \n", + "\n", + "print(\"After\", x)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 317, + "status": "ok", + "timestamp": 1607393907915, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "z8lM2V7EI0VX", + "outputId": "a3901eaf-2ac7-4a51-b009-260a213c9549", + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "['__add__',\n", + " '__class__',\n", + " '__contains__',\n", + " '__delattr__',\n", + " '__delitem__',\n", + " '__dir__',\n", + " '__doc__',\n", + " '__eq__',\n", + " '__format__',\n", + " '__ge__',\n", + " '__getattribute__',\n", + " '__getitem__',\n", + " '__gt__',\n", + " '__hash__',\n", + " '__iadd__',\n", + " '__imul__',\n", + " '__init__',\n", + " '__init_subclass__',\n", + " '__iter__',\n", + " '__le__',\n", + " '__len__',\n", + " '__lt__',\n", + " '__mul__',\n", + " '__ne__',\n", + " '__new__',\n", + " '__reduce__',\n", + " '__reduce_ex__',\n", + " '__repr__',\n", + " '__reversed__',\n", + " '__rmul__',\n", + " '__setattr__',\n", + " '__setitem__',\n", + " '__sizeof__',\n", + " '__str__',\n", + " '__subclasshook__',\n", + " 'append',\n", + " 'clear',\n", + " 'copy',\n", + " 'count',\n", + " 'extend',\n", + " 'index',\n", + " 'insert',\n", + " 'pop',\n", + " 'remove',\n", + " 'reverse',\n", + " 'sort']" + ] + }, + "execution_count": 28, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "dir(x) # Lists have many other features, we'll visit them again soon\n", + "\n", + "# dir() is a builtin function we can use to discover the variables \n", + "# and methods of a Python object - the \"__\" names look strange here, \n", + "# we'll figure these out later" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "# Challenge 6" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3]\n" + ] + } + ], + "source": [ + "x = [ \"1\", \"2\", \"3\" ]\n", + "# Convert each member of the list to an integer\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "VUnSbmkzyKwd", + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# Dictionary Basics\n", + "\n", + "Dictionaries are \"maps\" (think like a function) from a set of keys (domain) to a set of values (codomain)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "Fv6ucZiFYbG9", + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "english_to_spanish = {\"two\": \"dos\", \"one\": \"uno\", \"three\":\"tres\"}\n", + "\n", + "fruit_costs = {\"apples\": 430, \"bananas\": 312, \"oranges\": 525, \"pears\": 217}" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "L2INsf0OhGkP", + "slideshow": { + "slide_type": "fragment" + } + }, + "source": [ + "* Dictionaries are defined by curly braces {}\n", + "\n", + "* They are a collection of key:value pairs, each pair separated by a comma.\n", + "\n", + "* Keys and values can be most Python objects, e.g: { 1:\"hello\", \"two\":True }\n", + "\n", + "* You look up elements in a dictionary using square bracket notation:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "executionInfo": { + "elapsed": 1128, + "status": "ok", + "timestamp": 1606701189291, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "0tE3-3TZiLY1", + "outputId": "42f08720-81ae-49cc-f542-c6d921639555", + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "string" + }, + "text/plain": [ + "'uno'" + ] + }, + "execution_count": 2, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "english_to_spanish[\"one\"]" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "cuONFTaehZ9M", + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "**No duplicate keys**: You can't have duplicate keys in a dictionary:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 1125, + "status": "ok", + "timestamp": 1606701189292, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "4vxBbOTbysDq", + "outputId": "2bfac3c3-5d17-47e1-bdb8-3300eecd2638", + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{1: 6, 3: 4}\n" + ] + } + ], + "source": [ + "a = { 1:2, 3:4, 1:6 } # Don't expect this to out work well\n", + "\n", + "print(a) " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "a8MVbh8f3VB0", + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "**Duplicate values are allowed**:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 1122, + "status": "ok", + "timestamp": 1606701189292, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "qJOxmZ1p3X-A", + "outputId": "0f3af977-2a7f-4c9f-8331-179e205d95a2", + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{1: 1, 3: 4, 5: 1}\n" + ] + } + ], + "source": [ + "a = { 1:1, 3:4, 5:1 } \n", + "\n", + "print(a) " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "9MdtD4pgy89t", + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "**Dictionaries are mutable:**" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 1119, + "status": "ok", + "timestamp": 1606701189293, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "0x2AWL4EzBlb", + "outputId": "34eeedea-26de-48fc-a3b1-2e6de1515df7", + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{1: 'hello', 3: 4, 5: 1}\n" + ] + } + ], + "source": [ + "a[1] = \"hello\"\n", + "\n", + "print(a)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 1115, + "status": "ok", + "timestamp": 1606701189293, + "user": { + "displayName": "Benedict Paten", + "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", + "userId": "06399644931392855882" + }, + "user_tz": 480 + }, + "id": "OSUCX1d9zHpM", + "outputId": "7cf2d42f-0058-4a84-cf69-59b4ff5e43eb", + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{1: 'hello', 3: 4, 5: 1, 'a new key': 9}\n" + ] + } + ], + "source": [ + "a[\"a new key\"] = 9\n", + "\n", + "print(a)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "# Challenge 7" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "# Make a dictionary mapping the integers 1, 2, ..., 5 to their cubes (1, 8, 27, ...)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "KZP9y5sgyVm8", + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# Homework\n", + "\n", + "* Go to Canvas and complete the third lecture quiz, which involves completing each challenge problem\n", + "* ZyBook: Reading 3\n" + ] + } + ], + "metadata": { + "celltoolbar": "Slideshow", + "colab": { + "collapsed_sections": [ + "S4CkZnM_4FvB", + "odfPyGzkdUIJ", + "JPxEMRO9bFgc", + "nx4iryO296kf", + "MiqSwuSkggbP", + "GFggsPxW6lqW", + "MID_5W3JHkY8", + "VUnSbmkzyKwd" + ], + "name": "L03 More Types", + "provenance": [] + }, + "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.15" + }, + "vscode": { + "interpreter": { + "hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49" + } + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} From d9f9706cf8137a9fd92ee1f8fc26497b30d90c6c Mon Sep 17 00:00:00 2001 From: crimpmug <113626525+crimpmug@users.noreply.github.com> Date: Wed, 18 Jan 2023 15:36:15 -0800 Subject: [PATCH 6/7] Delete L03 More Types.ipynb --- lecture_notebooks/L03 More Types.ipynb | 2820 ------------------------ 1 file changed, 2820 deletions(-) delete mode 100644 lecture_notebooks/L03 More Types.ipynb diff --git a/lecture_notebooks/L03 More Types.ipynb b/lecture_notebooks/L03 More Types.ipynb deleted file mode 100644 index d9472b3..0000000 --- a/lecture_notebooks/L03 More Types.ipynb +++ /dev/null @@ -1,2820 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "id": "S4CkZnM_4FvB", - "slideshow": { - "slide_type": "slide" - } - }, - "source": [ - "# Lecture 3 - More Types (https://bit.ly/intro_python_03)\n", - "\n", - "Today:\n", - "* More strings\n", - "* F strings and number formatting\n", - "* Type Conversions\n", - "* Automatic Type Conversion\n", - "* More Input\n", - "* List basics\n", - "* Dictionary Basics" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "odfPyGzkdUIJ", - "slideshow": { - "slide_type": "slide" - } - }, - "source": [ - "# More Strings\n", - "\n", - "In Python3.0 strings are sequences of symbols from an alphabet called Unicode: https://unicode.org/ \n", - "\n", - "* Unicode allows a very wide range of symbols for many languages and encodings. Much more than ASCII (http://www.asciitable.com/), which is what Python used to use. This requires more memory to store the strings, but prevents weird syntax translation issues." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "qEy9bWvx5QHt", - "slideshow": { - "slide_type": "subslide" - } - }, - "source": [ - "![alt text](https://pediaa.com/wp-content/uploads/2018/07/Difference-Between-ASCII-and-Unicode-Comparison-Summary.jpg)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "slideshow": { - "slide_type": "slide" - } - }, - "source": [ - "# String Syntax" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - }, - "executionInfo": { - "elapsed": 584, - "status": "ok", - "timestamp": 1569539469038, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "", - "userId": "02468802669323291213" - }, - "user_tz": 420 - }, - "id": "z5hWOCXYcrFt", - "outputId": "694e6e4c-17e5-429d-d48a-8bc0552cff97", - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "str" - ] - }, - "execution_count": 6, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "# Strings can be expressed a bunch of ways:\n", - "\n", - "type(\"hello\") # Double quotes" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - }, - "executionInfo": { - "elapsed": 693, - "status": "ok", - "timestamp": 1554323886402, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh6.googleusercontent.com/-KEXp34y2Ra8/AAAAAAAAAAI/AAAAAAAAAHs/xzPsOsCpcaU/s64/photo.jpg", - "userId": "06399644931392855882" - }, - "user_tz": 420 - }, - "id": "13L6yoJ2f5_F", - "outputId": "17e82f98-e82e-4f31-e2a0-3bc2c856471c", - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "str" - ] - }, - "execution_count": 7, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "type('hello') # Single quotes" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - }, - "executionInfo": { - "elapsed": 378, - "status": "ok", - "timestamp": 1554323904826, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh6.googleusercontent.com/-KEXp34y2Ra8/AAAAAAAAAAI/AAAAAAAAAHs/xzPsOsCpcaU/s64/photo.jpg", - "userId": "06399644931392855882" - }, - "user_tz": 420 - }, - "id": "xG8NQ3iLf7m4", - "outputId": "cdf6c8d7-bfc9-46e3-8cb2-f8b64a39fc59", - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "str" - ] - }, - "execution_count": 8, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "type(\"\"\"hello\"\"\") # Triple double quotes" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - }, - "executionInfo": { - "elapsed": 539, - "status": "ok", - "timestamp": 1554324028562, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh6.googleusercontent.com/-KEXp34y2Ra8/AAAAAAAAAAI/AAAAAAAAAHs/xzPsOsCpcaU/s64/photo.jpg", - "userId": "06399644931392855882" - }, - "user_tz": 420 - }, - "id": "ospXwmAEf9cn", - "outputId": "13ca371a-b6ce-48f6-afcc-3df1628a2531", - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "str" - ] - }, - "execution_count": 14, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "type('''hello''') # Triple single quotes" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "ND2UHQJkgCCH", - "slideshow": { - "slide_type": "subslide" - } - }, - "source": [ - "We have these different syntax so we can embed quotes and whitespace (tabs, spaces and new lines) in strings. " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - }, - "executionInfo": { - "elapsed": 437, - "status": "ok", - "timestamp": 1554324081443, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh6.googleusercontent.com/-KEXp34y2Ra8/AAAAAAAAAAI/AAAAAAAAAHs/xzPsOsCpcaU/s64/photo.jpg", - "userId": "06399644931392855882" - }, - "user_tz": 420 - }, - "id": "_kq0Ugs2Gc8_", - "outputId": "3ec14f59-7aa5-44c7-8d0d-474b02e1450f", - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "str" - ] - }, - "execution_count": 15, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "# We can embed single quotes in double quotes:\n", - "type(\"This is a string containing a 'single quoted substring'\")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 130 - }, - "executionInfo": { - "elapsed": 501, - "status": "error", - "timestamp": 1554324108166, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh6.googleusercontent.com/-KEXp34y2Ra8/AAAAAAAAAAI/AAAAAAAAAHs/xzPsOsCpcaU/s64/photo.jpg", - "userId": "06399644931392855882" - }, - "user_tz": 420 - }, - "id": "J5UDSxu6gnm8", - "outputId": "b27a3768-0b1f-4524-c772-bee9b7a8b837", - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "ename": "SyntaxError", - "evalue": "ignored", - "output_type": "error", - "traceback": [ - "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m type('This is a string containing a 'single quoted substring'')\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" - ] - } - ], - "source": [ - "# Trying to embed single quotes \n", - "# in a single quoted string naively does not work\n", - "type('This is a string containing a 'single quoted substring'')" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - }, - "executionInfo": { - "elapsed": 432, - "status": "ok", - "timestamp": 1554324181369, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh6.googleusercontent.com/-KEXp34y2Ra8/AAAAAAAAAAI/AAAAAAAAAHs/xzPsOsCpcaU/s64/photo.jpg", - "userId": "06399644931392855882" - }, - "user_tz": 420 - }, - "id": "nvgVKUi2g7yI", - "outputId": "49f5539f-bdd5-4965-c235-f0fe8bea4316", - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "str" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# And vice-versa, we can embed double quotes in single quotes:\n", - "type('This is a string containing a \"double quoted substring\"')" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 131 - }, - "executionInfo": { - "elapsed": 1099, - "status": "error", - "timestamp": 1569539635155, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "", - "userId": "02468802669323291213" - }, - "user_tz": 420 - }, - "id": "mZ-JcBF9jkay", - "outputId": "aac1e383-898b-4b0f-95a0-a5592e4b20c3", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "ename": "SyntaxError", - "evalue": "ignored", - "output_type": "error", - "traceback": [ - "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m type('This is an\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m EOL while scanning string literal\n" - ] - } - ], - "source": [ - "# Single and double quoted strings can't span multiple lines:\n", - "type('This is an \n", - " attempt to have a string on two lines')" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - }, - "executionInfo": { - "elapsed": 417, - "status": "ok", - "timestamp": 1554324236518, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh6.googleusercontent.com/-KEXp34y2Ra8/AAAAAAAAAAI/AAAAAAAAAHs/xzPsOsCpcaU/s64/photo.jpg", - "userId": "06399644931392855882" - }, - "user_tz": 420 - }, - "id": "tPVSUn-biK47", - "outputId": "d6dda436-f9f3-4bb3-dd71-5213c27942b7", - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "str" - ] - }, - "execution_count": 19, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "# Triple quotes (either single or double), allow embedding essentially anything, \n", - "# including spanning multiple lines:\n", - "\n", - "type(\"\"\"Boom, now\n", - " we can make crazy strings that ' nest quotes in quotes \" \" ' etc.\n", - " and which span multiple lines\"\"\")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 54 - }, - "executionInfo": { - "elapsed": 850, - "status": "ok", - "timestamp": 1554324365093, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh6.googleusercontent.com/-KEXp34y2Ra8/AAAAAAAAAAI/AAAAAAAAAHs/xzPsOsCpcaU/s64/photo.jpg", - "userId": "06399644931392855882" - }, - "user_tz": 420 - }, - "id": "2NARKErpkhsr", - "outputId": "062c975d-aed6-4de7-c18b-4be0692e3bc8", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "\" \\nA comment string. \\n\\nThese can be used to document some code\\nacross multiple lines. We'll later see this at the top\\nof classes and functions as a standard way of giving\\na description string.\\n\"" - ] - }, - "execution_count": 20, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "# You will also see this a lot in Python code:\n", - "\n", - "\"\"\" \n", - "A comment string. \n", - "\n", - "These can be used to document some code\n", - "across multiple lines. We'll later see this at the top\n", - "of classes and functions as a standard way of giving\n", - "a description string.\n", - "\"\"\"\n" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "slideshow": { - "slide_type": "subslide" - } - }, - "source": [ - "# Challenge 1" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [], - "source": [ - "# Create a string containing single quotes, double quotes and which spans multiple lines" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "JPxEMRO9bFgc", - "slideshow": { - "slide_type": "slide" - } - }, - "source": [ - "# Whitespace Characters\n", - "\n", - "* There is a difference between the contents of a Python string you type (termed a string literal) and what gets printed to the screen.\n", - "\n", - "* In general, Python and most other programming languages use reserved ASCII or unicode characters to encode whitespace in string literals. \n", - "\n", - "* Lets cover tabs and newlines" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - }, - "executionInfo": { - "elapsed": 872, - "status": "ok", - "timestamp": 1571351313722, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AAuE7mBMnWy8dDR7jyTHNy9tPaRx6DCyA3QKrIcuQ7R4=s64", - "userId": "06399644931392855882" - }, - "user_tz": 420 - }, - "id": "lwuNwoa1kPb1", - "outputId": "f77eb9ac-60af-4b1b-c933-4c4a1e47eb17", - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "This\tis\ttab\tseparated\n" - ] - } - ], - "source": [ - "s = \"This\\tis\\ttab\\tseparated\" # Tabs are encoded with '\\t' \n", - "\n", - "print(s)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 87 - }, - "executionInfo": { - "elapsed": 484, - "status": "ok", - "timestamp": 1571351375970, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AAuE7mBMnWy8dDR7jyTHNy9tPaRx6DCyA3QKrIcuQ7R4=s64", - "userId": "06399644931392855882" - }, - "user_tz": 420 - }, - "id": "-S3ED-nHk4uA", - "outputId": "12ed2be0-97e4-4141-dcb7-37cf24e25d10", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "This\n", - "is\n", - "newline\n", - "separated\n" - ] - } - ], - "source": [ - "s = \"This\\nis\\nnewline\\nseparated\" # Newline characters: '\\n' specify new lines\n", - "\n", - "print(s)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - }, - "executionInfo": { - "elapsed": 929, - "status": "ok", - "timestamp": 1571351438223, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AAuE7mBMnWy8dDR7jyTHNy9tPaRx6DCyA3QKrIcuQ7R4=s64", - "userId": "06399644931392855882" - }, - "user_tz": 420 - }, - "id": "UlraTeHQlUvU", - "outputId": "b42aef5a-099a-4c51-e513-4711fc29a452", - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "True\n" - ] - } - ], - "source": [ - "s2 = \"\"\"This\n", - "is\n", - "newline\n", - "separated\"\"\" # This is the equivalent way of specifying s by using \n", - "# explicit white space in your code\n", - "\n", - "print(s == s2)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 87 - }, - "executionInfo": { - "elapsed": 476, - "status": "ok", - "timestamp": 1571351461721, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AAuE7mBMnWy8dDR7jyTHNy9tPaRx6DCyA3QKrIcuQ7R4=s64", - "userId": "06399644931392855882" - }, - "user_tz": 420 - }, - "id": "JYS9BkO6lg2I", - "outputId": "9dbc2e01-4047-4bea-c45f-b1dd45974af4", - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "This\n", - "is\n", - "newline\n", - "separated\n" - ] - } - ], - "source": [ - "print(s)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 87 - }, - "executionInfo": { - "elapsed": 485, - "status": "ok", - "timestamp": 1571351463892, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AAuE7mBMnWy8dDR7jyTHNy9tPaRx6DCyA3QKrIcuQ7R4=s64", - "userId": "06399644931392855882" - }, - "user_tz": 420 - }, - "id": "p1PTjHmMliDv", - "outputId": "dc867dbb-f667-4b5a-9039-a2f1b089c974", - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "This\n", - "is\n", - "newline\n", - "separated\n" - ] - } - ], - "source": [ - "print(s2)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 69 - }, - "executionInfo": { - "elapsed": 874, - "status": "ok", - "timestamp": 1571351511757, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AAuE7mBMnWy8dDR7jyTHNy9tPaRx6DCyA3QKrIcuQ7R4=s64", - "userId": "06399644931392855882" - }, - "user_tz": 420 - }, - "id": "Wk43tEb0l550", - "outputId": "b9239e54-5105-472e-de52-6ae4ddd5092d", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Welcome to my program.\n", - "\tHere is an indent.\n", - "\t\tHere is a double indent\n" - ] - } - ], - "source": [ - "# It is often convenient to use tab and newline characters in your string \n", - "# formatting to make the strings do what you want, e.g.\n", - "\n", - "print(\"Welcome to my program.\\n\\tHere is an indent.\\n\\t\\tHere is a double indent\")" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "Mdhal66NmQHu", - "slideshow": { - "slide_type": "fragment" - } - }, - "source": [ - "Doing the above with triple quoted strings is less compact, and generally more error prone,\n", - "because the way the editor encodes tabs depends on how the code formatting is setup:\n", - "e.g. are blocks of spaces tabs? how many spaces? etc." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 69 - }, - "executionInfo": { - "elapsed": 626, - "status": "ok", - "timestamp": 1571351574595, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AAuE7mBMnWy8dDR7jyTHNy9tPaRx6DCyA3QKrIcuQ7R4=s64", - "userId": "06399644931392855882" - }, - "user_tz": 420 - }, - "id": "MqDnWyC_mXwN", - "outputId": "66396915-41fa-4076-c1e9-ccdbaae24efa", - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Welcome to my program.\n", - " Here is an indent.\n", - " Here is a double indent\n" - ] - } - ], - "source": [ - "print(\"\"\"Welcome to my program.\n", - " Here is an indent.\n", - " Here is a double indent\"\"\")" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "slideshow": { - "slide_type": "subslide" - } - }, - "source": [ - "# Escape Characters\n", - "\n", - "**The use of \"\\t\" and \"\\n\" are examples of escape characters.**\n", - "\n", - "* In general, the **backslash** “\\” or the “**escape**” character is used to insert whitespace character into strings. \n", - "* Some of these include...\n", - "\n", - " 1. \\t - Horizontal tab\n", - " 2. \\v - Vertical tab\n", - " 3. \\n - Newline\n", - " 4. \\r - Carriage return\n", - " 5. \\f - Feed\n", - " #\n", - " \n", - "(As an aside, I never use the vertical tab, feed or carriage return, but YMMV) \n", - " \n", - " \n", - "* The “***escape***” character can also be used to utilize special characters when using strings\n", - "* Some of these include (and are useful to remember):\n", - "\n", - " 1. \\\\' - Single Quote\n", - " 2. \\\\\\ - Backslash\n" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": { - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "This is a string with a double quote in it: \", and a single quote: ' and a backslash \\n \n" - ] - } - ], - "source": [ - "# Use escape characters to include the symbols you want in a string:\n", - "\n", - "x = \"This is a string with a double quote in it: \\\", and a single quote: \\' and a backslash with an n \\\\n \"\n", - "\n", - "print(x)" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": { - "slideshow": { - "slide_type": "notes" - } - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "This is a string with a \r", - " carriage return\n" - ] - } - ], - "source": [ - "# Using carriage return to write over what was already printed!\n", - "\n", - "print(\"This is a string with a \\r carriage return\")" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "slideshow": { - "slide_type": "subslide" - } - }, - "source": [ - "# Challenge 2" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [], - "source": [ - "# Rewrite the following string to use single quotes and escape characters\n", - "\"\"\"this is\n", - "a contrived example\n", - " to test \n", - " you!\"\"\"" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "pycharm": { - "name": "#%% md\n" - }, - "slideshow": { - "slide_type": "slide" - } - }, - "source": [ - "# F-strings" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "slideshow": { - "slide_type": "fragment" - } - }, - "source": [ - "Often we want to substitute values into strings. Python's f-strings are a nice way to do this (we'll see other ways to do this later)" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": { - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "I went to 10 Downing St, London, SW1A, UK to see the prime minister\n" - ] - } - ], - "source": [ - "# Suppose we have these two variables\n", - "\n", - "address=\"10 Downing St, London, SW1A, UK\"\n", - "person=\"prime minister\"\n", - "\n", - "# We can introduce them into a f-string as follows\n", - "print(f\"I went to {address} to see the {person}\")" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "pycharm": { - "name": "#%%\n" - }, - "slideshow": { - "slide_type": "fragment" - } - }, - "source": [ - "F-strings were introduced in Python 3.6. They provide a convenient alternative to the format method (see below)" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": { - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "I went to 10 Downing St, London, SW1A, UK to see the prime minister\n", - "I went to 10 Downing St, London, SW1A, UK to see the prime minister\n", - "I went to 10 Downing St, London, SW1A, UK to see the prime minister\n", - "I went to 10 Downing St, London, SW1A, UK to see the prime minister\n" - ] - } - ], - "source": [ - "# The general syntax allows either F or f as the prefix. \n", - "# They work with single quotes, double quotes and triple quotes, just as with vanilla strings\n", - "\n", - "print(f\"I went to {address} to see the {person}\")\n", - "print(F\"I went to {address} to see the {person}\")\n", - "print(f'I went to {address} to see the {person}')\n", - "print(F\"\"\"I went to {address} to see the {person}\"\"\")\n", - "# etc..." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": { - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "This is math: 25, and this is string multiplication: hellohello\n" - ] - } - ], - "source": [ - "# You can embed arbitrary expressions within the f-string:\n", - "print(f\"This is math: { 5 * 5 }, and this is string multiplication: { 'hello' * 2 }\")" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "slideshow": { - "slide_type": "fragment" - } - }, - "source": [ - "Note in the above example we must embed single quoted strings in the double quoted string." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "SfuZ30qgzSDd", - "slideshow": { - "slide_type": "slide" - } - }, - "source": [ - "# Formatting Numbers\n", - "\n", - "It is important to be able to control the format of numbers during printing!\n", - "\n", - "This is one of those things I always have to look up to get right, this table is useful as a cheat sheet:\n", - "\n", - "https://mkaz.blog/code/python-string-format-cookbook/" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - }, - "executionInfo": { - "elapsed": 615, - "status": "ok", - "timestamp": 1571354377395, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AAuE7mBMnWy8dDR7jyTHNy9tPaRx6DCyA3QKrIcuQ7R4=s64", - "userId": "06399644931392855882" - }, - "user_tz": 420 - }, - "id": "Kej77oYdI540", - "outputId": "555063a7-033f-4690-98b0-18e43461d0ae", - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'The number is: 0.9496503'" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "x = 0.9745\n", - "\n", - "f\"The number is: {x ** 2:.7f}\" # The : indicates that you're issuing a formatting command\n", - "# the .7 says you want to seven decimal places, \n", - "# f says the thing you're printing is a float" - ] - }, - { - "cell_type": "code", - "execution_count": 52, - "metadata": { - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'The number is: 00.975'" - ] - }, - "execution_count": 52, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# You can control the total number of digits printed as follows:\n", - "\n", - "f\"The number is: {x:06.3f}\" # The same\n", - "\n", - "## Here {:0x.yf} x (the 6) is the total number of symbols to be printed \n", - "## including the decimal place and y is the number of digits after\n", - "## the decimal. The 0 instructs the formatting command to prepend 0s\n", - "## to the number to get the right number of digits (without it it inserts spaces)" - ] - }, - { - "cell_type": "code", - "execution_count": 54, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - }, - "executionInfo": { - "elapsed": 562, - "status": "ok", - "timestamp": 1571354409251, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AAuE7mBMnWy8dDR7jyTHNy9tPaRx6DCyA3QKrIcuQ7R4=s64", - "userId": "06399644931392855882" - }, - "user_tz": 420 - }, - "id": "EWKu3b0pKLkC", - "outputId": "dd0d4171-b215-4e0f-8530-813730e61b86", - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'+000.97'" - ] - }, - "execution_count": 54, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "f\"{x:+07.2f}\" # Add + indicates to print a sign (either + or -)" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": { - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'The number is: 0.9745000'" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# We can also embed the number directly\n", - "\n", - "f\"The number is: {0.9745:.7f}\" # The same" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": { - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'The number is: 0.9745000'" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# You can also use the number formatting commands with the format method\n", - "\n", - "\"The number is: {:.7f}\".format(0.9745) # The same" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "slideshow": { - "slide_type": "subslide" - } - }, - "source": [ - "For integers..." - ] - }, - { - "cell_type": "code", - "execution_count": 57, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - }, - "executionInfo": { - "elapsed": 959, - "status": "ok", - "timestamp": 1571354441635, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AAuE7mBMnWy8dDR7jyTHNy9tPaRx6DCyA3QKrIcuQ7R4=s64", - "userId": "06399644931392855882" - }, - "user_tz": 420 - }, - "id": "XOOfIWhVJwUI", - "outputId": "6d9057b2-65f4-4a20-eea3-068752474ab9", - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'00657'" - ] - }, - "execution_count": 57, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "x = 657\n", - "\n", - "f\"{x:05d}\" # the d indicates an integer\n", - "# the 05 says pad the integer with 0s to make it have at least 5 characters" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "slideshow": { - "slide_type": "fragment" - } - }, - "source": [ - "There is a lot more to learn about formatting numbers for printing, so refer to the cheat sheet and play with the standard to learn it better." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "slideshow": { - "slide_type": "subslide" - } - }, - "source": [ - "# Challenge 3" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": { - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "0005.69\n" - ] - } - ], - "source": [ - "x = 5.6932\n", - "\n", - "# Write a command to print '0005.69'" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "nx4iryO296kf", - "slideshow": { - "slide_type": "slide" - } - }, - "source": [ - "# Type Conversion" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - }, - "executionInfo": { - "elapsed": 469, - "status": "ok", - "timestamp": 1569539824065, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "", - "userId": "02468802669323291213" - }, - "user_tz": 420 - }, - "id": "6wtzEpU6hH4D", - "outputId": "47fd552f-33fd-44cf-c5b4-5f91d608fdd7", - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "int" - ] - }, - "execution_count": 9, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "# It is useful to know that int, float and string types can be cross converted\n", - "\n", - "type( int('5') ) # This int() turns the string into an integer " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - }, - "executionInfo": { - "elapsed": 486, - "status": "ok", - "timestamp": 1569539847816, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "", - "userId": "02468802669323291213" - }, - "user_tz": 420 - }, - "id": "3_CzAiTvhzXQ", - "outputId": "748b4210-804e-404c-b4b0-ba24889cb259", - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "str" - ] - }, - "execution_count": 10, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "type( str(5) ) # We can also go back the other way" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 166 - }, - "executionInfo": { - "elapsed": 466, - "status": "error", - "timestamp": 1569539868997, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "", - "userId": "02468802669323291213" - }, - "user_tz": 420 - }, - "id": "7galkn84iCsf", - "outputId": "ed75be1f-2c32-429e-a29a-3f5bb02eaf45", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "ename": "ValueError", - "evalue": "ignored", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"foo\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mValueError\u001b[0m: invalid literal for int() with base 10: 'foo'" - ] - } - ], - "source": [ - "# Of course, this doesn't work because int() doesn't how\n", - "# to interpret 'foo'\n", - "int(\"foo\")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - }, - "executionInfo": { - "elapsed": 535, - "status": "ok", - "timestamp": 1569539921108, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "", - "userId": "02468802669323291213" - }, - "user_tz": 420 - }, - "id": "b-su-iJhiVUg", - "outputId": "f5aef519-2a67-447a-b758-0e95e30ad25d", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "5" - ] - }, - "execution_count": 12, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "# We can also convert between floats and ints, and \n", - "# dropping the decimal part as we go float --> int\n", - "int(5.999)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - }, - "executionInfo": { - "elapsed": 497, - "status": "ok", - "timestamp": 1569539932138, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "", - "userId": "02468802669323291213" - }, - "user_tz": 420 - }, - "id": "z6ISL0_Hif7J", - "outputId": "ae78ec2d-ec9a-4f19-cfde-5368aa9e7771", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "5.0" - ] - }, - "execution_count": 13, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "float(5)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "MiqSwuSkggbP", - "slideshow": { - "slide_type": "slide" - } - }, - "source": [ - "# Automatic Type Conversion" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 457, - "status": "ok", - "timestamp": 1607393155959, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "-tRC_KvLQznp", - "outputId": "0af55f0a-757a-4b36-a73b-93e00d1358cb", - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "int" - ] - }, - "execution_count": 6, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "# Remember everything in Python has a type, discoverable with \"type()\"\n", - "\n", - "# What's the type of x?\n", - "x = 6\n", - "\n", - "type(x)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 383, - "status": "ok", - "timestamp": 1607393157453, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "PZLtFsLwQ1Xk", - "outputId": "7abf453e-57db-4be9-8fc4-0e70c1356a04", - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "int" - ] - }, - "execution_count": 7, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "y = 5\n", - "\n", - "type(y)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 383, - "status": "ok", - "timestamp": 1607393158678, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "vvlSITbvQmp8", - "outputId": "c0c32394-6952-46c9-c01b-1630872dabc9", - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "int" - ] - }, - "execution_count": 8, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "# So what's the type of our expression result?\n", - "\n", - "type(x // y)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 386, - "status": "ok", - "timestamp": 1607393163405, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "fL7D8cPVQu7T", - "outputId": "b7289a02-c419-4530-95d8-2e842547dff3", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "float" - ] - }, - "execution_count": 9, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "type(x / y) # And this one ?" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 452, - "status": "ok", - "timestamp": 1607393165002, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "ak9CmTIJREvA", - "outputId": "33470a33-d35a-47af-a912-a653813f78d9", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "float" - ] - }, - "execution_count": 10, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "type(x + 0.0) # Python will generally automatically switch into floating point to avoid losing the decimal" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "VNemVpofRKmr", - "slideshow": { - "slide_type": "fragment" - } - }, - "source": [ - "In general, Python will try to convert into floating point if needed. However, you can use float() and int() to make it do what you want." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "slideshow": { - "slide_type": "subslide" - } - }, - "source": [ - "# Challenge 4" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [], - "source": [ - "# What is the type and value of?:\n", - "5 % 2\n", - "# And ?\n", - "5.6 % 2" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "GFggsPxW6lqW", - "slideshow": { - "slide_type": "slide" - } - }, - "source": [ - "# More Input" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 2440, - "status": "ok", - "timestamp": 1607393280148, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "Kbsy1ieS6lqY", - "outputId": "5c1b3879-f9c3-4c8e-b153-bb66d6e75229", - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "What's your name? Benedict\n", - "Hello Benedict\n" - ] - } - ], - "source": [ - "# Recall:\n", - "\n", - "name = input(\"What's your name? \")\n", - "\n", - "print(\"Hello\", name)" - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 2705, - "status": "ok", - "timestamp": 1610495174610, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "olkcXHHz6lqg", - "outputId": "5efa2d35-6c2f-4f23-a0bd-265c3507cd40", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Enter a number: 5\n", - "Enter another number: 10\n", - "The sum of your two numbers is: 15\n" - ] - } - ], - "source": [ - "# Input always creates a string.\n", - "\n", - "# We can use type conversion to create numbers\n", - "\n", - "value1 = int(input(\"Enter a number: \")) # Read a first number\n", - "# from the user\n", - "\n", - "value2 = int(input(\"Enter another number: \")) # Read a second number\n", - "# from the user\n", - "\n", - "print(\"The sum of your two numbers is: \", value1 + value2)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "V4a91aOq6lql", - "slideshow": { - "slide_type": "fragment" - } - }, - "source": [ - "* Note: input(), like print(), str(), int(), type() is an example of a function, we'll come back to how these work soon" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "slideshow": { - "slide_type": "subslide" - } - }, - "source": [ - "# Challenge 5" - ] - }, - { - "cell_type": "code", - "execution_count": 38, - "metadata": { - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Enter a number: 5\n", - "Enter another number: 10\n", - "The sum of your two numbers is: 15\n" - ] - } - ], - "source": [ - "# Adapt the below program to read in floating point numbers:\n", - "\n", - "value1 = int(input(\"Enter a number: \")) # Read a first number\n", - "# from the user\n", - "\n", - "value2 = int(input(\"Enter another number: \")) # Read a second number\n", - "# from the user\n", - "\n", - "print(\"The sum of your two numbers is: \", value1 + value2)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "MID_5W3JHkY8", - "slideshow": { - "slide_type": "slide" - } - }, - "source": [ - "# List Basics\n", - "\n", - "Python lists are sequences of objects" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "7jpmuhZ2H9Mj", - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [], - "source": [ - "x = [ 1, 8, \"hello\"] # Python lists are sequences of objects\n", - "\n", - "# They are specified using this square bracket notation" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 394, - "status": "ok", - "timestamp": 1610495395337, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "L404R-wpIJNB", - "outputId": "d53d4067-5f3c-4142-c3e4-6580d28b6143", - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "3" - ] - }, - "execution_count": 8, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "len(x) # The length of the list" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 338, - "status": "ok", - "timestamp": 1607393851269, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "P8ZaQvcFIMJn", - "outputId": "5b08d31e-3b34-4d5a-9072-d171382ed479", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 8, 'hello']\n" - ] - } - ], - "source": [ - "print(x) # Printing a list shows it contents" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 338, - "status": "ok", - "timestamp": 1607393852471, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "z7kWX_iuIXnf", - "outputId": "e20c4700-f52f-4208-ea21-2c018f108ede", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "1" - ] - }, - "execution_count": 23, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "x[0] # The first element of the list, Python lists are INDEXED FROM 0 " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 326, - "status": "ok", - "timestamp": 1607393853731, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "4zxPY5pvIdcT", - "outputId": "cd79b1fd-379a-449b-99f7-0327fe4c87ed", - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "8" - ] - }, - "execution_count": 24, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "x[1] # The second" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 35 - }, - "executionInfo": { - "elapsed": 353, - "status": "ok", - "timestamp": 1607393855019, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "j5sUzBpxIfJy", - "outputId": "b36d07aa-349f-47c7-82e3-865c3d510fc3", - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "data": { - "application/vnd.google.colaboratory.intrinsic+json": { - "type": "string" - }, - "text/plain": [ - "'hello'" - ] - }, - "execution_count": 25, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "x[2] # The third" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 343, - "status": "ok", - "timestamp": 1607393856573, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "Mal2A87YImp2", - "outputId": "cb527017-7c8a-45e6-9c17-e52eb6d5f9cc", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Before [1, 8, 'hello']\n", - "After [2, 8, 'hello']\n" - ] - } - ], - "source": [ - "print(\"Before\", x)\n", - "\n", - "x[0] = 2 # Replace a member of the list \n", - "\n", - "print(\"After\", x)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 317, - "status": "ok", - "timestamp": 1607393907915, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "z8lM2V7EI0VX", - "outputId": "a3901eaf-2ac7-4a51-b009-260a213c9549", - "slideshow": { - "slide_type": "subslide" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "['__add__',\n", - " '__class__',\n", - " '__contains__',\n", - " '__delattr__',\n", - " '__delitem__',\n", - " '__dir__',\n", - " '__doc__',\n", - " '__eq__',\n", - " '__format__',\n", - " '__ge__',\n", - " '__getattribute__',\n", - " '__getitem__',\n", - " '__gt__',\n", - " '__hash__',\n", - " '__iadd__',\n", - " '__imul__',\n", - " '__init__',\n", - " '__init_subclass__',\n", - " '__iter__',\n", - " '__le__',\n", - " '__len__',\n", - " '__lt__',\n", - " '__mul__',\n", - " '__ne__',\n", - " '__new__',\n", - " '__reduce__',\n", - " '__reduce_ex__',\n", - " '__repr__',\n", - " '__reversed__',\n", - " '__rmul__',\n", - " '__setattr__',\n", - " '__setitem__',\n", - " '__sizeof__',\n", - " '__str__',\n", - " '__subclasshook__',\n", - " 'append',\n", - " 'clear',\n", - " 'copy',\n", - " 'count',\n", - " 'extend',\n", - " 'index',\n", - " 'insert',\n", - " 'pop',\n", - " 'remove',\n", - " 'reverse',\n", - " 'sort']" - ] - }, - "execution_count": 28, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "dir(x) # Lists have many other features, we'll visit them again soon\n", - "\n", - "# dir() is a builtin function we can use to discover the variables \n", - "# and methods of a Python object - the \"__\" names look strange here, \n", - "# we'll figure these out later" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "slideshow": { - "slide_type": "subslide" - } - }, - "source": [ - "# Challenge 6" - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "metadata": { - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 2, 3]\n" - ] - } - ], - "source": [ - "x = [ \"1\", \"2\", \"3\" ]\n", - "# Convert each member of the list to an integer\n" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "VUnSbmkzyKwd", - "slideshow": { - "slide_type": "slide" - } - }, - "source": [ - "# Dictionary Basics\n", - "\n", - "Dictionaries are \"maps\" (think like a function) from a set of keys (domain) to a set of values (codomain)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "Fv6ucZiFYbG9", - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [], - "source": [ - "english_to_spanish = {\"two\": \"dos\", \"one\": \"uno\", \"three\":\"tres\"}\n", - "\n", - "fruit_costs = {\"apples\": 430, \"bananas\": 312, \"oranges\": 525, \"pears\": 217}" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "L2INsf0OhGkP", - "slideshow": { - "slide_type": "fragment" - } - }, - "source": [ - "* Dictionaries are defined by curly braces {}\n", - "\n", - "* They are a collection of key:value pairs, each pair separated by a comma.\n", - "\n", - "* Keys and values can be most Python objects, e.g: { 1:\"hello\", \"two\":True }\n", - "\n", - "* You look up elements in a dictionary using square bracket notation:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 35 - }, - "executionInfo": { - "elapsed": 1128, - "status": "ok", - "timestamp": 1606701189291, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "0tE3-3TZiLY1", - "outputId": "42f08720-81ae-49cc-f542-c6d921639555", - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "data": { - "application/vnd.google.colaboratory.intrinsic+json": { - "type": "string" - }, - "text/plain": [ - "'uno'" - ] - }, - "execution_count": 2, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "english_to_spanish[\"one\"]" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "cuONFTaehZ9M", - "slideshow": { - "slide_type": "subslide" - } - }, - "source": [ - "**No duplicate keys**: You can't have duplicate keys in a dictionary:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 1125, - "status": "ok", - "timestamp": 1606701189292, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "4vxBbOTbysDq", - "outputId": "2bfac3c3-5d17-47e1-bdb8-3300eecd2638", - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{1: 6, 3: 4}\n" - ] - } - ], - "source": [ - "a = { 1:2, 3:4, 1:6 } # Don't expect this to out work well\n", - "\n", - "print(a) " - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "a8MVbh8f3VB0", - "slideshow": { - "slide_type": "subslide" - } - }, - "source": [ - "**Duplicate values are allowed**:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 1122, - "status": "ok", - "timestamp": 1606701189292, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "qJOxmZ1p3X-A", - "outputId": "0f3af977-2a7f-4c9f-8331-179e205d95a2", - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{1: 1, 3: 4, 5: 1}\n" - ] - } - ], - "source": [ - "a = { 1:1, 3:4, 5:1 } \n", - "\n", - "print(a) " - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "9MdtD4pgy89t", - "slideshow": { - "slide_type": "subslide" - } - }, - "source": [ - "**Dictionaries are mutable:**" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 1119, - "status": "ok", - "timestamp": 1606701189293, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "0x2AWL4EzBlb", - "outputId": "34eeedea-26de-48fc-a3b1-2e6de1515df7", - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{1: 'hello', 3: 4, 5: 1}\n" - ] - } - ], - "source": [ - "a[1] = \"hello\"\n", - "\n", - "print(a)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 1115, - "status": "ok", - "timestamp": 1606701189293, - "user": { - "displayName": "Benedict Paten", - "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gh2rYrrH6yoNnUp0Oj4p4ouybc6ZcWGyPmKFtIY=s64", - "userId": "06399644931392855882" - }, - "user_tz": 480 - }, - "id": "OSUCX1d9zHpM", - "outputId": "7cf2d42f-0058-4a84-cf69-59b4ff5e43eb", - "slideshow": { - "slide_type": "fragment" - } - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{1: 'hello', 3: 4, 5: 1, 'a new key': 9}\n" - ] - } - ], - "source": [ - "a[\"a new key\"] = 9\n", - "\n", - "print(a)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "slideshow": { - "slide_type": "subslide" - } - }, - "source": [ - "# Challenge 7" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [], - "source": [ - "# Make a dictionary mapping the integers 1, 2, ..., 5 to their cubes (1, 8, 27, ...)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "KZP9y5sgyVm8", - "slideshow": { - "slide_type": "slide" - } - }, - "source": [ - "# Homework\n", - "\n", - "* Go to Canvas and complete the third lecture quiz, which involves completing each challenge problem\n", - "* ZyBook: Reading 3\n" - ] - } - ], - "metadata": { - "celltoolbar": "Slideshow", - "colab": { - "collapsed_sections": [ - "S4CkZnM_4FvB", - "odfPyGzkdUIJ", - "JPxEMRO9bFgc", - "nx4iryO296kf", - "MiqSwuSkggbP", - "GFggsPxW6lqW", - "MID_5W3JHkY8", - "VUnSbmkzyKwd" - ], - "name": "L03 More Types", - "provenance": [] - }, - "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.15" - }, - "vscode": { - "interpreter": { - "hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49" - } - } - }, - "nbformat": 4, - "nbformat_minor": 1 -} From 09c363dd0c48a83f9faf46de84f1c0a70562554a Mon Sep 17 00:00:00 2001 From: crimpmug <113626525+crimpmug@users.noreply.github.com> Date: Wed, 18 Jan 2023 15:36:46 -0800 Subject: [PATCH 7/7] Rename L03 More Types.ipynb to lecture_notebooks/L03 More Types.ipynb --- L03 More Types.ipynb => lecture_notebooks/L03 More Types.ipynb | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename L03 More Types.ipynb => lecture_notebooks/L03 More Types.ipynb (100%) diff --git a/L03 More Types.ipynb b/lecture_notebooks/L03 More Types.ipynb similarity index 100% rename from L03 More Types.ipynb rename to lecture_notebooks/L03 More Types.ipynb