{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Python Basics\n", "===\n", "\n", "## Unit 3, Lecture 1\n", "\n", "\n", "*Numerical Methods and Statistics*\n", "\n", "----\n", "#### Prof. Andrew White, Jan 30 2020\n", "-----\n", "\n", "*Suggested Reading*\n", "\n", "1. https://docs.python.org/3.6/tutorial/\n", "2. https://en.wikipedia.org/wiki/Binary_number" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Variables\n", "====\n", "\n", "A way of storing a number. You create a variable by assigning (`=`) an expression (right-hand-side) to a name (left-hand-side)." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n" ] } ], "source": [ "x = 2\n", "print(x)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "8\n" ] } ], "source": [ "print(x * 4)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n" ] } ], "source": [ "x = x + 2\n", "print(x)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.5551133015206257\n" ] } ], "source": [ "import math\n", "x = math.cos(43)\n", "print(x)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "ename": "NameError", "evalue": "name 'b' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\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 1\u001b[0m \u001b[0ma\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0ma\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0ma\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0mb\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m4\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mNameError\u001b[0m: name 'b' is not defined" ] } ], "source": [ "a = 2\n", "a = a * b\n", "b = 4" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Be careful\n", "\n", "Just like you remember to save you documents, be sure to store what you want in your variables." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n" ] } ], "source": [ "x = 2\n", "x * 2\n", "print(x)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Python String Formatting\n", "====\n", "\n", "What's the point of having variables if we can't tell anyone about them?!?!" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "This is the simple way of printing x 4\n" ] } ], "source": [ "x = 4\n", "print('This is the simple way of printing x', x)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "This is how we print x 4 and y 2.3 . Notice spaces are inserted for us\n" ] } ], "source": [ "y = 2.3\n", "print('This is how we print x', x, 'and y', y, '. Notice spaces are inserted for us')" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "What if I want to print y (2.3) without spaces?\n" ] } ], "source": [ "print('What if I want to print y ({}) without spaces?'.format(y))" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4.893498424324239e+31\n" ] } ], "source": [ "z = 0.489349842432423829328933 * 10**32\n", "print('{}'.format(z))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "The 'Formatted' in String Formatting\n", "====\n", "\n", "By using special character inside the `{}` expression, we can modify how variables are output." ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "What if I want to print x twice, like this 4 4, and y once, 2.3?\n" ] } ], "source": [ "print('What if I want to print x twice, like this {0:} {0:}, and y once, {1:}?'.format(x,y))" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "With a fixed precision (number of digits): 4.893e+31\n" ] } ], "source": [ "print('With a fixed precision (number of digits): {:.4}'.format(z))" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Make it take up exactly 10 spaces: 4.893e+31\n", "Make it take up exactly 10 spaces: 3.142\n", "Make it take up exactly 10 spaces: 1.537e+32\n" ] } ], "source": [ "print('Make it take up exactly 10 spaces: {:10.4}'.format(z))\n", "print('Make it take up exactly 10 spaces: {:10.4}'.format(math.pi))\n", "print('Make it take up exactly 10 spaces: {:10.4}'.format(z*math.pi))" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "You can demand to show without scientific notation too: 48934984243242387076283208040448.000000\n" ] } ], "source": [ "print('You can demand to show without scientific notation too: {:f}'.format(z))" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "or demand to use scientific notation: 3.141593e+00\n" ] } ], "source": [ "print('or demand to use scientific notation: {:e}'.format(math.pi))" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "There are many more formatting tags, such as binary: 101011 and percentage: 23.000000%\n" ] } ], "source": [ "print('There are many more formatting tags, such as binary: {:b} and percentage: {:%}'.format(43, 0.23))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Representing Data\n", "====\n", "Consider a model where the computer uses 8 bit numbers (1 byte). You could represent $[0,2^8-1]$. " ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "slideshow": { "slide_type": "skip" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: fixedint in /home/whitead/miniconda3/lib/python3.7/site-packages (0.1.5)\n" ] } ], "source": [ "%%bash\n", "pip install fixedint" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "from fixedint import *\n", "\n", "def bprint(x):\n", " print('{0:08b} ({0:})'.format(x))" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "00001000 (8)\n" ] } ], "source": [ "i = UInt8(8)\n", "bprint(i)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "00011000 (24)\n" ] } ], "source": [ "i = UInt8(24)\n", "bprint(i)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "11111111 (255)\n" ] } ], "source": [ "i = UInt8(2**8 - 1)\n", "bprint(i)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "00000000 (0)\n" ] } ], "source": [ "i = UInt8(2**8)\n", "bprint(i)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "This effect, where if we add 1 too many to a number and \"roll-over\" to the beginnig is called **integer overflow**. This is a common bug in programming, where a number becomes too large and then rolls-back. " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Ineger overflow can result in negative numbers, if we have signed numbers." ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "01111111 (127)\n", "-10000000 (-128)\n" ] } ], "source": [ "i = Int8(2**7 - 1)\n", "bprint(i)\n", "bprint(i + 1)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Notice here that our max is half previously, because one of our 8 bits is for sign, so only 7 bits are available for integers. *The key takeaway from integer overflow is that if we have integers with not enough bytes, we can suddenly have negative numbers or zero when adding large numbers*. Real world examples of integer overflow include view counts becoming 0, having data usage become negative on a cellphone, the Y2K bug, a step counter going to 0 steps. " ] } ], "metadata": { "celltoolbar": "Slideshow", "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3" } }, "nbformat": 4, "nbformat_minor": 1 }