From 4bfdef3f794beda07e106e6cb1ea0510633fd93b Mon Sep 17 00:00:00 2001 From: Martijn Vermaat <martijn@vermaat.name> Date: Mon, 8 Jul 2013 10:56:55 +0200 Subject: [PATCH] Booleans --- python-intro/python-intro.ipynb | 447 +++++++++++++++++++++++++++++++- 1 file changed, 445 insertions(+), 2 deletions(-) diff --git a/python-intro/python-intro.ipynb b/python-intro/python-intro.ipynb index cd8eeab..3e02773 100644 --- a/python-intro/python-intro.ipynb +++ b/python-intro/python-intro.ipynb @@ -1462,10 +1462,453 @@ } }, "source": [ - "Todo: add this when we introduce blocks\n", + "Sets\n", "===\n", "\n", - "A notable difference between Python and other languages is that its syntax uses whitespace indentation instead of braces or keywords to delimit blocks. It is therefore important to be consistent in the way you use indentation. We recommend to always increase indentation by *4 spaces* at a time and *never use tabs*. Some editors can be configured to always behave like that." + "Mutable unordered collections of hashable values\n", + "---" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "x = {12, 28, 21, 17}\n", + "type(x)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 50, + "text": [ + "set" + ] + } + ], + "prompt_number": 50 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "x.add(12)\n", + "x" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 51, + "text": [ + "{12, 17, 21, 28}" + ] + } + ], + "prompt_number": 51 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "x.discard(21)\n", + "x" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 52, + "text": [ + "{12, 17, 28}" + ] + } + ], + "prompt_number": 52 + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "Sets\n", + "===\n", + "\n", + "Operations with sets\n", + "---\n", + "\n", + "We can test for membership and apply many common set operations such as union and intersect." + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "17 in {12, 28, 21, 17}" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 56, + "text": [ + "True" + ] + } + ], + "prompt_number": 56 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "{12, 28, 21, 17} | {12, 18, 11}" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 57, + "text": [ + "{11, 12, 17, 18, 21, 28}" + ] + } + ], + "prompt_number": 57 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "{12, 28, 21, 17} & {12, 18, 11}" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 58, + "text": [ + "{12}" + ] + } + ], + "prompt_number": 58 + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "Booleans\n", + "===\n", + "\n", + "Boolean values and operations\n", + "---\n", + "\n", + "The two boolean values are written `False` and `True`." + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "True or False" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 59, + "text": [ + "True" + ] + } + ], + "prompt_number": 59 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "True and False" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 60, + "text": [ + "False" + ] + } + ], + "prompt_number": 60 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "not False" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 61, + "text": [ + "True" + ] + } + ], + "prompt_number": 61 + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "Booleans\n", + "===\n", + "\n", + "Comparisons\n", + "---\n", + "\n", + "Comparisons can be done on all objects and return a boolean value." + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "22 * 3 > 66" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 63, + "text": [ + "False" + ] + } + ], + "prompt_number": 63 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We have two equivalence relations: value equality (`==`) and object identity (`is`)." + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "a, b = [1, 2, 3], [1, 2, 3]\n", + "a == b" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 70, + "text": [ + "True" + ] + } + ], + "prompt_number": 70 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "a is b" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 71, + "text": [ + "False" + ] + } + ], + "prompt_number": 71 + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "Booleans\n", + "===\n", + "\n", + "`if` statements\n", + "---\n", + "\n", + "The `print` statement writes a string representation of the given value." + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "if 26 <= 17:\n", + " print 'Fact: 26 is less than or equal to 17'\n", + "elif (26 + 8 > 14) == True:\n", + " print 'Did we need the ` == True` part here?'\n", + "else:\n", + " print 'Nothing seems true'" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Did we need the ` == True` part here?\n" + ] + } + ], + "prompt_number": 74 + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "Booleans\n", + "===\n", + "\n", + "`while` statements\n", + "---\n", + "\n", + "Our first looping control structure just repeats until the given expression evaluates to `False`." + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "i = 0\n", + "while i < 5:\n", + " print i\n", + " i += 1" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "0\n", + "1\n", + "2\n", + "3\n", + "4\n" + ] + } + ], + "prompt_number": 75 + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "A note about syntax\n", + "===\n", + "\n", + "Indentation\n", + "---\n", + "\n", + "Python uses indentation to delimit blocks\n", + "\n", + "* Instead of `begin ... end` or `{ ... }` in other languages.\n", + "* Always increase indentation by *4 spaces*, never use tabs.\n", + "* In any case, be consistent." + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "if False:\n", + " if False:\n", + " print 'Why am I here?'\n", + " else:\n", + " while True:\n", + " print 'When will it stop?'" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 1 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Some editors can be configured to behave just like that." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "Iteration\n", + "===\n" ] } ], -- GitLab