{"worksheets": [{"cells": [{"source": ["# Python Introduction: Names and Values"], "cell_type": "markdown", "metadata": {}}, {"source": ["Define and reference a variable:"], "cell_type": "markdown", "metadata": {}}, {"collapsed": false, "prompt_number": 22, "language": "python", "outputs": [], "cell_type": "code", "input": ["a = 3*2 + 5"], "metadata": {}}, {"collapsed": false, "prompt_number": 23, "language": "python", "outputs": [{"text": ["11"], "prompt_number": 23, "metadata": {}, "output_type": "pyout"}], "cell_type": "code", "input": ["a"], "metadata": {}}, {"collapsed": false, "prompt_number": 24, "language": "python", "outputs": [], "cell_type": "code", "input": ["a = \"interesting\"*3"], "metadata": {}}, {"collapsed": false, "prompt_number": 25, "language": "python", "outputs": [{"text": ["'interestinginterestinginteresting'"], "prompt_number": 25, "metadata": {}, "output_type": "pyout"}], "cell_type": "code", "input": ["a"], "metadata": {}}, {"source": ["No type declaration needed!\n", "\n", "(But values still have types--let's check.)"], "cell_type": "markdown", "metadata": {}}, {"collapsed": false, "prompt_number": 26, "language": "python", "outputs": [{"text": ["str"], "prompt_number": 26, "metadata": {}, "output_type": "pyout"}], "cell_type": "code", "input": ["type(a)"], "metadata": {}}, {"source": ["Python variables are like *pointers*.\n", "\n", "(if that word makes sense)"], "cell_type": "markdown", "metadata": {}}, {"collapsed": false, "prompt_number": 27, "language": "python", "outputs": [], "cell_type": "code", "input": ["a = [1,2,3]"], "metadata": {}}, {"collapsed": false, "prompt_number": 28, "language": "python", "outputs": [], "cell_type": "code", "input": ["b = a"], "metadata": {}}, {"collapsed": false, "prompt_number": 29, "language": "python", "outputs": [], "cell_type": "code", "input": ["b.append(4)"], "metadata": {}}, {"collapsed": false, "prompt_number": 30, "language": "python", "outputs": [{"text": ["[1, 2, 3, 4]"], "prompt_number": 30, "metadata": {}, "output_type": "pyout"}], "cell_type": "code", "input": ["b"], "metadata": {}}, {"collapsed": false, "prompt_number": 31, "language": "python", "outputs": [{"text": ["[1, 2, 3, 4]"], "prompt_number": 31, "metadata": {}, "output_type": "pyout"}], "cell_type": "code", "input": ["a"], "metadata": {}}, {"source": ["You can see this pointer with `id()`."], "cell_type": "markdown", "metadata": {}}, {"collapsed": false, "prompt_number": 33, "language": "python", "outputs": [{"text": ["140441837907336 140441837907336\n"], "output_type": "stream", "stream": "stdout"}], "cell_type": "code", "input": ["print(id(a), id(b))"], "metadata": {}}, {"source": ["The `is` operator tests for object sameness."], "cell_type": "markdown", "metadata": {}}, {"collapsed": false, "prompt_number": 34, "language": "python", "outputs": [{"text": ["True"], "prompt_number": 34, "metadata": {}, "output_type": "pyout"}], "cell_type": "code", "input": ["a is b"], "metadata": {}}, {"source": ["This is a **stronger** condition than being equal!"], "cell_type": "markdown", "metadata": {}}, {"collapsed": false, "prompt_number": 36, "language": "python", "outputs": [{"text": ["IS False\n", "EQUAL True\n"], "output_type": "stream", "stream": "stdout"}], "cell_type": "code", "input": ["a = [1,2,3]\n", "b = [1,2,3]\n", "print(\"IS \", a is b)\n", "print(\"EQUAL\", a == b)"], "metadata": {}}, {"source": ["What do you think the following prints?"], "cell_type": "markdown", "metadata": {}}, {"collapsed": false, "prompt_number": 38, "language": "python", "outputs": [{"text": ["[1, 2, 3]\n"], "output_type": "stream", "stream": "stdout"}], "cell_type": "code", "input": ["a = [1,2,3]\n", "b = a\n", "a = a + [4]\n", "print(b)"], "metadata": {}}, {"collapsed": false, "prompt_number": 39, "language": "python", "outputs": [{"text": ["False"], "prompt_number": 39, "metadata": {}, "output_type": "pyout"}], "cell_type": "code", "input": ["a is b"], "metadata": {}}, {"source": ["Why is that?"], "cell_type": "markdown", "metadata": {}}, {"source": ["-----\n", "How could this lead to bugs?"], "cell_type": "markdown", "metadata": {}}, {"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`."], "cell_type": "markdown", "metadata": {}}, {"collapsed": false, "prompt_number": 40, "language": "python", "outputs": [{"text": ["list"], "prompt_number": 40, "metadata": {}, "output_type": "pyout"}], "cell_type": "code", "input": ["a = [1,2,3]\n", "type(a)"], "metadata": {}}, {"collapsed": false, "prompt_number": 41, "language": "python", "outputs": [{"text": ["tuple"], "prompt_number": 41, "metadata": {}, "output_type": "pyout"}], "cell_type": "code", "input": ["a = (1,2,3)\n", "type(a)"], "metadata": {}}, {"source": ["Let's try to change that tuple."], "cell_type": "markdown", "metadata": {}}, {"collapsed": false, "prompt_number": 42, "language": "python", "outputs": [{"evalue": "'tuple' object does not support item assignment", "ename": "TypeError", "output_type": "pyerr", "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"]}], "cell_type": "code", "input": ["a[2] = 0"], "metadata": {}}, {"source": ["*Bonus question:* How do you spell a single-element tuple?"], "cell_type": "markdown", "metadata": {}}, {"collapsed": false, "prompt_number": 43, "language": "python", "outputs": [{"text": ["tuple"], "prompt_number": 43, "metadata": {}, "output_type": "pyout"}], "cell_type": "code", "input": ["a = (3,)\n", "type(a)"], "metadata": {}}], "metadata": {}}], "nbformat": 3, "nbformat_minor": 0, "metadata": {"name": "", "signature": "sha256:11ef4a2f10ac55a86b2a04617b175ff798e11f21270d0bbd3edf0c37f2208ab0"}}