{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## CS 200 - Introduction\n", "

\n", "

\n", "\n", "August 31, 2022\n", "\n", "Load file: f0831.py (http://zoo.cs.yale.edu/classes/cs200/lectures/f0831.py) \n", "\n", "Note: python file names cannot start with a number, so no 0831.py\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "end of f0831\n", "name: f0831\n" ] } ], "source": [ "import f0831" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The file contains the line:\n", "\n", "digits = range(10)\n", "\n", "which defines a variable, digits" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'digits' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "Input \u001b[0;32mIn [2]\u001b[0m, in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0m digits\n", "\u001b[0;31mNameError\u001b[0m: name 'digits' is not defined" ] } ], "source": [ "digits" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "But we get an error, because the import statement will prepend the filename to every variable to protect the name space." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "range(0, 10)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f0831.digits" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(f0831.digits)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we want to avoid this prefix notation, we can use the from statement" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "from f0831 import *" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "range(0, 10)" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "digits" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We next define a function, num_to_word" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'eight'" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "num_to_word(8)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "num_to_word(12)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'nine'" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "num_to_word(-1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Using negative numbers to index a list gives you elements starting at the right hand side" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "from psource import *" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "

\n", "\n", "
def num_to_word(num):\n",
       "    if num > 9:\n",
       "        return False\n",
       "    numbers = ["zero","one","two","three","four","five","six","seven","eight","nine"]\n",
       "    return numbers[num]\n",
       "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "psource(num_to_word)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "psource is a function that displays the definition of a given function. Here is the definition of psouce:" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "

\n", "\n", "
def psource(*functions):\n",
       "    """Print the source code for the given function(s)."""\n",
       "    source_code = '\\n\\n'.join(getsource(fn) for fn in functions)\n",
       "    try:\n",
       "        from pygments.formatters import HtmlFormatter\n",
       "        from pygments.lexers import PythonLexer\n",
       "        from pygments import highlight\n",
       "\n",
       "        display(HTML(highlight(source_code, PythonLexer(), HtmlFormatter(full=True))))\n",
       "\n",
       "    except ImportError:\n",
       "        print(source_code)\n",
       "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "psource(psource)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "

\n", "\n", "
def word_to_num(word):\n",
       "    numbers = ["zero","one","two","three","four","five","six","seven","eight","nine"]\n",
       "    def find_index(lst,n, word):\n",
       "        if not lst:\n",
       "            return False\n",
       "        if lst[0] == word:\n",
       "            return n\n",
       "        return find_index(lst[1:], n+1, word)\n",
       "    return find_index(numbers,0,word)\n",
       "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "psource(word_to_num)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['zero',\n", " 'one',\n", " 'two',\n", " 'three',\n", " 'four',\n", " 'five',\n", " 'six',\n", " 'seven',\n", " 'eight',\n", " 'nine']" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "xlist" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['eight',\n", " 'five',\n", " 'four',\n", " 'nine',\n", " 'one',\n", " 'seven',\n", " 'six',\n", " 'three',\n", " 'two',\n", " 'zero']" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "z" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[8, 5, 4, 9, 1, 7, 6, 3, 2, 0]" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "zlist" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'8549176320'" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "''.join(map(str,zlist))" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "8549176320" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(''.join(map(str,zlist)))" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'85491763201'" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "''.join(map(str,zlist)) + '1'" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "8549176321" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(''.join(map(str,zlist))) + 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This should look familiar." ] } ], "metadata": { "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.10.5" } }, "nbformat": 4, "nbformat_minor": 4 }