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