{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### 2. Some Objects are More Equal than Others\n", "What does `x = 1` mean to a computer? This is assignment, it tags the *object* on the right with the *variable* on the left. A variable is just a name for an object.\n", "\n", "\n", "\n", "The function `id()` gives the memory address of its argument, `print()` prints its argument." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4297514912\n" ] } ], "source": [ "x = 1\n", "print(id(x))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Writing `y=x` tags the object named `x` with the variable `y`. `x` and `y` are now different names for the same object, so the memory addresses are the same.\n", "\n", "" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4297514944\n", "4297514944\n" ] } ], "source": [ "y = x\n", "print(id(x))\n", "print(id(y))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Returning to `x=x+1`, we can make now sense of it. It calculates `x+1` using the original object named `x`, then names this new object `x`. Importantly, the memory address of `x` has changed.\n", "\n", "" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4297514912\n", "4297514944\n", "2\n" ] } ], "source": [ "x = 1\n", "print(id(x))\n", "x = x+1\n", "print(id(x))\n", "print(y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Types\n", "A number can be stored as an integer (`int`) or a decimal (`float`). Python guesses the type of your variable, so writing `x=1` will give you an integer tagged by `x`, whereas `x=1.0` gives a float. There's also a type for text: `string`." ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "#### Operations\n", "We can operate on variables using the following operations:\n", "\n", "* Addition: `+` e.g. `a = 5.0 + 1.0` results in `a = 6.0`\n", "* Subtraction: `-` e.g. `b = a - 1.0` results in `b = 5.0`\n", "* Multiplication: `*` e.g. `c = 5.0 \\ 3.0` results in `c = 15.0`\n", "* Division: `/` e.g. `d = 10.0 / 5.0` results in `d = 2.0`\n", "* Raising to powers: `**` e.g. `e = 2.0 ** 3.0` results in `e = 8.0`\n", "\n", "Other operations do exist, but for now these should be enough.\n", "\n", "An example is shown below, where we calculate the fractional difference in the gravitational force on the Earth depending on whether the moon is on the same side as the sun or on the other side." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.9988770761490094\n" ] } ], "source": [ "sun_mass = 1.981 * (10 ** 31)\n", "moon_mass = 7.348 * (10 ** 22)\n", "earth_mass = 5.972 * (10 ** 24)\n", "G = 6.674 * (10 ** (-11))\n", "r1 = 149597870700 # Sun earth distance\n", "r2 = 384402000 # Earth moon distance\n", "sun_force = (G * earth_mass * sun_mass)/(r1**2)\n", "moon_force = (G * earth_mass * moon_mass)/(r2**2)\n", "max_force = sun_force + moon_force\n", "min_force = sun_force - moon_force\n", "print(min_force/max_force)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Advanced Operations\n", "\n", "In addition to the ones above, we have the following more advanced operators:\n", "\n", "* Modulus: `%` This gives the remainder if you divide one by another \n", " a = 6.7 % 2.2 results in a = 0.1\n", "* Add AND: `+=` If we have `a += b`, this is equivalent to `a = a + b`\n", " a += 1 results in a = 1.1\n", "* Subtract AND: `-=` If we have `a -= b`, this is equivalent to `a = a - b` \n", " a -= 1 results in a = 0.1\n", "* Multiply AND: `\\=` If we have `a \\= b`, this is equivalent to `a = a \\*b`\n", " a *= 10 results in a = 1.0\n", "* Divide AND: `/=` If we have `a /= b`, this is equivalent to `a = a / b` \n", " a /= 10 results in a = 0.1\n", "* Exponent AND: `**=` If we have a **= b, this is equivalent to `a = a ** b`\n", " a **= 2 results in a = 0.01\n", "* Modulus AND: %= If we have a %= b, this is equivalent to `a = a % b`\n", " a %= 0.01 results in a = 0.0\n", "\n", "A simple example of some of these is shown below:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "current balance: £2900\n" ] } ], "source": [ "starting_balance = 1000\n", "current_balance = starting_balance\n", "current_balance -= 100 # Expensive night out\n", "current_balance += 2000 # Student finance\n", "print(\"current balance: £\" + str(current_balance))\n", "# We changed current balance into a string and added it to another string, and the total was printed out." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### An introduction to control statements\n", "\n", "The way we are using Python currently, it is nothing more than a calculator with memory.\n", "\n", "We can do much better. One way to do more advanced things is by harnessing the power of control statements, the simplest of which is the if/else if statement. An example is shown in a cheerful example below:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ ":(\n" ] } ], "source": [ "tripos_score = 31.0\n", "\n", "if tripos_score >= 70:\n", " tripos_class = 1.0\n", "elif tripos_score >= 60:\n", " tripos_class = 2.1\n", "elif tripos_score >= 50:\n", " tripos_class = 2.2\n", "elif tripos_score >= 40:\n", " tripos_class = 3.0\n", "else:\n", " tripos_class = None\n", "\n", "if tripos_class:\n", " print(\"You got a \" + str(tripos_class))\n", "else:\n", " print(\":(\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Boolean expressions\n", "\n", "In addition to `int`, `float`, and `string`, python also has a type called `bool`. A boolean can take the value `True` or `False`. In addition, any variable in python can also be `None`, which means it doesn't hold a value.\n", "\n", "A boolean expression is a statement which evaluates to either `True` or `False`. Examples are\n", "\n", "* `a == b` True if `a` is equal to `b`\n", "* `a != b` True if `a` is not equal to `b`\n", "* `a > b` True if `a` is greater than `b`\n", "* `a < b` True if `a` is less than `b`\n", "* `a <= b` True if `a` is less than or equal to `b`\n", "* `a >= b` True if `a` is greater than or equal to `b`\n", "\n", "We can also create a compound statement consisting of more than one boolean expression using the following operators:\n", "\n", "* `and`\n", "* `or`\n", "* `not`\n", "\n", "Which operate as you expect, e.g.\n", " `not is_poisonous and is_tasty`\n", "Would give `True` if `is_posinous` is `False` and `is_tasty` is `True`.\n", "\n", "#### The if-elif-else statement\n", "\n", "An `if` statement performs an action if the boolean expression in the `if` statement is `True`. `elif` is short for else if, and the action is performed if the first if statement was false but the boolean expression in the elif statement is true. Finally, if all the ifs and the elifs are False, the action in the else is executed. `else` and `elif` statements are optional.\n", "\n", "The basic structure is shown below:\n", "\n", " if boolean expression:\n", " statement_1\n", " elif boolean expression:\n", " statement_2\n", " else:\n", " statement_3\n", "\n", "Indentation is used to show where the statements from one if statement ends and the next starts." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "anaconda-cloud": {}, "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.1" }, "widgets": { "state": {}, "version": "1.1.2" } }, "nbformat": 4, "nbformat_minor": 0 }