Skip to content
Snippets Groups Projects
lecture.ipynb 33 KiB
Newer Older
Felix Nyffenegger's avatar
Felix Nyffenegger committed
{
 "cells": [
  {
   "cell_type": "markdown",
nyfelix's avatar
nyfelix committed
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   "source": [
    "# Agenda\n",
    "\n",
    "* Python Syntax \n",
    "* Variables & Data Types\n",
    "* Operators\n",
    "* Decisions\n",
    "* Loops\n",
    "* Lists\n",
    "* Iterations"
   ]
  },
  {
   "cell_type": "markdown",
nyfelix's avatar
nyfelix committed
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   "source": [
    "# Python Syntax"
   ]
  },
  {
   "cell_type": "markdown",
nyfelix's avatar
nyfelix committed
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   "source": [
    "## Structure of the Code\n",
    "\n",
    "* Basically, Python code executes from top top bottom\n",
nyfelix's avatar
nyfelix committed
    "* Every programming language organizes code in code blocks (statements that belong together)\n",
Felix Nyffenegger's avatar
Felix Nyffenegger committed
    "* Indentation in Python is very important (it's mandatory not optional)\n",
nyfelix's avatar
nyfelix committed
    "  * Every code block (subcode) must be indented\n",
    "  * Unlike other languages, code blocks do not require brackets {}\n",
Felix Nyffenegger's avatar
Felix Nyffenegger committed
    "  * The number of spaces is up to you you, the most common use is four, but it has to be at least one."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
nyfelix's avatar
nyfelix committed
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
Felix Nyffenegger's avatar
Felix Nyffenegger committed
    }
nyfelix's avatar
nyfelix committed
   },
   "outputs": [],
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   "source": [
    "if 5 > 2:\n",
nyfelix's avatar
nyfelix committed
    "    print(\"Five is greater than two!\")\n",
    "    print(\"This is a second statement.\")\n",
    "else:\n",
    "    print(\"The world is a disk!\")"
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   ]
  },
  {
   "cell_type": "code",
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   "execution_count": 4,
nyfelix's avatar
nyfelix committed
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   "outputs": [
    {
     "ename": "IndentationError",
Felix Nyffenegger's avatar
Felix Nyffenegger committed
     "evalue": "expected an indented block (3793329317.py, line 2)",
Felix Nyffenegger's avatar
Felix Nyffenegger committed
     "output_type": "error",
     "traceback": [
Felix Nyffenegger's avatar
Felix Nyffenegger committed
      "\u001b[0;36m  Cell \u001b[0;32mIn[4], line 2\u001b[0;36m\u001b[0m\n\u001b[0;31m    print(\"Five is greater than two!\")\u001b[0m\n\u001b[0m    ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m expected an indented block\n"
Felix Nyffenegger's avatar
Felix Nyffenegger committed
     ]
    }
   ],
   "source": [
    "if 5 > 2:\n",
    "print(\"Five is greater than two!\")"
   ]
  },
  {
   "cell_type": "markdown",
nyfelix's avatar
nyfelix committed
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   "source": [
    "## Important Keywords (must not be used as names)\n",
    "\n",
    "* As with every programming language, there are some keywords that belong to the language and must not be used for names\n",
    "* Some popular ones are: True, False, None, def, class, break, return\n",
    "\n",
nyfelix's avatar
nyfelix committed
    " The code below prints the full list"
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
nyfelix's avatar
nyfelix committed
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   "source": [
    "import keyword\n",
    "print(keyword.kwlist)"
   ]
  },
  {
   "cell_type": "markdown",
nyfelix's avatar
nyfelix committed
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   "source": [
    "# Comments\n",
    "\n",
    "* Python has commenting capabilities for the purpose of in-code documentation.\n",
    "* Comments start with a #, and Python will ignore them:"
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
nyfelix's avatar
nyfelix committed
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   "outputs": [],
   "source": [
    "# This is a comment\n",
    "print(\"Hello, World!\")"
   ]
  },
  {
   "cell_type": "markdown",
nyfelix's avatar
nyfelix committed
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   "source": [
    "* Comments can be placed at the end of a line, and Python will ignore the rest of the line"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
nyfelix's avatar
nyfelix committed
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   "outputs": [],
   "source": [
    "print(\"Hello, World!\") #This is a comment"
   ]
  },
  {
   "cell_type": "markdown",
nyfelix's avatar
nyfelix committed
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   "source": [
    "# Variables and Data Types"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
nyfelix's avatar
nyfelix committed
    "slideshow": {
     "slide_type": "slide"
    },
Felix Nyffenegger's avatar
Felix Nyffenegger committed
    "vscode": {
     "languageId": "plaintext"
    }
   },
   "source": [
Felix Nyffenegger's avatar
Felix Nyffenegger committed
    "## Introducing a Variable in Python\n",
    "\n",
    "* Variables are an essential concept to store data.\n",
Felix Nyffenegger's avatar
Felix Nyffenegger committed
    "* Python has no command for declaring a variable.\n",
    "* A variable is automatically created when you first assign a value to it.\n",
    "* In Python, variables are not declared with any particular type.\n",
    "* To output the value of a variable, we use the `print()` command.\n",
Felix Nyffenegger's avatar
Felix Nyffenegger committed
    "\n",
    "Let's see some examples"
   ]
  },
  {
   "cell_type": "code",
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   "execution_count": 6,
nyfelix's avatar
nyfelix committed
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n",
      "This is a string\n"
     ]
    }
   ],
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   "source": [
    "x = 10\n",
    "print(x)\n",
    "y = \"This is a string\"\n",
    "print(y)"
   ]
  },
  {
   "cell_type": "markdown",
nyfelix's avatar
nyfelix committed
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   "source": [
    "## Naming Conventions\n",
    "\n",
    "* It is very important to choose meaningful names for variables\n",
    "  * This will make your code better readable to others as well\n",
    "  * You need fewer comments to explain your code\n",
    "* These are the most important rules and conventions:\n",
    "  * PEP 8 Style Guide for Python Code https://peps.python.org/pep-0008/#naming-conventions\n",
    "  * A good summary can be found here: https://medium.com/@rowainaabdelnasser/python-naming-conventions-10-essential-guidelines-for-clean-and-readable-code-fe80d2057bc9"
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Most Important Naming Conventions\n",
    "\n",
    "* Names **never start with a number**\n",
    "* We **don't use special characters** in names (ASCII compatiblity)\n",
Felix Nyffenegger's avatar
Felix Nyffenegger committed
    "* **Variable and Function Names**: Use `snake_case` (all lowercase letters with words separated by underscores).\n",
    "* **Constants**: Use `ALL_CAPS` with words separated by underscores. Constants are usually defined at the top of a module.\n",
    "* **Boolean Variables**: Should be named to sound like a yes/no question or statement, often starting with `is`, `has`, `can`, or `should`. E.g., `is_approved`\n",
    "* **Class Names**: Use `PascalCase` (each word starts with an uppercase letter, no underscores)."
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   ]
  },
  {
   "cell_type": "markdown",
nyfelix's avatar
nyfelix committed
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   "source": [
    "## Data Types I\n",
    "\n",
    "* Variables store values, and these values are of a particular type\n",
    "* Different types exhibit different behavior\n",
    "  * Adding two integers will result in the arithmetic sum\n",
    "  * Adding two strings will result in a string containing both substrings\n",
Felix Nyffenegger's avatar
Felix Nyffenegger committed
    "  * different data types have different functions available"
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   ]
  },
  {
   "cell_type": "code",
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   "execution_count": 8,
nyfelix's avatar
nyfelix committed
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9\n",
Felix Nyffenegger's avatar
Felix Nyffenegger committed
      "Text more text\n"
Felix Nyffenegger's avatar
Felix Nyffenegger committed
     ]
    }
   ],
   "source": [
    "x = 4\n",
    "y = 5\n",
    "z = x + y\n",
    "print(z)\n",
    "\n",
    "x = \"Text\"\n",
    "y = \" more text\"\n",
    "z = x + y\n",
    "print(z)"
   ]
  },
  {
   "cell_type": "markdown",
nyfelix's avatar
nyfelix committed
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   "source": [
    "## Data Types II\n",
    "\n",
    "These are the built-in data types of Python\n",
    "\n",
    "| Type Category       | Data Types                         |\n",
    "|---------------------|------------------------------------|\n",
    "| Text Type           | 'str'                              |\n",
    "| Numeric Types       | 'int', 'float', 'complex'          |\n",
    "| Sequence Types      | 'list', 'tuple', 'range'           |\n",
    "| Mapping Type        | 'dict'                             |\n",
    "| Set Types           | 'set', 'frozenset'                 |\n",
    "| Boolean Type        | 'bool'                             |\n",
    "| Binary Types        | 'bytes', 'bytearray', 'memoryview' |\n",
    "| None Type           | 'NoneType'                         |\n",
    "\n",
    "Some of these we will use soon, while others you will learn throughout the semester."
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   ]
  },
nyfelix's avatar
nyfelix committed
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Boolean Data Type\n",
    "\n",
    "* Boolean: `bool` has only two possible values: `True` or `False`\n",
    "* In programming you often need to know if an expression is `True` or `False`.\n",
    "* E.g. comparing values always returns  boolean result\n",
nyfelix's avatar
nyfelix committed
    "* Technically `False` correspons to the value of 0, any other number is considerd `True` (will be important for casting to bool)\n",
    "\n",
    "\n"
   ]
  },
Felix Nyffenegger's avatar
Felix Nyffenegger committed
  {
   "cell_type": "code",
nyfelix's avatar
nyfelix committed
   "execution_count": 1,
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
nyfelix's avatar
nyfelix committed
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "10 > 9"
   ]
  },
  {
   "cell_type": "markdown",
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
nyfelix's avatar
nyfelix committed
   "source": [
    "* Boolian variables / expressions are important for decisions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
nyfelix's avatar
nyfelix committed
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10 is greater than 9\n"
     ]
    }
   ],
   "source": [
    "is_true = 10 > 9\n",
    "if (is_true):\n",
    "    print(\"10 is greater than 9\")\n"
   ]
  },
  {
   "cell_type": "markdown",
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
nyfelix's avatar
nyfelix committed
   "source": [
    "## Number Data Type\n",
    "\n",
    "Python konws 3 numeric data types:\n",
    "| Type      | Explanation                                                                                           | Example                |\n",
    "| --------- | ----------------------------------------------------------------------------------------------------- | ---------------------- |\n",
nyfelix's avatar
nyfelix committed
    "| `int`     | A whole number, positive or negative, without decimals, of unlimited length.                          | 3, 88, -12837654       |\n",
    "| `float`   | Float, or \"floating point number\" is a number, positive or negative, containing one or more decimals. | 7.5, 1212.875620, 35e3 |\n",
    "| `complex` | Complex numbers are written with a \"j\" as the imaginary part:                                         | 3+5j                   |"
nyfelix's avatar
nyfelix committed
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   "outputs": [],
   "source": [
nyfelix's avatar
nyfelix committed
    "x = 10 # int\n",
    "y = 10.0 # float needs a .0 to be a float\n",
    "z = 2 + 3j # complex"
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   ]
  },
  {
   "cell_type": "markdown",
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
nyfelix's avatar
nyfelix committed
   "source": [
    "Float can also be scientific numbers with an \"e\" to indicate the power of 10"
   ]
  },
  {
   "cell_type": "code",
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   "execution_count": 38,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
nyfelix's avatar
nyfelix committed
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
Felix Nyffenegger's avatar
Felix Nyffenegger committed
      "35000.0\n"
nyfelix's avatar
nyfelix committed
     ]
    }
   ],
   "source": [
    "x = 35e3\n",
Felix Nyffenegger's avatar
Felix Nyffenegger committed
    "print(x)"
nyfelix's avatar
nyfelix committed
   ]
  },
  {
   "cell_type": "markdown",
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
nyfelix's avatar
nyfelix committed
   "source": [
Felix Nyffenegger's avatar
Felix Nyffenegger committed
    "## Text Data Types I\n",
nyfelix's avatar
nyfelix committed
    "\n",
    "* Besides numbers, we often want to process text in our programms\n",
nyfelix's avatar
nyfelix committed
    "* Python's built-in data type to do so is: `str`\n",
Felix Nyffenegger's avatar
Felix Nyffenegger committed
    "* Strings in python are surrounded by either single quotation marks, or double quotation marks. `'hello'` can be used as well as `\"hello\"`."
nyfelix's avatar
nyfelix committed
   ]
  },
  {
   "cell_type": "code",
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   "execution_count": 12,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "my_name = \"John\"\n",
    "my_name = 'John'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " * Special characters must be \"escaped\" with a I \\ (\\n = return, \\t = tabulator)\n",
    " * \" is allowed inside singel quotes, ' is allowed inside double quotes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
nyfelix's avatar
nyfelix committed
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
Felix Nyffenegger's avatar
Felix Nyffenegger committed
      "this is a \n",
      "return\n"
nyfelix's avatar
nyfelix committed
     ]
    }
   ],
   "source": [
Felix Nyffenegger's avatar
Felix Nyffenegger committed
    "my_text = \"this is 'ok' a double-quote needs an escape: \\\" to work\"\n",
    "my_text = 'here we can use \"\" but \\' must be escaped'\n",
    "print(\"this is a \\nreturn\")"
nyfelix's avatar
nyfelix committed
   ]
  },
  {
   "cell_type": "markdown",
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
nyfelix's avatar
nyfelix committed
   "source": [
Felix Nyffenegger's avatar
Felix Nyffenegger committed
    "## Text Data Types II\n",
nyfelix's avatar
nyfelix committed
    "* Strings can be concatibated"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
nyfelix's avatar
nyfelix committed
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "John Doe\n"
     ]
    }
   ],
   "source": [
    "first_name = \"John\"\n",
    "last_name = \"Doe\"\n",
    "full_name = first_name + \" \" + last_name\n",
    "print(full_name)"
   ]
  },
  {
   "cell_type": "markdown",
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
nyfelix's avatar
nyfelix committed
   "source": [
    "* Strings have various functions, that can be accessed"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
nyfelix's avatar
nyfelix committed
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "JOHN DOE\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "print(full_name.upper())\n",
    "print(full_name.count(\"o\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   "source": [
    "## Type Casting\n",
    "\n",
    "* Sometimes you want to explicitly change the data type of a variable\n",
    "* This process is called casting or type casting\n",
    "* In Python we do have functions to do this\n",
nyfelix's avatar
nyfelix committed
    "* Casting functions are available for all built-in datatypes, where the function name equals the type name\n",
Felix Nyffenegger's avatar
Felix Nyffenegger committed
    "\n",
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   ]
  },
  {
   "cell_type": "code",
nyfelix's avatar
nyfelix committed
   "execution_count": 7,
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'str'>\n",
      "3\n",
nyfelix's avatar
nyfelix committed
      "3.0\n",
      "3\n"
Felix Nyffenegger's avatar
Felix Nyffenegger committed
     ]
    }
   ],
   "source": [
    "a = str(3)    # Change an int value to a string\n",
    "print(type(a))\n",
    "b = int(3.7)  # Change a float value to an int\n",
    "print(b)\n",
nyfelix's avatar
nyfelix committed
    "c = float(3)  # Change an int value to a float\n",
    "print(c)\n",
    "c = int(\"3\")  # Change a string value to a int\n",
Felix Nyffenegger's avatar
Felix Nyffenegger committed
    "print(c)"
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   ]
  },
  {
   "cell_type": "markdown",
nyfelix's avatar
nyfelix committed
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   "source": [
Felix Nyffenegger's avatar
Felix Nyffenegger committed
    "## For the Geeks: Background Knowledge I\n",
Felix Nyffenegger's avatar
Felix Nyffenegger committed
    "\n",
Felix Nyffenegger's avatar
Felix Nyffenegger committed
    "* In Python, everything is an Object (an instance of a complex data type with functions and behavior)\n",
    "* More precise, a variable points to an instance of an object\n",
    "* The `is` operator checks whether two variables point to the same object in memory, not just if they have the same value."
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
nyfelix's avatar
nyfelix committed
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4537738384"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = 10.0\n",
    "id(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
nyfelix's avatar
nyfelix committed
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4537738384"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y = x\n",
    "id(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x is y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x is 10.0"
   ]
  },
Felix Nyffenegger's avatar
Felix Nyffenegger committed
  {
   "cell_type": "markdown",
nyfelix's avatar
nyfelix committed
   "metadata": {
    "slideshow": {
Felix Nyffenegger's avatar
Felix Nyffenegger committed
     "slide_type": "slide"
nyfelix's avatar
nyfelix committed
    }
   },
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   "source": [
Felix Nyffenegger's avatar
Felix Nyffenegger committed
    "## For the Geeks: Background Knowledge II \n",
Felix Nyffenegger's avatar
Felix Nyffenegger committed
    "\n",
    "* However, a new assignment creates a new object."
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   ]
  },
  {
   "cell_type": "code",
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   "execution_count": 15,
nyfelix's avatar
nyfelix committed
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   "outputs": [
    {
Felix Nyffenegger's avatar
Felix Nyffenegger committed
     "data": {
      "text/plain": [
       "4455295664"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
Felix Nyffenegger's avatar
Felix Nyffenegger committed
    }
   ],
   "source": [
    "y = 20.0\n",
    "id(y)"
   ]
  },
  {
   "cell_type": "markdown",
nyfelix's avatar
nyfelix committed
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   "source": [
Felix Nyffenegger's avatar
Felix Nyffenegger committed
    "* Thus, variables can be reassigned to a different type at runtime (variable remains a pointer, but to a different address/object)."
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   ]
  },
  {
   "cell_type": "code",
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   "execution_count": 16,
nyfelix's avatar
nyfelix committed
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'int'>\n",
      "<class 'str'>\n"
     ]
    }
   ],
   "source": [
    "x = 10\n",
    "print(type(x))\n",
    "x = \"This is a string\"\n",
    "print(type(x))"
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   ]
  },
  {
   "cell_type": "markdown",
nyfelix's avatar
nyfelix committed
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   "source": [
    "# Operators "
   ]
  },
  {
   "cell_type": "markdown",
nyfelix's avatar
nyfelix committed
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   "source": [
    "## Types of Operators\n",
    "\n",
    "* To work (compare, add, etc.) with variables, we need operators\n",
    "* Python categorizes operators into the following groups:\n",
Felix Nyffenegger's avatar
Felix Nyffenegger committed
    "    * Arithmetic operators\n",
    "    * Assignment operators\n",
    "    * Comparison operators\n",
    "    * Logical operators\n",
    "    * *Identity operators*\n",
    "    * *Membership operators*\n",
    "    * *Bitwise operators*\n",
    "\n",
    "Reference: https://www.w3schools.com/python/python_operators.asp "
   ]
  },
  {
   "cell_type": "markdown",
nyfelix's avatar
nyfelix committed
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   "source": [
    "## Arithmetic Operators\n",
    "\n",
    "We need arithmetic operators to calculate.\n",
    "\n",
    "| Operator | Name           | Example  |\n",
    "|----------|----------------|----------|\n",
    "| `+`      | Addition       | `x + y`  |\n",
    "| `-`      | Subtraction    | `x - y`  |\n",
    "| `*`      | Multiplication | `x * y`  |\n",
    "| `/`      | Division       | `x / y`  |\n",
    "| `%`      | Modulus        | `x % y`  |\n",
    "| `**`     | Exponentiation | `x ** y` |\n",
    "| `//`     | Floor division | `x // y` |\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
nyfelix's avatar
nyfelix committed
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   "outputs": [
    {
     "data": {
      "text/plain": [
       "25"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "5**2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
nyfelix's avatar
nyfelix committed
    "slideshow": {
     "slide_type": "slide"
    },
Felix Nyffenegger's avatar
Felix Nyffenegger committed
    "vscode": {
     "languageId": "plaintext"
    }
   },
   "source": [
    "## Assignment Operators\n",
    "\n",
    "We need assignment operators to assign values to variables.\n",
Felix Nyffenegger's avatar
Felix Nyffenegger committed
    "\n",
    "| Operator | Example | Same As     |\n",
    "|----------|---------|-------------|\n",
    "| `=`      | `x = 5` | `x = 5`     |\n",
    "| `+=`     | `x += 3`| `x = x + 3` |\n",
    "| `-=`     | `x -= 3`| `x = x - 3` |\n",
    "| `*=`     | `x *= 3`| `x = x * 3` |\n",
    "| `/=`     | `x /= 3`| `x = x / 3` |\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
nyfelix's avatar
nyfelix committed
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n"
     ]
    }
   ],
   "source": [
    "x = 5\n",
    "x *=2\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
nyfelix's avatar
nyfelix committed
    "slideshow": {
     "slide_type": "slide"
    },
Felix Nyffenegger's avatar
Felix Nyffenegger committed
    "vscode": {
     "languageId": "plaintext"
    }
   },
   "source": [
    "## Comparison Operators\n",
    "\n",
    "Comparison operators are needed to make decisions.\n",
    "The result of a comparison operator is always `true`or `false`.\n",
    "\n",
    "| Operator | Name                        | Example   |\n",
    "|----------|-----------------------------|-----------|\n",
    "| `==`     | Equal                       | `x == y`  |\n",
    "| `!=`     | Not equal                   | `x != y`  |\n",
    "| `>`      | Greater than                | `x > y`   |\n",
    "| `<`      | Less than                   | `x < y`   |\n",
    "| `>=`     | Greater than or equal to    | `x >= y`  |\n",
    "| `<=`     | Less than or equal to       | `x <= y`  |"
   ]
  },
  {
   "cell_type": "code",
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   "execution_count": 3,
nyfelix's avatar
nyfelix committed
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
Felix Nyffenegger's avatar
Felix Nyffenegger committed
     "execution_count": 3,
Felix Nyffenegger's avatar
Felix Nyffenegger committed
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "4 == 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
nyfelix's avatar
nyfelix committed
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
Felix Nyffenegger's avatar
Felix Nyffenegger committed
   "outputs": [
    {
     "data": {
      "text/plain": [