{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### 3. Data Structures and Loops\n", "\n", "#### Data Structures\n", "\n", "We learnt some basic types in the previous lesson. There are more advanced types as well, lists and dictionaries.\n", "\n", "A list is, just as its name suggests, a list. It is declared as follows:\n", "\n", " a = [525, 420, 1024, 216, 74]\n", " \n", "It can contain any mix of types, so we could also have a list as follows:\n", "\n", " b = [\"A string\", 56.2, 98, True]\n", " \n", "We access an element in a list by using its index, i.e. the location in the list. Take care though, the first element in an index is at 0. A neat trick is that you can get the last element in the list by getting its -1th element, the second last with -2, etc. e.g.\n", "\n", " c = b[0] # c = \"A string\"\n", " d = b[-1] # d = True\n", "\n", "A similar data type is a dictionary. A dictionary is like a list of 'key: value' pairs. A key difference however is that they're unordered, so you can't access an element by specifying its index. Instead, you get an element by specifying the key. An example is shown below:\n", "\n", " e = {\"electron\": -1.0, \"proton\": 1.0, \"neutron\": 0.0}\n", " proton_charge = e[\"proton\"] # proton_charge = 1.0\n", " \n", "Another feature of Python is shown above. if there is a logical statement surrounded by brackets, even if new lines are made, Python treats it as one continuous line, which can be handy if you're making a long dictionary, or other complex statements." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### While Loops\n", "\n", "We learnt our first control statement in the previous lesson, if/else if. In this lesson, we will learn a few ways to implement a new type of control statement, loops.\n", "\n", "The first type of loop is a `while` loop, shown below:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n", "10\n" ] } ], "source": [ "x = 0\n", "while x <= 10:\n", " print(x)\n", " x += 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The structure of a `while` loop is as follows:\n", "\n", " while boolean expression:\n", " statement_1\n", "\n", "i.e., while the boolean expression is true, the content of the while loop is executed. If we do something like the example above, we need to take care to have x += 1, or some equivalent statement, so that we don't get an infinite loop.\n", "\n", "#### `for` loops\n", "\n", "`for` loops are a different type of loop, which are especially useful when iterating over lists. The next two cells show two examples:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n", "10\n" ] } ], "source": [ "for x in range(0, 11):\n", " # range(0, 11) means that the for loop iterates between 0 and 10\n", " print(x)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n", "10\n" ] } ], "source": [ "a = [0, 1, 2, 3,\n", " 4, 5, 6, 7,\n", " 8, 9, 10]\n", "\n", "for x in a:\n", " print(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "These two examples of for loops provide the same functionality as the while loop above.\n", "\n", "There are many cases where a for loop is interchangable with a while loop, but there are some cases where a for loop may be more suitable, such as:\n", "\n", "* When you want to iterate through a list and perform operations on all of the elements\n", "* When you know you want to iterate for a fixed amount of times\n", "\n", "Conversely, a while loop may be more useful when you want to do something like:\n", "\n", "* Run a loop forever. (We can do this by having while True:)\n", "\n", "In both cases, we can exit a loop early by using break, e.g." ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n" ] } ], "source": [ "for x in range(0,11):\n", " print(x)\n", " if x == 3:\n", " break" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Factorial program\n", "\n", "Using these constructs, we can now write the algorithm from lesson 1 for calculating a factorial in Python." ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3628800\n" ] } ], "source": [ "# input integer n:\n", "n = 10\n", "# Is n = 0 or 1?\n", "if n == 0 or n ==1:\n", " # Yes it is, so factorial = 1\n", " factorial = 1\n", "else:\n", " # No, so factorial = n\n", " factorial = n\n", " while n != 1:\n", " # Decrease n by 1\n", " n -= 1\n", " # Multiply factorial by n\n", " factorial = factorial * n\n", " # If n is not 1, then this loop will continue\n", "# Have factorial, so now print it\n", "print(factorial)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now that you've seen how loops can be used to calculate the factorial, try writing your program for generating a Fibonacci sequence in Python." ] }, { "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" } }, "nbformat": 4, "nbformat_minor": 0 }