{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Python Introduction: Names and Values" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Define and reference a variable:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "a = 3*2 + 5" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "11" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [], "source": [ "a = \"interesting\"*3" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'interestinginterestinginteresting'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "No type declaration needed!\n", "\n", "(But values still have types--let's check.)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python variables are like *pointers*.\n", "\n", "(if that word makes sense)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false }, "outputs": [], "source": [ "a = [1,2,3]" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": false }, "outputs": [], "source": [ "b = a" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": false }, "outputs": [], "source": [ "b.append(4)" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4]" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4]" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can see this pointer with `id()`." ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "140441837907336 140441837907336\n" ] } ], "source": [ "print(id(a), id(b))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `is` operator tests for object sameness." ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a is b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is a **stronger** condition than being equal!" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "IS False\n", "EQUAL True\n" ] } ], "source": [ "a = [1,2,3]\n", "b = [1,2,3]\n", "print(\"IS \", a is b)\n", "print(\"EQUAL\", a == b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What do you think the following prints?" ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3]\n" ] } ], "source": [ "a = [1,2,3]\n", "b = a\n", "a = a + [4]\n", "print(b)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a is b" ] }, { "cell_type": "markdown", "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": "code", "execution_count": 40, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "list" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [1,2,3]\n", "type(a)" ] }, { "cell_type": "code", "execution_count": 41, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "tuple" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = (1,2,3)\n", "\n", "type(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's try to change that tuple." ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "collapsed": false }, "outputs": [ { "ename": "TypeError", "evalue": "'tuple' object does not support item assignment", "output_type": "error", "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" ] } ], "source": [ "a[2] = 0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Bonus question:* How do you spell a single-element tuple?" ] }, { "cell_type": "code", "execution_count": 43, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "tuple" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = (3,)\n", "\n", "type(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---------------------\n", "\n", "Can you represent that graphically?" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The gvmagic extension is already loaded. To reload it, use:\n", " %reload_ext gvmagic\n" ] } ], "source": [ "# (This cell contains a bunch of voodoo that\n", "# helps with the graphics below. You don't need to\n", "# know what this does.)\n", "\n", "%load_ext gvmagic\n", "from objgraph_helper import dot_refs" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": true }, "outputs": [], "source": [ "a = [1,2,3]\n", "b = [a,[1,2], a]" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "ObjectGraph\n", "\n", "\n", "o140352440694024\n", "\n", "list\n", "3 items\n", "\n", "\n", "o140352442844424\n", "\n", "list\n", "3 items\n", "\n", "\n", "o140352440694024->o140352442844424\n", "\n", "\n", "\n", "\n", "o140352440694024->o140352442844424\n", "\n", "\n", "\n", "\n", "o140352437032136\n", "\n", "list\n", "2 items\n", "\n", "\n", "o140352440694024->o140352437032136\n", "\n", "\n", "\n", "\n", "o10923616\n", "\n", "int\n", "3\n", "\n", "\n", "o140352442844424->o10923616\n", "\n", "\n", "\n", "\n", "o10923584\n", "\n", "int\n", "2\n", "\n", "\n", "o140352442844424->o10923584\n", "\n", "\n", "\n", "\n", "o10923552\n", "\n", "int\n", "1\n", "\n", "\n", "o140352442844424->o10923552\n", "\n", "\n", "\n", "\n", "o140352437032136->o10923584\n", "\n", "\n", "\n", "\n", "o140352437032136->o10923552\n", "\n", "\n", "\n", "\n", "\n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%dotstr dot_refs([b])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.2+" }, "widgets": { "state": {}, "version": "1.1.2" } }, "nbformat": 4, "nbformat_minor": 0 }