{ "metadata": { "name": "recitation1" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# CS1001.py\n", "## Extended Introduction to Computer Science with Python, Tel-Aviv University, Spring 2013\n", "# Recitation 1 - 28.2-4.3.2013\n", "## Last update: 28.2.2013" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Python general comments\n", "\n", "1. Course site at \n", "1. Programming language -> Interpreter -> Machine language\n", "1. IDLE (editor + interpreter), see site for installation instructions\n", "1. Interactive mode vs. Script mode\n", "1. Python version 3.2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* print function - prints a textual representation to the console" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print(\"Hello world!\")" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Hello world!\n" ] } ], "prompt_number": 35 }, { "cell_type": "code", "collapsed": false, "input": [ "print(\"Hello\", \"world!\")" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Hello world!\n" ] } ], "prompt_number": 36 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Variables, types" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* int - integers: ..., -3, -2, -1, 0, 1, 2, 3, ..." ] }, { "cell_type": "code", "collapsed": false, "input": [ "x=5\n", "y=-3\n", "print(x, type(x))\n", "print(y, type(y))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "5 \n", "-3 \n" ] } ], "prompt_number": 37 }, { "cell_type": "code", "collapsed": false, "input": [ "x = 5.5\n", "print(type(x))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n" ] } ], "prompt_number": 38 }, { "cell_type": "markdown", "metadata": {}, "source": [ "* float - floating point numbers, decimal point fractions: -3.2, 1.5, 1e-8, 3.2e5" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x=5.0\n", "y=-3.2\n", "z=2.2e6\n", "print(x, type(x))\n", "print(z, type(z))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "5.0 \n", "2200000.0 \n" ] } ], "prompt_number": 39 }, { "cell_type": "markdown", "metadata": {}, "source": [ "* str - character strings, text: \"intro2CS\", 'python'" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x = \"CS1001.py\"\n", "y = 'I love python'\n", "print(x, type(x))\n", "print(y, type(y))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "CS1001.py \n", "I love python \n" ] } ], "prompt_number": 40 }, { "cell_type": "code", "collapsed": false, "input": [ "print(type(4), type(4.0), type(\"4\"))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " \n" ] } ], "prompt_number": 41 }, { "cell_type": "markdown", "metadata": {}, "source": [ "* bool - boolean values: True and False" ] }, { "cell_type": "code", "collapsed": false, "input": [ "i_love_python = True\n", "python_loves_me = False\n", "print(i_love_python, type(i_love_python))\n", "print(python_loves_me, type(python_loves_me))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "True \n", "False \n" ] } ], "prompt_number": 42 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Operators" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Mathematical operators" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Addition:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "4 + 5" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 45, "text": [ "9" ] } ], "prompt_number": 45 }, { "cell_type": "code", "collapsed": false, "input": [ "x = 5\n", "4 + x" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 46, "text": [ "9" ] } ], "prompt_number": 46 }, { "cell_type": "code", "collapsed": false, "input": [ "x = 4.0 + 5\n", "print(x, type(x))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "9.0 \n" ] } ], "prompt_number": 47 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Subtraction:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x - 3" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 48, "text": [ "6.0" ] } ], "prompt_number": 48 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Multiplication:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x * 3" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 49, "text": [ "27.0" ] } ], "prompt_number": 49 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Division - float and integral with / and //:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "10 / 3, 10 // 3" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 50, "text": [ "(3.3333333333333335, 3)" ] } ], "prompt_number": 50 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Power:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "2 ** 3, 2 ** 3.0, 3 ** 2" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 52, "text": [ "(8, 8.0, 9)" ] } ], "prompt_number": 52 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Modolu:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "10 % 3" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 53, "text": [ "1" ] } ], "prompt_number": 53 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### String operators" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "String concatenation using +:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"Hello\" + \" World\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 55, "text": [ "'Hello World'" ] } ], "prompt_number": 55 }, { "cell_type": "markdown", "metadata": {}, "source": [ "String duplication using *:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"Bye\" * 2" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 58, "text": [ "'ByeBye'" ] } ], "prompt_number": 58 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Strings vs. numbers:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "4 + 5" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 59, "text": [ "9" ] } ], "prompt_number": 59 }, { "cell_type": "code", "collapsed": false, "input": [ "\"4\" + \"5\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 60, "text": [ "'45'" ] } ], "prompt_number": 60 }, { "cell_type": "code", "collapsed": false, "input": [ "\"4\" + 5" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "Can't convert 'int' object to str implicitly", "output_type": "pyerr", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;34m\"4\"\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;36m5\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mTypeError\u001b[0m: Can't convert 'int' object to str implicitly" ] } ], "prompt_number": 61 }, { "cell_type": "code", "collapsed": false, "input": [ "4 + \"5\"" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "unsupported operand type(s) for +: 'int' and 'str'", "output_type": "pyerr", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;36m4\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;34m\"5\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'int' and 'str'" ] } ], "prompt_number": 62 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Comparisons" ] }, { "cell_type": "code", "collapsed": false, "input": [ "5 < 4" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 63, "text": [ "False" ] } ], "prompt_number": 63 }, { "cell_type": "code", "collapsed": false, "input": [ "5 > 4" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 64, "text": [ "True" ] } ], "prompt_number": 64 }, { "cell_type": "code", "collapsed": false, "input": [ "5 >= 4" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 65, "text": [ "True" ] } ], "prompt_number": 65 }, { "cell_type": "code", "collapsed": false, "input": [ "4 >= 4" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 66, "text": [ "True" ] } ], "prompt_number": 66 }, { "cell_type": "code", "collapsed": false, "input": [ "4 <= 3" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 73, "text": [ "False" ] } ], "prompt_number": 73 }, { "cell_type": "code", "collapsed": false, "input": [ "5 == 4" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 67, "text": [ "False" ] } ], "prompt_number": 67 }, { "cell_type": "code", "collapsed": false, "input": [ "5 == 5.0" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 68, "text": [ "True" ] } ], "prompt_number": 68 }, { "cell_type": "code", "collapsed": false, "input": [ "5 == \"5\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 69, "text": [ "False" ] } ], "prompt_number": 69 }, { "cell_type": "code", "collapsed": false, "input": [ "5 != 4" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 70, "text": [ "True" ] } ], "prompt_number": 70 }, { "cell_type": "code", "collapsed": false, "input": [ "2 + 2 == 4" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 71, "text": [ "True" ] } ], "prompt_number": 71 }, { "cell_type": "code", "collapsed": false, "input": [ "2 => 3" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (, line 1)", "output_type": "pyerr", "traceback": [ "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m 2 => 3\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" ] } ], "prompt_number": 72 }, { "cell_type": "code", "collapsed": false, "input": [ "x = 1 / 3 \n", "print(x)\n", "x == 0.3333333333333333" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0.3333333333333333\n" ] }, { "output_type": "pyout", "prompt_number": 77, "text": [ "True" ] } ], "prompt_number": 77 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Logical operators" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* not:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print(not True)\n", "a = 2 == 5\n", "print(not a)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "False\n", "True\n" ] } ], "prompt_number": 83 }, { "cell_type": "markdown", "metadata": {}, "source": [ "* and:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "True and True" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 84, "text": [ "True" ] } ], "prompt_number": 84 }, { "cell_type": "code", "collapsed": false, "input": [ "True and False" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 85, "text": [ "False" ] } ], "prompt_number": 85 }, { "cell_type": "code", "collapsed": false, "input": [ "False and False" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 86, "text": [ "False" ] } ], "prompt_number": 86 }, { "cell_type": "markdown", "metadata": {}, "source": [ "* or:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "True or True" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 87, "text": [ "True" ] } ], "prompt_number": 87 }, { "cell_type": "code", "collapsed": false, "input": [ "True or False" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 88, "text": [ "True" ] } ], "prompt_number": 88 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Conversions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use the functions `int()`, `float()`, and `str()` to convert between types (we will talk about *functions* next time):" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x = \"6\"\n", "print(x, type(x))\n", "x = int(\"6\")\n", "print(x, type(x))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "6 \n", "6 \n" ] } ], "prompt_number": 91 }, { "cell_type": "code", "collapsed": false, "input": [ "float(\"1.25\")" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 92, "text": [ "1.25" ] } ], "prompt_number": 92 }, { "cell_type": "code", "collapsed": false, "input": [ "str(4)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 93, "text": [ "'4'" ] } ], "prompt_number": 93 }, { "cell_type": "code", "collapsed": false, "input": [ "int(\"a\")" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "invalid literal for int() with base 10: 'a'", "output_type": "pyerr", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"a\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mValueError\u001b[0m: invalid literal for int() with base 10: 'a'" ] } ], "prompt_number": 94 }, { "cell_type": "code", "collapsed": false, "input": [ "course = \"intro\" + str(2) + \"cs\"\n", "print(course)\n", "print(\"intro\", 2, \"cs\", sep='')" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "intro2cs\n", "intro2cs\n" ] } ], "prompt_number": 98 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Flow control" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Conditional statements" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `if` condition formula - replace conditions and statements with meaningful code:\n", "\n", " if *condition*:\n", " *statement*\n", " *statement*\n", " ...\n", " elif *condition*: # 0 or more elif clauses\n", " *statement*\n", " *statement*\n", " ... \n", " else: # optional\n", " *statement*\n", " *statement*\n", "\n", "Example:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "today = \"Monday\"\n", "strike = \"N\"\n", "my_recitation = \"Monday\"\n", "\n", "if today == \"Sunday\":\n", " print(\"Shvizut Yom Alef\")\n", " if strike == \"Y\":\n", " print(\"Stay home\")\n", " else:\n", " print(\"Lecture in intro to CS!\")\n", "elif today == \"Wednesday\":\n", " print(\"Another lecture in intro to CS!\")\n", "elif today == my_recitation:\n", " print(\"Go to recitation!\")\n", "elif today == \"Monday\" or today == \"Tuesday\" or today == \"Thursday\" or \\\n", " today == \"Friday\" or today == \"Saturday\":\n", " print(\"no intro to CS\")\n", "else:\n", " print(\"Not a day\")" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Go to recitation!\n" ] } ], "prompt_number": 101 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Loops" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* While:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " while *condition*:\n", " *statement*\n", " *statement*\n", "\n", "Example - count how many times 0 appears in an integer number:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "num = 2**100\n", "print(num)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1267650600228229401496703205376\n" ] } ], "prompt_number": 102 }, { "cell_type": "code", "collapsed": false, "input": [ "count = 0\n", "\n", "while num > 0: #what if we changed to >=0?\n", " if num % 10 == 0:\n", " count = count + 1\n", " num = num // 10\n", "\n", "print(count)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "6\n" ] } ], "prompt_number": 103 }, { "cell_type": "markdown", "metadata": {}, "source": [ "* For:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " for *variable* in *iterable*:\n", " *statement*\n", " *statement*\n", "\n", "Example - solve the same problem with a `str` type instead of `int`:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "num = 2**100\n", "count = 0\n", "for digit in str(num):\n", " #print(digit, type(digit))\n", " if digit == \"0\":\n", " count = count + 1\n", "\n", "print(count)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "6\n" ] } ], "prompt_number": 105 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Builtin solution:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "num = 2**100\n", "count = str.count(str(num), \"0\")\n", "\n", "print(count)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "6\n" ] } ], "prompt_number": 106 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Efficiency" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can measure which solution is faster:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%%timeit\n", "num = 2**100\n", "count = 0\n", "while num>0: #what if we changed to >=0?\n", " if num % 10 == 0:\n", " count = count + 1\n", " num = num // 10" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "10000 loops, best of 3: 37.4 us per loop\n" ] } ], "prompt_number": 108 }, { "cell_type": "code", "collapsed": false, "input": [ "%%timeit \n", "num = 2**100\n", "count = 0\n", "for digit in str(num):\n", " if digit == \"0\":\n", " count = count + 1" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "100000 loops, best of 3: 8.76 us per loop\n" ] } ], "prompt_number": 109 }, { "cell_type": "code", "collapsed": false, "input": [ "%%timeit \n", "num = 2**100\n", "count = str.count(str(num), \"0\")" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "100000 loops, best of 3: 2.82 us per loop\n" ] } ], "prompt_number": 110 }, { "cell_type": "markdown", "metadata": {}, "source": [ "The builtin solution is 4 times faster than the `for` solution which is 3 times faster than the `while` solution." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Other notes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* The `while` solution will not work for `num <= 0`\n", "* The `while` solution will not work for non-numerals (e.g, `num = \"Cola 0 is awesome!\"`)\n", "* The builtin solution is implemented with C and that is why it is faster" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Fin" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook is part of the [Extended introduction to computer science](http://tau-cs1001-py.wikidot.com/) course at Tel-Aviv University.\n", "\n", "The notebook was written using Python 3.2 and IPython 0.13.1.\n", "\n", "The code is available at .\n", "\n", "The notebook can be viewed online at .\n", "\n", "The notebooks is also available as a PDF at .\n", "\n", "This work is licensed under a [Creative Commons Attribution-ShareAlike 3.0 Unported License](http://creativecommons.org/licenses/by-sa/3.0/)." ] } ], "metadata": {} } ] }