{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Copyright 2014 Brett Slatkin, Pearson Education Inc.\n", "#\n", "# Licensed under the Apache License, Version 2.0 (the \"License\");\n", "# you may not use this file except in compliance with the License.\n", "# You may obtain a copy of the License at\n", "#\n", "# http://www.apache.org/licenses/LICENSE-2.0\n", "#\n", "# Unless required by applicable law or agreed to in writing, software\n", "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", "# See the License for the specific language governing permissions and\n", "# limitations under the License.\n", "\n", "# Preamble to mimick book environment\n", "import logging\n", "from pprint import pprint\n", "from sys import stdout as STDOUT" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "First four: ['a', 'b', 'c', 'd']\n", "Last four: ['e', 'f', 'g', 'h']\n", "Middle two: ['d', 'e']\n" ] } ], "source": [ "# Example 1\n", "a = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']\n", "print('First four:', a[:4])\n", "print('Last four: ', a[-4:])\n", "print('Middle two:', a[3:-3])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### No need to use 0" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "# Example 2\n", "print (a[:5] == a[0:5])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### No need to use the last value" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "# Example 3\n", "print(a[5:] == a[5:len(a)])" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['a', 'b', 'c', 'd', 'e']\n", "['a', 'b', 'c', 'd', 'e', 'f', 'g']\n", "['e', 'f', 'g', 'h']\n", "['f', 'g', 'h']\n", "['c', 'd', 'e']\n", "['c', 'd', 'e', 'f', 'g']\n", "['f', 'g']\n" ] } ], "source": [ "# Example 4\n", "print(a[:5])\n", "print(a[:-1])\n", "print(a[4:])\n", "print(a[-3:])\n", "print(a[2:5])\n", "print(a[2:-1])\n", "print(a[-3:-1])" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']\n", "['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']\n", "['a', 'b', 'c', 'd', 'e']\n", "['a', 'b', 'c', 'd', 'e', 'f', 'g']\n", "['e', 'f', 'g', 'h']\n", "['f', 'g', 'h']\n", "['c', 'd', 'e']\n", "['c', 'd', 'e', 'f', 'g']\n", "['f', 'g']\n" ] } ], "source": [ "# Example 5\n", "a = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']\n", "print(a)\n", "print(a[:]) # ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']\n", "print(a[:5]) # ['a', 'b', 'c', 'd', 'e']\n", "print(a[:-1]) # ['a', 'b', 'c', 'd', 'e', 'f', 'g']\n", "print(a[4:]) # ['e', 'f', 'g', 'h']\n", "print(a[-3:]) # ['f', 'g', 'h']\n", "print(a[2:5]) # ['c', 'd', 'e']\n", "print(a[2:-1]) # ['c', 'd', 'e', 'f', 'g']\n", "print(a[-3:-1]) # ['f', 'g']" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']\n", "['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']\n" ] } ], "source": [ "# Example 6\n", "first_twenty_items = a[:20]\n", "print(first_twenty_items)\n", "last_twenty_items = a[-20:]\n", "print(last_twenty_items)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "logged\n" ] } ], "source": [ "# Example 7\n", "try:\n", " print(a[200])\n", "except:\n", " logging.exception('Expected')\n", " print(\"logged\")\n", "else:\n", " assert(False)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Before: ['e', 'f', 'g', 'h']\n", "After: ['e', 99, 'g', 'h']\n", "No change: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']\n" ] } ], "source": [ "# Example 8\n", "b = a[4:]\n", "print('Before: ', b)\n", "b[1] = 99\n", "print('After: ', b)\n", "print('No change:', a)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Before ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']\n", "After ['a', 'b', 99, 22, 14, 'h']\n" ] } ], "source": [ "# Example 9\n", "print('Before ', a)\n", "a[2:7] = [99, 22, 14]\n", "print('After ', a)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "# Example 10\n", "b = a[:]\n", "print (b == a and b is not a)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Before ['a', 'b', 99, 22, 14, 'h']\n", "After [101, 102, 103]\n" ] } ], "source": [ "# Example 11\n", "b = a\n", "print('Before', a)\n", "a[:] = [101, 102, 103]\n", "assert a is b # Still the same list object\n", "print('After ', a) # Now has different contents" ] } ], "metadata": { "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 }