{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "These notes follow the official python tutorial pretty closely: http://docs.python.org/3/tutorial/" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from __future__ import print_function" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Lists" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Lists group together data. Many languages have arrays (we'll look at those in a bit in python). But unlike arrays in most languages, lists can hold data of all different types -- they don't need to be homogeneos. The data can be a mix of integers, floating point or complex #s, strings, or other objects (including other lists).\n", "\n", "A list is defined using square brackets:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": true }, "outputs": [], "source": [ "a = [1, 2.0, \"my list\", 4]" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2.0, 'my list', 4]\n" ] } ], "source": [ "print(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can index a list to get a single element -- remember that python starts counting at 0:" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "my list\n" ] } ], "source": [ "print(a[2])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Like with strings, mathematical operators are defined on lists:" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2.0, 'my list', 4, 1, 2.0, 'my list', 4]\n" ] } ], "source": [ "print(a*2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `len()` function returns the length of a list" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n" ] } ], "source": [ "print(len(a))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Unlike strings, lists are _mutable_ -- you can change elements in a list easily" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[1, -2.0, 'my list', 4]" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[1] = -2.0\n", "a" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[-1, -2.1, -2.0, 'my list', 4]" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[0:1] = [-1, -2.1] # this will put two items in the spot where 1 existed before\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that lists can even contain other lists:" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[-1, ['other list', 3], -2.0, 'my list', 4]" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[1] = [\"other list\", 3]\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Just like everything else in python, a list is an object that is the instance of a class. Classes have methods (functions) that know how to operate on an object of that class.\n", "\n", "There are lots of methods that work on lists. Two of the most useful are append, to add to the end of a list, and pop, to remove the last element:" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[-1, ['other list', 3], -2.0, 'my list', 4, 6]" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.append(6)\n", "a" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.pop()" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[-1, ['other list', 3], -2.0, 'my list', 4]" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "