{ "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": 22, "metadata": { "collapsed": false }, "outputs": [], "source": [ "a = 3*2 + 5" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "11" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false }, "outputs": [], "source": [ "a = \"interesting\"*3" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'interestinginterestinginteresting'" ] }, "execution_count": 25, "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)" ] } ], "metadata": {}, "nbformat": 4, "nbformat_minor": 0 }