{"worksheets": [{"metadata": {}, "cells": [{"cell_type": "markdown", "metadata": {}, "source": ["The `range` function lets us build a list of numbers."]}, {"prompt_number": 12, "input": ["list(range(10, 20))"], "language": "python", "cell_type": "code", "collapsed": false, "outputs": [{"output_type": "pyout", "prompt_number": 12, "text": ["[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]"], "metadata": {}}], "metadata": {}}, {"cell_type": "markdown", "metadata": {}, "source": ["Notice anything funny?\n", "\n", "Python uses this convention everywhere."]}, {"prompt_number": 13, "input": ["a = list(range(10, 20))\n", "type(a)"], "language": "python", "cell_type": "code", "collapsed": false, "outputs": [{"output_type": "pyout", "prompt_number": 13, "text": ["list"], "metadata": {}}], "metadata": {}}, {"cell_type": "markdown", "metadata": {}, "source": ["Let's talk about indexing.\n", "\n", "Indexing in Python starts at 0."]}, {"prompt_number": 14, "input": ["a[0]"], "language": "python", "cell_type": "code", "collapsed": false, "outputs": [{"output_type": "pyout", "prompt_number": 14, "text": ["10"], "metadata": {}}], "metadata": {}}, {"cell_type": "markdown", "metadata": {}, "source": ["And goes from there."]}, {"prompt_number": 15, "input": ["a[1]"], "language": "python", "cell_type": "code", "collapsed": false, "outputs": [{"output_type": "pyout", "prompt_number": 15, "text": ["11"], "metadata": {}}], "metadata": {}}, {"prompt_number": 16, "input": ["a[2]"], "language": "python", "cell_type": "code", "collapsed": false, "outputs": [{"output_type": "pyout", "prompt_number": 16, "text": ["12"], "metadata": {}}], "metadata": {}}, {"cell_type": "markdown", "metadata": {}, "source": ["What do negative numbers do?"]}, {"prompt_number": 6, "input": ["a[-1]"], "language": "python", "cell_type": "code", "collapsed": false, "outputs": [{"output_type": "pyout", "prompt_number": 6, "text": ["19"], "metadata": {}}], "metadata": {}}, {"prompt_number": 17, "input": ["a[-2]"], "language": "python", "cell_type": "code", "collapsed": false, "outputs": [{"output_type": "pyout", "prompt_number": 17, "text": ["18"], "metadata": {}}], "metadata": {}}, {"cell_type": "markdown", "metadata": {}, "source": ["You can get a sub-list by *slicing*."]}, {"prompt_number": 19, "input": ["a[3:7]"], "language": "python", "cell_type": "code", "collapsed": false, "outputs": [{"output_type": "pyout", "prompt_number": 19, "text": ["[13, 14, 15, 16]"], "metadata": {}}], "metadata": {}}, {"cell_type": "markdown", "metadata": {}, "source": ["Start and end are optional."]}, {"prompt_number": 20, "input": ["a[3:]"], "language": "python", "cell_type": "code", "collapsed": false, "outputs": [{"output_type": "pyout", "prompt_number": 20, "text": ["[13, 14, 15, 16, 17, 18, 19]"], "metadata": {}}], "metadata": {}}, {"prompt_number": 21, "input": ["a[:3]"], "language": "python", "cell_type": "code", "collapsed": false, "outputs": [{"output_type": "pyout", "prompt_number": 21, "text": ["[10, 11, 12]"], "metadata": {}}], "metadata": {}}, {"cell_type": "markdown", "metadata": {}, "source": ["Again, notice how the end entry is not included:"]}, {"prompt_number": 23, "input": ["print(a[:3])\n", "print(a[3])"], "language": "python", "cell_type": "code", "collapsed": false, "outputs": [{"output_type": "stream", "text": ["[10, 11, 12]\n", "13\n"], "stream": "stdout"}], "metadata": {}}, {"cell_type": "markdown", "metadata": {}, "source": ["Slicing works on any collection type! (`list`, `tuple`, `str`, `numpy` array)"]}, {"prompt_number": 24, "input": ["a = \"CS357\"\n", "a[-3:]"], "language": "python", "cell_type": "code", "collapsed": false, "outputs": [{"output_type": "pyout", "prompt_number": 24, "text": ["'357'"], "metadata": {}}], "metadata": {}}]}], "nbformat_minor": 0, "nbformat": 3, "metadata": {"signature": "sha256:262f581e2636631fcbaab02f18eb112620837fff61e683114e12aaabb8ee7a57", "name": ""}}