{"worksheets": [{"metadata": {}, "cells": [{"metadata": {}, "cell_type": "markdown", "source": ["Define and reference a variable:"]}, {"language": "python", "input": ["a = 3*2 + 5"], "metadata": {}, "collapsed": false, "cell_type": "code", "prompt_number": 22, "outputs": []}, {"language": "python", "input": ["a"], "metadata": {}, "collapsed": false, "cell_type": "code", "prompt_number": 23, "outputs": [{"text": ["11"], "metadata": {}, "prompt_number": 23, "output_type": "pyout"}]}, {"language": "python", "input": ["a = \"interesting\"*3"], "metadata": {}, "collapsed": false, "cell_type": "code", "prompt_number": 24, "outputs": []}, {"language": "python", "input": ["a"], "metadata": {}, "collapsed": false, "cell_type": "code", "prompt_number": 25, "outputs": [{"text": ["'interestinginterestinginteresting'"], "metadata": {}, "prompt_number": 25, "output_type": "pyout"}]}, {"metadata": {}, "cell_type": "markdown", "source": ["No type declaration needed!\n", "\n", "(But values still have types--let's check.)"]}, {"language": "python", "input": ["type(a)"], "metadata": {}, "collapsed": false, "cell_type": "code", "prompt_number": 26, "outputs": [{"text": ["str"], "metadata": {}, "prompt_number": 26, "output_type": "pyout"}]}, {"metadata": {}, "cell_type": "markdown", "source": ["Python variables are like *pointers*.\n", "\n", "(if that word makes sense)"]}, {"language": "python", "input": ["a = [1,2,3]"], "metadata": {}, "collapsed": false, "cell_type": "code", "prompt_number": 27, "outputs": []}, {"language": "python", "input": ["b = a"], "metadata": {}, "collapsed": false, "cell_type": "code", "prompt_number": 28, "outputs": []}, {"language": "python", "input": ["b.append(4)"], "metadata": {}, "collapsed": false, "cell_type": "code", "prompt_number": 29, "outputs": []}, {"language": "python", "input": ["b"], "metadata": {}, "collapsed": false, "cell_type": "code", "prompt_number": 30, "outputs": [{"text": ["[1, 2, 3, 4]"], "metadata": {}, "prompt_number": 30, "output_type": "pyout"}]}, {"language": "python", "input": ["a"], "metadata": {}, "collapsed": false, "cell_type": "code", "prompt_number": 31, "outputs": [{"text": ["[1, 2, 3, 4]"], "metadata": {}, "prompt_number": 31, "output_type": "pyout"}]}, {"metadata": {}, "cell_type": "markdown", "source": ["You can see this pointer with `id()`."]}, {"language": "python", "input": ["print(id(a), id(b))"], "metadata": {}, "collapsed": false, "cell_type": "code", "prompt_number": 33, "outputs": [{"text": ["140441837907336 140441837907336\n"], "stream": "stdout", "output_type": "stream"}]}, {"metadata": {}, "cell_type": "markdown", "source": ["The `is` operator tests for object sameness."]}, {"language": "python", "input": ["a is b"], "metadata": {}, "collapsed": false, "cell_type": "code", "prompt_number": 34, "outputs": [{"text": ["True"], "metadata": {}, "prompt_number": 34, "output_type": "pyout"}]}, {"metadata": {}, "cell_type": "markdown", "source": ["This is a **stronger** condition than being equal!"]}, {"language": "python", "input": ["a = [1,2,3]\n", "b = [1,2,3]\n", "print(\"IS \", a is b)\n", "print(\"EQUAL\", a == b)"], "metadata": {}, "collapsed": false, "cell_type": "code", "prompt_number": 36, "outputs": [{"text": ["IS False\n", "EQUAL True\n"], "stream": "stdout", "output_type": "stream"}]}, {"metadata": {}, "cell_type": "markdown", "source": ["What do you think the following prints?"]}, {"language": "python", "input": ["a = [1,2,3]\n", "b = a\n", "a = a + [4]\n", "print(b)"], "metadata": {}, "collapsed": false, "cell_type": "code", "prompt_number": 38, "outputs": [{"text": ["[1, 2, 3]\n"], "stream": "stdout", "output_type": "stream"}]}, {"language": "python", "input": ["a is b"], "metadata": {}, "collapsed": false, "cell_type": "code", "prompt_number": 39, "outputs": [{"text": ["False"], "metadata": {}, "prompt_number": 39, "output_type": "pyout"}]}, {"metadata": {}, "cell_type": "markdown", "source": ["Why is that?"]}, {"metadata": {}, "cell_type": "markdown", "source": ["-----\n", "How could this lead to bugs?"]}, {"metadata": {}, "cell_type": "markdown", "source": ["----------\n", "* To help manage this risk, Python provides **immutable** types.\n", "\n", "* Immutable types cannot be changed in-place, only by creating a new object.\n", "\n", "* A `tuple` is an immutable `list`."]}, {"language": "python", "input": ["a = [1,2,3]\n", "type(a)"], "metadata": {}, "collapsed": false, "cell_type": "code", "prompt_number": 40, "outputs": [{"text": ["list"], "metadata": {}, "prompt_number": 40, "output_type": "pyout"}]}, {"language": "python", "input": ["a = (1,2,3)\n", "type(a)"], "metadata": {}, "collapsed": false, "cell_type": "code", "prompt_number": 41, "outputs": [{"text": ["tuple"], "metadata": {}, "prompt_number": 41, "output_type": "pyout"}]}, {"metadata": {}, "cell_type": "markdown", "source": ["Let's try to change that tuple."]}, {"language": "python", "input": ["a[2] = 0"], "metadata": {}, "collapsed": false, "cell_type": "code", "prompt_number": 42, "outputs": [{"traceback": ["\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ma\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"], "ename": "TypeError", "output_type": "pyerr", "evalue": "'tuple' object does not support item assignment"}]}, {"metadata": {}, "cell_type": "markdown", "source": ["*Bonus question:* How do you spell a single-element tuple?"]}, {"language": "python", "input": ["a = (3,)\n", "type(a)"], "metadata": {}, "collapsed": false, "cell_type": "code", "prompt_number": 43, "outputs": [{"text": ["tuple"], "metadata": {}, "prompt_number": 43, "output_type": "pyout"}]}]}], "metadata": {"signature": "sha256:61d7bfca2734ef4b6da44493f9985b5dbaa82a70845c456cc5605c2d7e2a9a09", "name": ""}, "nbformat_minor": 0, "nbformat": 3}