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