{ "metadata": { "name": "", "signature": "sha256:d6500c6ef2e08098c8cda299c8dd764c91b6669955f725b14b121bdcc3722f87" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Booleans, Tuples, and Dictionaries" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Booleans" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "A ``boolean`` is one of the simplest Python types, and it can have two values: ``True`` and ``False`` (with uppercase ``T`` and ``F``):" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = True\n", "b = False" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Booleans can be combined with logical operators to give other booleans:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "True and False" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "True or False" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "(False and (True or False)) or (False and True)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Standard comparison operators can also produce booleans:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "1 == 3" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "1 != 3" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "3 > 2" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "3 <= 3.4" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Exercise 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write an expression that returns ``True`` if ``x`` is strictly greater than 3.4 and smaller or equal to 6.6, or if it is 2, and try changing ``x`` to see if it works:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x = 3.7\n", "\n", "# your solution here\n" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Tuples" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Tuples are, like lists, a type of sequence, but they use round parentheses rather than square brackets:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "t = (1, 2, 3)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "They can contain heterogeneous types like lists:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "t = (1, 2.3, 'spam')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "and also support item access and slicing like lists:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "t[1]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "t[:2]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "The main difference is that they are **immutable**, like strings:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "t[1] = 2" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We will not go into the details right now of why this is useful, but you should know that these exist as you may encounter them in examples." ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Dictionaries" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "One of the data types that we have not talked about yet is called *dictionaries* (``dict``). If you think about what a 'real' dictionary is, it is a list of words, and for each word is a definition. Similarly, in Python, we can assign definitions (or 'values'), to words (or 'keywords')." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Dictionaries are defined using curly brackets ``{}``:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "d = {'a':1, 'b':2, 'c':3}" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Items are accessed using square brackets and the 'key':" ] }, { "cell_type": "code", "collapsed": false, "input": [ "d['a']" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "d['c']" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Values can also be set this way:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "d['r'] = 2.2" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print(d)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "The keywords don't have to be strings, they can be many (but not all) Python objects:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "e = {}\n", "e['a_string'] = 3.3\n", "e[3445] = 2.2\n", "e[complex(2,1)] = 'value'" ], "language": "python", "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print(e)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "e[3445]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "If you try and access an element that does not exist, you will get a ``KeyError``:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "e[4]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Also, note that dictionaries do *not* know about order, so there is no 'first' or 'last' element." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It is easy to check if a specific key is in a dictionary, using the ``in`` operator:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"a\" in d" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "\"t\" in d" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that this also works for lists:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "3 in [1,2,3]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note, however, that while for dictionaries, ``in`` remains fast when they grow, with lists ``in`` becomes linearly slower as the list grows." ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Exercise 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Try making a dictionary to translate a few English words into German and try using it!" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "# your solution here\n" ], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }