{ "metadata": { "name": "variables-assignment" }, "nbformat": 2, "worksheets": [ { "cells": [ { "cell_type": "markdown", "source": [ "Variables & Assignment", "======================", "A variable is a name that has a value associated with it.", "", "You can assign a value to a variable name using the equals sign." ] }, { "cell_type": "code", "collapsed": true, "input": [ "tail_length_mm = 27" ], "language": "python", "outputs": [], "prompt_number": 1 }, { "cell_type": "markdown", "source": [ "On the left hand side of the equals is the name of the variable. On the right hand side is that value that is assigned to it.", "", "The variable name then works just like typing it's value." ] }, { "cell_type": "code", "collapsed": false, "input": [ "tail_length_mm" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 2, "text": [ "27" ] } ], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "10 + tail_length_mm * 2" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 3, "text": [ "64" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "source": [ "The value of the variable won't change unless you assign a new value to that variable.", "", "The value of one variable can also be assigned to another variable." ] }, { "cell_type": "code", "collapsed": false, "input": [ "focal_tail_length = tail_length_mm", "focal_tail_length" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 4, "text": [ "27" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "source": [ "Or we can do a calculation with a variable and then assign it to a new variable." ] }, { "cell_type": "code", "collapsed": false, "input": [ "mass_g = 1000", "mass_kg = mass_g / 1000", "mass_kg" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 5, "text": [ "1" ] } ], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "x = 10", "y = 2", "x_plus_y = x + y", "print(x_plus_y)" ], "language": "python", "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "12" ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "source": [ "Changing the value assigned to a variable does not change anything that has already happened. It only changes things that happen after the change." ] }, { "cell_type": "code", "collapsed": false, "input": [ "genome_length = 600", "half_genome_length = genome_length / 2", "genome_length = 1000", "print(half_genome_length)" ], "language": "python", "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "300" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "source": [ "We can also use the same variable on both sides of an equals sign. This is conterintuitive from a mathematical perspective as we would never expect to see x = x + 5, but it's perfectly fine in programming." ] }, { "cell_type": "code", "collapsed": false, "input": [ "x = 10", "x = x + 5", "x" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 8, "text": [ "15" ] } ], "prompt_number": 8 }, { "cell_type": "markdown", "source": [ "This works because the expression on the right hand side of the equation is evaluated first. Specifically, the order of operations is:", "", "1. Look up the current value of x", "2. Add 5 to that value", "3. Assign the value resulting from the addition to x", "", "Updating the value of a variable like this is a common paradigm in programming." ] } ] } ] }