{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Python Basics\n", "===\n", "\n", "## Unit 3, Lecture 2\n", "\n", "*Numerical Methods and Statistics*\n", "\n", "----\n", "\n", "#### Prof. Andrew White, February 6, 2018\n", "-----\n", "\n", "*Suggested Reading*\n", "\n", "1. https://docs.python.org/3.6/tutorial/\n", "2. https://en.wikipedia.org/wiki/Floating-point_arithmetic#Floating-point_numbers" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Floating Point Representation\n", "====\n", "\n", "Floating point numbers are represented with a mantissa and exponent:\n", "$$\\underbrace{0.3423}_{\\textrm{mantissa}}\\times{}\\underbrace{10^{-2}}_{\\textrm{exponent}}$$\n", "\n", "----\n", "\n", "We are working with computers though, so the mantissa is a *binary* and the exponent is in powers of 2. Let's look at them as decimals to begin though." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [ "from IPython.display import Math\n", "from math import frexp, pi\n", "import math\n", " \n", "#Convert a float into its mantissa and exponent and print as LaTeX\n", "def fprint(x):\n", " m,e = frexp(x)\n", " return Math('{:4} \\\\times 2^{{{:}}}'.format(m, int(e)))\n", "\n", "#Convert a mantissa from decimal to binary and print as LaTeX\n", "def ffrac_ltx(x, terms=10):\n", " bits = []\n", " exp = 1\n", " latex = ''\n", " while x > 0 and exp < terms:\n", " bits.append(int(x / 2.0 ** -exp))\n", " x -= bits[-1] * 2.0 ** -exp\n", " latex += '\\\\frac{{ {} }}{{ 2^{{ {} }} }} + '.format(bits[-1], exp)\n", " exp += 1\n", " return Math(latex[:-3] + ' = {}'.format(sum([b * 2**-i for b,i in zip(bits,range(1,exp))])))\n", "\n", "def ffrac(x, terms=10):\n", " bits = []\n", " exp = 1\n", " while x > 0 and exp < terms:\n", " bits.append(int(x / 2.0 ** -exp))\n", " x -= bits[-1] * 2.0 ** -exp\n", " exp += 1\n", " return ''.join(['{}'.format(x) for x in bits])" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle 0.5375 \\times 2^{3}$" ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fprint(4.3)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle 0.8 \\times 2^{-3}$" ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fprint(0.1)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle 0.7853981633974483 \\times 2^{2}$" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fprint(pi)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "A 'decimal' in binary is challenging to think about, because each integer location is a $1 / 2^{n}$, where $n$ is the location. That means to represent a binary-mantissa exacly, its denominator must be a power of $2$. For example: " ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\frac{ 1 }{ 2^{ 1 } } = 0.5$" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ffrac_ltx(0.5)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\frac{ 1 }{ 2^{ 1 } } + \\frac{ 1 }{ 2^{ 2 } } = 0.75$" ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ffrac_ltx(0.75)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\frac{ 1 }{ 2^{ 1 } } + \\frac{ 0 }{ 2^{ 2 } } + \\frac{ 0 }{ 2^{ 3 } } + \\frac{ 0 }{ 2^{ 4 } } + \\frac{ 0 }{ 2^{ 5 } } + \\frac{ 1 }{ 2^{ 6 } } + \\frac{ 0 }{ 2^{ 7 } } + \\frac{ 1 }{ 2^{ 8 } } + \\frac{ 1 }{ 2^{ 9 } } = 0.521484375$" ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ffrac_ltx(0.5232)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0001100110011001100110011001100110011001100110011001101\n" ] } ], "source": [ "#This is the maximum number of bits used in the 0.1 Mantissa as a string.\n", "print(ffrac(0.1, 100))" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.1000000000000000055511151231\n" ] } ], "source": [ "print('{:0.28f}'.format(0.1))" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "0.1 + 0.2 == (1.0 + 2.0) / 10.0" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Do not use == with floats!" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Using Functions and Modules\n", "====" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "43\n" ] } ], "source": [ "print(abs(-43))" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n" ] } ], "source": [ "x = -5\n", "x = abs(x)\n", "print(x)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Imports and Modules\n", "====\n", "\n", "Try typing math.fa and hit TAB in ipython notebook. You should see suggestions. Next, type math.fabs( and hit TAB and ipython notebook will tell you what the function does." ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4.34322\n" ] } ], "source": [ "from math import fabs\n", "print(fabs(-4.34322))" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.4201111317694318\n" ] } ], "source": [ "from math import fabs, sin, cos\n", "print(fabs(sin(4) * cos(43.)))" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2.718281828459045\n" ] } ], "source": [ "from math import *\n", "print(e)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Boolean Logic\n", "====" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "print(2 == 5)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "a = 3\n", "print(a == 3)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "print(a <= 0)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "print(a > 0 and a < 4)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Boolean Logic uses Short-Circuiting - only as much as necessary is evaluated." ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "print(True or 1 / 0.)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "ename": "ZeroDivisionError", "evalue": "float division by zero", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mZeroDivisionError\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[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;32mTrue\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0;36m1\u001b[0m \u001b[0;34m/\u001b[0m \u001b[0;36m0.\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mZeroDivisionError\u001b[0m: float division by zero" ] } ], "source": [ "print(True and 1 / 0.)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Flow with Boolean\n", "====\n", "\n", "You can make code only accessible if a condition is true with if. You can also use else and elif for alternatives.\n", "\n", "To indicate where your if code is located, press tab (which adds 4 spaces)." ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Surprise! 65 is greater than 43\n" ] } ], "source": [ "x = 65\n", "if x > 43:\n", " print('Surprise! {} is greater than 43'.format(x))" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "54 is positive\n" ] } ], "source": [ "x = 54\n", "if x < 0:\n", " print('{} is negative'.format(x))\n", "elif x > 0:\n", " print('{} is positive'.format(x))\n", "else:\n", " print('{} is 0'.format(x))" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-32 is less than 0 and has magnitude greater than 5\n", "-32 is less than 0 and has magnitude greater than 5\n" ] } ], "source": [ "x = -32\n", "if x < 0:\n", " if abs(x) > 5:\n", " print('{} is less than 0 and has magnitude greater than 5'.format(x))\n", " else:\n", " print('{} is not interesting'.format(x))\n", " \n", "#or you can do:\n", "if x < 0 and abs(x) > 5:\n", " print('{} is less than 0 and has magnitude greater than 5'.format(x))\n", "if x < 0 and not abs(x) > 5:\n", " print('{} is not interesting'.format(x))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Full list of boolean operators\n", "====\n", "\n", "Here are the boolean operators: >, <, >=, <=, ==, !=, is\n", "\n", "Recall our keywords from Lecture 1: and, or, and not\n", "\n", "is is used to see if a variables is None or if two variables point to the same memory address." ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False True\n" ] } ], "source": [ "a = 28238\n", "b = 28238\n", "print(a is b, a == b)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "a = b\n", "print(a is b)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "is None is special case. None is used as a \"sentinel\" value to indicate if something is not ready or invalid. " ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "var = None\n", "print(var is None)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "a = 43\n", "b = 23\n", "print(a != b)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Default Boolean - Don't Rely on this!\n", "====\n", "\n", "Just to be aware of, Python has a way of checking if an object is False based on its value (0 or None)." ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "x = 1\n", "if(x):\n", " print('True')\n", "else:\n", " print('False')" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "x = None\n", "if(x):\n", " print('True')\n", "else:\n", " print('False')" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "x = 'Hello'\n", "if(x):\n", " print('True')\n", "else:\n", " print('False')" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "x = ''\n", "if(x):\n", " print('True')\n", "else:\n", " print('False')" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "x = math.sin(0.0)\n", "if(x):\n", " print('True')\n", "else:\n", " print('False')" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "It is better to just be explicit and give a full boolean expression. You'll most often see these used by accident" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Floating Point Boolean\n", "====" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "x = 0.15 + 0.15\n", "y = 0.1 + 0.2\n", "print(x == y)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "The solution?\n", "=====\n", "\n", "Only use <, >. Never use == with boolean" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 0.15 + 0.15\n", "y = 0.1 + 0.2\n", "abs(x - y) < 10**-8" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Lists\n", "====\n", "\n", "In python, groups of values can be stored together into lists, which are like groups of variables." ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[4, 7, 24, 11, 2]\n" ] } ], "source": [ "numbers = [4,7,24,11,2]\n", "print(numbers)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Individual elements can be accessed with []. The thing that goes inside the brackets is called the *index*. Recall that python starts counting from 0" ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4 7\n" ] } ], "source": [ "print(numbers[0], numbers[1])" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Slicing Lists\n", "----\n", "\n", "You can access multiple elements with the : symbol, which is called *slicing*." ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[4, 7, 24]\n" ] } ], "source": [ "print(numbers[0:3])" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "The last element to the slice operator is not inclusive, so 0:3 means 0, 1, 2. Why? Becuase you expect $3 - 0$ elements, which is three, starting from the $0$ index." ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "[24, 11, 2]" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "numbers[2:5]" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "If it's more convienent, you can count backwards from the end of the list by using negative indices. The trick is that -0 is not really a thing, so the last element in the list is -1. " ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[4, 7, 24, 11, 2]\n", "2 11\n" ] } ], "source": [ "print(numbers)\n", "print(numbers[-1], numbers[-2])" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "The index arguments to a slice are optional." ] }, { "cell_type": "code", "execution_count": 41, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[4, 7, 24, 11]\n" ] } ], "source": [ "print(numbers[:4])" ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[4, 7, 24, 11, 2]\n", "[4, 7, 24, 11]\n" ] } ], "source": [ "print(numbers)\n", "print(numbers[:-1])" ] }, { "cell_type": "code", "execution_count": 43, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[11, 2]\n" ] } ], "source": [ "print(numbers[3:])" ] }, { "cell_type": "code", "execution_count": 44, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[24, 11, 2]\n" ] } ], "source": [ "print(numbers[-3:])" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "You can add a third argument by adding another : It is the step-size." ] }, { "cell_type": "code", "execution_count": 45, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[4, 7, 24, 11, 2]\n", "[4, 24]\n" ] } ], "source": [ "print(numbers)\n", "print(numbers[0:-1:2])" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "The other slicing indices are optional." ] }, { "cell_type": "code", "execution_count": 46, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[4, 24, 2]\n" ] } ], "source": [ "print(numbers[0::2])" ] }, { "cell_type": "code", "execution_count": 47, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[4, 7, 24, 11, 2]\n", "[4, 24, 2]\n" ] } ], "source": [ "print(numbers)\n", "print(numbers[::2])" ] }, { "cell_type": "code", "execution_count": 48, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[24, 2]\n" ] } ], "source": [ "print(numbers[2::2])" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Using a negative stepsize allows you to count downwards" ] }, { "cell_type": "code", "execution_count": 49, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2, 11, 24, 7]\n" ] } ], "source": [ "print(numbers[4:0:-1])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that we still include the first slice index (4), but not the second one (0). " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "A common idiom is to reverse the order of list this way:" ] }, { "cell_type": "code", "execution_count": 50, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[4, 7, 24, 11, 2] [2, 11, 24, 7, 4]\n" ] } ], "source": [ "print(numbers, numbers[::-1])" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Notice that Python was clever and knew with a step-size of -1, we actually wanted to start at the far end and count downwards." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "List Functions\n", "====\n", "\n", "Generally, most functions (like cos) do not take a list. One common function that we need is len, which returns the length of a list." ] }, { "cell_type": "code", "execution_count": 51, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "100" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = range(100)\n", "len(x)" ] }, { "cell_type": "code", "execution_count": 52, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "9\n" ] } ], "source": [ "x = range(10)\n", "print(max(x))" ] }, { "cell_type": "code", "execution_count": 53, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "ename": "TypeError", "evalue": "must be real number, not range", "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 1\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mmath\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mmath\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcos\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: must be real number, not range" ] } ], "source": [ "import math\n", "math.cos(x)" ] } ], "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.6.7" } }, "nbformat": 4, "nbformat_minor": 1 }