{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Advanced Python\n", "====\n", "\n", "## Unit 9, Lecture 1\n", "\n", "*Numerical Methods and Statistics*\n", "\n", "----\n", "\n", "#### Prof. Andrew White, March 30, 2020" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Becoming Pythonic\n", "===" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The Zen of Python, by Tim Peters\n", "\n", "Beautiful is better than ugly.\n", "Explicit is better than implicit.\n", "Simple is better than complex.\n", "Complex is better than complicated.\n", "Flat is better than nested.\n", "Sparse is better than dense.\n", "Readability counts.\n", "Special cases aren't special enough to break the rules.\n", "Although practicality beats purity.\n", "Errors should never pass silently.\n", "Unless explicitly silenced.\n", "In the face of ambiguity, refuse the temptation to guess.\n", "There should be one-- and preferably only one --obvious way to do it.\n", "Although that way may not be obvious at first unless you're Dutch.\n", "Now is better than never.\n", "Although never is often better than *right* now.\n", "If the implementation is hard to explain, it's a bad idea.\n", "If the implementation is easy to explain, it may be a good idea.\n", "Namespaces are one honking great idea -- let's do more of those!\n" ] } ], "source": [ "import this" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Programming Idioms To Make Life Easier\n", "--" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Iterating Lists - Enumerate\n", "===" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 hi\n", "1 this\n", "2 is\n", "3 my\n", "4 list\n" ] } ], "source": [ "my_list = ['hi', 'this', 'is', 'my', 'list']\n", "\n", "for i,e in enumerate(my_list):\n", " print(i, e)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Iterating Lists - Zip\n", "===" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 10\n", "1 11\n", "2 12\n", "3 13\n", "4 14\n", "5 15\n", "6 16\n", "7 17\n", "8 18\n", "9 19\n" ] } ], "source": [ "x = range(10)\n", "y = range(10, 20)\n", "\n", "for xi, yi in zip(x, y):\n", " print(xi, yi)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "List Comprehensions\n", "===\n", "\n", "This allows you to avoid a `for` loop when you don't want to type one out" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0, 1, 4, 9]\n" ] } ], "source": [ "powers = [xi ** 2 for xi in range(4)]\n", "print(powers)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 1\n", "3 9\n", "10 100\n" ] } ], "source": [ "# make y = x^2 quickly\n", "x = [1, 3, 10]\n", "y = [xi ** 2 for xi in x]\n", "\n", "for xi, yi in zip(x,y):\n", " print(xi, yi)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0, 0, 0, 0]\n" ] } ], "source": [ "# by convention, use _ to indicate we don't care about a variable\n", "zeros = [0 for _ in range(4)]\n", "print(zeros)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.3\n" ] } ], "source": [ "x = [4,3, 6, 1, 4]\n", "mean_x = sum(x) / len(x)\n", "delta_mean = [(xi - mean_x)**2 for xi in x]\n", "var = sum(delta_mean) / (len(x) - 1)\n", "\n", "print(var)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Dict Comprehensions\n", "===" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'number of people': 4, 'the number 10': 10, 'another number': 11}\n" ] } ], "source": [ "x = [4, 10, 11]\n", "y = ['number of people', 'the number 10', 'another number']\n", "\n", "#key: value\n", "my_dict = {yi: xi for xi, yi in zip(x,y)}\n", "\n", "print(my_dict)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "F Strings\n", "===\n", "\n", "You can simplify string formatting with f strings:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The answer is 4 and the mean is -3.00\n" ] } ], "source": [ "answer = 4\n", "mean = -3\n", "\n", "print(f'The answer is {answer} and the mean is {mean:.2f}')\n" ] } ], "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": 2 }