{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Chapter 3\n", "## Introducing Lists" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### What is a List?" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['trek', 'cannondale', 'redline', 'specialized']\n" ] } ], "source": [ "bicycles = ['trek', 'cannondale', 'redline', 'specialized']\n", "print(bicycles)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Accessing elements in a list" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "trek\n" ] } ], "source": [ "print(bicycles[0])" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Trek\n" ] } ], "source": [ "print(bicycles[0].title())" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "cannondale\n", "specialized\n" ] } ], "source": [ "print(bicycles[1])\n", "print(bicycles[3])" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "specialized\n" ] } ], "source": [ "print(bicycles[-1])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Using individual values from a list" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "My first bicycle was a Trek.\n" ] } ], "source": [ "message = 'My first bicycle was a ' + bicycles[0].title() + '.'\n", "print(message)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Try it yourself" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Emily\n", "Ray\n", "Anthony\n", "Kate\n", "Margo\n", "Stephen\n", "Reed\n" ] } ], "source": [ "# 3-1. Names: Store the names of a few of your friends in a list\n", "# called names. Print each person's name by accessing each element in the list,\n", "# one at a time\n", "friend_names = ['Emily', 'Ray', 'Anthony', 'Kate', 'Margo', 'Stephen', 'Reed']\n", "print(friend_names[0])\n", "print(friend_names[1])\n", "print(friend_names[2])\n", "print(friend_names[3])\n", "print(friend_names[4])\n", "print(friend_names[5])\n", "print(friend_names[6])" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello Emily! You cool.\n" ] } ], "source": [ "# 3-2. Greetings: Start with the list you used in Exercise 3-1,\n", "# but instead of just printing each person's name, print a message to them.\n", "# The text of each message should be the same, but each message should be\n", "# personalized with the person's name.\n", "message = 'Hello ' + friend_names[0].title() + '! You cool.'\n", "print(message)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I want to be in a tank right now.\n", "I want to be in a jet right now.\n" ] } ], "source": [ "# 3-3. Your own list: Think of your favorite mode of transportation,\n", "# such as a motorcycle or a car, and make a list that stores several examples.\n", "# Use your list to print a series of statements about these items, such as\n", "# \"I would like to own a Honda motorcycle\"\n", "very_large_vehicles = ['tank', 'jet', 'yacht', 'blimp']\n", "message = 'I want to be in a ' + very_large_vehicles[0] + ' right now.'\n", "print(message)\n", "message = 'I want to be in a ' + very_large_vehicles[1] + ' right now.'\n", "print(message)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Changing, adding and removing elements" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Modifying elements in a list" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['honda', 'yamaha', 'suzuki']\n", "['ducati', 'yamaha', 'suzuki']\n" ] } ], "source": [ "motorcycles = ['honda', 'yamaha', 'suzuki']\n", "print(motorcycles)\n", "motorcycles[0] = 'ducati'\n", "print(motorcycles)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Adding elements to a list" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['honda', 'yamaha', 'suzuki']\n", "['honda', 'yamaha', 'suzuki', 'ducati']\n" ] } ], "source": [ "motorcycles = ['honda', 'yamaha', 'suzuki']\n", "print(motorcycles)\n", "motorcycles.append('ducati')\n", "print(motorcycles)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['honda', 'yamaha', 'suzuki']\n" ] } ], "source": [ "motorcycles = []\n", "motorcycles.append('honda')\n", "motorcycles.append('yamaha')\n", "motorcycles.append('suzuki')\n", "\n", "print(motorcycles)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['ducati', 'honda', 'yamaha', 'suzuki']\n" ] } ], "source": [ "motorcycles = ['honda', 'yamaha', 'suzuki']\n", "motorcycles.insert(0, 'ducati')\n", "print(motorcycles)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Removing elements from a list" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['honda', 'yamaha', 'suzuki']\n", "['yamaha', 'suzuki']\n" ] } ], "source": [ "motorcycles = ['honda', 'yamaha', 'suzuki']\n", "print(motorcycles)\n", "del motorcycles[0]\n", "print(motorcycles)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['honda', 'yamaha', 'suzuki']\n", "['honda', 'yamaha']\n", "suzuki\n" ] } ], "source": [ "motorcycles = ['honda', 'yamaha', 'suzuki']\n", "print(motorcycles)\n", "popped_motorcycle = motorcycles.pop()\n", "print(motorcycles)\n", "print(popped_motorcycle)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The last motorcycle I owned was a Suzuki.\n" ] } ], "source": [ "motorcycles = ['honda', 'yamaha', 'suzuki']\n", "last_owned_motorcycle = motorcycles.pop()\n", "print('The last motorcycle I owned was a ' + last_owned_motorcycle.title() + '.')" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The first motorcycle I owned was a Honda.\n" ] } ], "source": [ "motorcycles = ['honda', 'yamaha', 'suzuki']\n", "first_owned_motorcycle = motorcycles.pop(0)\n", "print('The first motorcycle I owned was a ' + first_owned_motorcycle.title() + '.')" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['honda', 'yamaha', 'suzuki', 'ducati']\n", "['honda', 'yamaha', 'suzuki']\n" ] } ], "source": [ "motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati']\n", "print(motorcycles)\n", "motorcycles.remove('ducati')\n", "print(motorcycles)" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['honda', 'yamaha', 'suzuki', 'ducati']\n", "['honda', 'yamaha', 'suzuki']\n", "\n", "A Ducati is too expensive for me.\n" ] } ], "source": [ "motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati']\n", "print(motorcycles)\n", "too_expensive_motorcycle = 'ducati'\n", "motorcycles.remove(too_expensive_motorcycle)\n", "print(motorcycles)\n", "print('\\nA ' + too_expensive_motorcycle.title() + ' is too expensive for me.')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Try it yourself" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Dear Carl Sagan, you are cordially invited to dinner with me at Tenuki at 8pm Saturday.\n" ] } ], "source": [ "# 3-4. Guest list: If you could invite anyone, living or deceased, to dinner, who would you invite?\n", "# Make a list that includes at least three people you'd like to invite to dinner.\n", "# Then use your list to print a message to each person, inviting them to dinner\n", "\n", "dream_dinner_guests = ['Carl Sagan', 'Dolly Parton', 'Nicolai Tesla', 'David Bowie']\n", "message = 'Dear ' + dream_dinner_guests[0].title() + ', you are cordially invited to dinner with me at Tenuki at 8pm Saturday.'\n", "print(message)" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Unfortunately, David Bowie will be unable to attend dinner on Saturday.\n", "['Carl Sagan', 'Dolly Parton', 'Nicolai Tesla', 'Bill Murray']\n" ] } ], "source": [ "# 3-5. Changing guest list: You just heard that one of your guests can't make\n", "# the dinner, so you need to send out a new set of invitations. You'll have to\n", "# think of someone else to invite.\n", "unavailable_guest = 'David Bowie'\n", "message = 'Unfortunately, ' + unavailable_guest + ' will be unable to attend dinner on Saturday.'\n", "print(message)\n", "replacement_guest = 'Bill Murray'\n", "dream_dinner_guests.remove(unavailable_guest)\n", "dream_dinner_guests.append(replacement_guest)\n", "print(dream_dinner_guests)" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hey, Carl Sagan, Dolly Parton, Nicolai Tesla, I found a bigger dinner table\n", "['Ada Lovelace', 'Carl Sagan', 'David Lynch', 'Dolly Parton', 'Nicolai Tesla', 'Bill Murray', 'Tony Danza']\n" ] } ], "source": [ "# 3-6. More guests: You just found a bigger dinner table, so now more space is available.\n", "# Think of three more guests to invite to dinner\n", "\n", "# Start with your program from 3-4 or 3-5.\n", "# Add a print statement to the end informing people that you found a bigger table.\n", "\n", "new_guests = ['Ada Lovelace', 'David Lynch', 'Tony Danza']\n", "message = 'Hey, ' + dream_dinner_guests[0] + ', ' + dream_dinner_guests[1] + ', ' + dream_dinner_guests[2] + ', I found a bigger dinner table'\n", "print(message)\n", "\n", "# Use insert() to add one new guest to the beginning of you list\n", "dream_dinner_guests.insert(0, new_guests[0])\n", "# Use insert() to add one new guest to the middle of you list\n", "dream_dinner_guests.insert(2, new_guests[1])\n", "# Use append() to add one new guest to the end of you list\n", "dream_dinner_guests.append(new_guests[2])\n", "print(dream_dinner_guests)" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Sorry y'all, I can only invite two people to dinner\n" ] } ], "source": [ "# 3-7. Shrinking guest list: You just found out that your new dinner table\n", "# won't arrive in time for the dinner, and you have space for only two guests.\n", "\n", "# Start with 3-6. Add a new line that prints a message saying that you can invite only two people for dinner.\n", "\n", "message = 'Sorry y\\'all, I can only invite two people to dinner'\n", "print(message)" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I am sorry to inform you, Tony Danza, that I will be unable to accomadate you for the Saturday dinner.\n", "['Ada Lovelace', 'Carl Sagan']\n" ] } ], "source": [ "# Use pop() to remove guests from you list one at a time until only two names remain in your list.\n", "# Each time you pop a name from your list, print a message to that person letting them\n", "# know you're sorry you can't invite them to dinner.\n", "\n", "uninvited_guest = dream_dinner_guests.pop()\n", "print('I am sorry to inform you, ' + uninvited_guest.title() + ', that I will be unable to accomadate you for the Saturday dinner.')\n", "uninvited_guest = dream_dinner_guests.pop()\n", "uninvited_guest = dream_dinner_guests.pop()\n", "uninvited_guest = dream_dinner_guests.pop()\n", "uninvited_guest = dream_dinner_guests.pop()\n", "print(dream_dinner_guests)" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [], "source": [ "# use del to remove the last two names from your list, so you have an empty list.\n", "# Print your list to make sure you actually have an empty list at the end of your program\n", "del dream_dinner_guests[0]\n", "del dream_dinner_guests[0]" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[]\n" ] } ], "source": [ "print(dream_dinner_guests)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Organizing a list" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['audi', 'bmw', 'subaru', 'toyota']\n" ] } ], "source": [ "cars = ['bmw', 'audi', 'toyota', 'subaru']\n", "cars.sort()\n", "print(cars)" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['toyota', 'subaru', 'bmw', 'audi']\n" ] } ], "source": [ "cars.sort(reverse=True)\n", "print(cars)" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Here is the original list:\n", "['bmw', 'audi', 'toyota', 'subaru']\n", "\n", "Here is the sorted list:\n", "['audi', 'bmw', 'subaru', 'toyota']\n", "Here is the original list again:\n", "['bmw', 'audi', 'toyota', 'subaru']\n" ] } ], "source": [ "cars = ['bmw', 'audi', 'toyota', 'subaru']\n", "print('Here is the original list:')\n", "print(cars)\n", "\n", "print('\\nHere is the sorted list:')\n", "print(sorted(cars))\n", "\n", "print('Here is the original list again:')\n", "print(cars)\n" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['bmw', 'audi', 'toyota', 'subaru']\n", "['subaru', 'toyota', 'audi', 'bmw']\n" ] } ], "source": [ "print(cars)\n", "\n", "cars.reverse()\n", "print(cars)" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(cars)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Try it yourself" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['la', 'thailand', 'berlin', 'japan', 'paris']\n", "['berlin', 'japan', 'la', 'paris', 'thailand']\n", "['la', 'thailand', 'berlin', 'japan', 'paris']\n", "['thailand', 'paris', 'la', 'japan', 'berlin']\n", "['la', 'thailand', 'berlin', 'japan', 'paris']\n", "['paris', 'japan', 'berlin', 'thailand', 'la']\n", "['la', 'thailand', 'berlin', 'japan', 'paris']\n", "['berlin', 'japan', 'la', 'paris', 'thailand']\n", "['thailand', 'paris', 'la', 'japan', 'berlin']\n" ] } ], "source": [ "# 3-8. Seeing the world: Think of at least five places in the world you'd like to visit.\n", "\n", "# Store the locations in a list. Make sure the list is not in alphabetical order.\n", "\n", "places_to_visit = ['la', 'thailand', 'berlin', 'japan', 'paris']\n", "\n", "# Print your list in its original order. Don't worry about printing the list neatly. just print it as a raw Python list\n", "\n", "print(places_to_visit)\n", "\n", "# Use `sorted()` to print your list in alphabetical order without modifying the actual list\n", "\n", "print(sorted(places_to_visit))\n", "\n", "# Show that your list is still in its original order by printing it\n", "\n", "print(places_to_visit)\n", "\n", "# Use `sorted()` to print you list in reverse alphabetical order without changing the order of the original list\n", "\n", "print(sorted(places_to_visit, reverse=True))\n", "\n", "# Show that your list is still in its original order by printing it again.\n", "\n", "print(places_to_visit)\n", "\n", "# Use `reverse()` to change the order of your list. Print the list to show that its order has changed\n", "\n", "places_to_visit.reverse()\n", "\n", "print(places_to_visit)\n", "\n", "# Use `reverse()` to change the order of your list again. Print the list to show it's back to its original order\n", "\n", "places_to_visit.reverse()\n", "\n", "print(places_to_visit)\n", "\n", "# Use `sort()` to change your list so it's stored in alphabetical order.\n", "# Print the list to show that its order has been changed.\n", "\n", "places_to_visit.sort()\n", "\n", "print(places_to_visit)\n", "\n", "# Use `sort()` to change your list so it's stored in reverse alphabetical order.\n", "# Print the list to show that its order has changed.\n", "\n", "\n", "places_to_visit.sort(reverse=True)\n", "\n", "print(places_to_visit)" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I am inviting 4 guests to dinner.\n" ] } ], "source": [ "# 3-9. Dinner guests: Working with one of the programs from 3-4 through 3-7, use `len()` to print a message indicating the number of people you are inviting to dinner.\n", "dream_dinner_guests = ['Carl Sagan', 'Dolly Parton', 'Nicolai Tesla', 'David Bowie']\n", "print('I am inviting ' + str(len(dream_dinner_guests)) + ' guests to dinner.')" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Original list:\n", "['Hungry Heart', 'Bipartisan', 'The Fillmore', 'Heart']\n", "Sorted (but unchanged) list:\n", "['Bipartisan', 'Heart', 'Hungry Heart', 'The Fillmore']\n", "Reverse-sorted (but unchanged) list:\n", "['The Fillmore', 'Hungry Heart', 'Heart', 'Bipartisan']\n", "List sorted in-place:\n", "\tBefore:\n", "['Hungry Heart', 'Bipartisan', 'The Fillmore', 'Heart']\n", "\tAfter:\n", "['Bipartisan', 'Heart', 'Hungry Heart', 'The Fillmore']\n", "Reversed (in-place):\n", "['The Fillmore', 'Hungry Heart', 'Heart', 'Bipartisan']\n", "... which should be the same as reversing back and then reverse-sorting in-place:\n", "['The Fillmore', 'Hungry Heart', 'Heart', 'Bipartisan']\n" ] } ], "source": [ "# 3-10. Every function: think of something you could store in a list.\n", "# E.g. you could make a list of mountains, rivers, countries, cities, languages or anything else you'd like.\n", "# Write a program that creates a list containing these items and uses each function introduced\n", "# in this chapter at least once.\n", "\n", "coffee_shops = ['Hungry Heart', 'Bipartisan', 'The Fillmore', 'Heart']\n", "print('Original list:')\n", "print(coffee_shops)\n", "print('Sorted (but unchanged) list:')\n", "print(sorted(coffee_shops))\n", "print('Reverse-sorted (but unchanged) list:')\n", "print(sorted(coffee_shops, reverse=True))\n", "print('List sorted in-place:')\n", "print('\\tBefore:')\n", "print(coffee_shops)\n", "print('\\tAfter:')\n", "coffee_shops.sort()\n", "print(coffee_shops)\n", "print('Reversed (in-place):')\n", "coffee_shops.reverse()\n", "print(coffee_shops)\n", "print('... which should be the same as reversing back and then reverse-sorting in-place:')\n", "coffee_shops.reverse()\n", "coffee_shops.sort(reverse=True)\n", "print(coffee_shops)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Avoiding index errors when working with lists" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "ename": "IndexError", "evalue": "list index out of range", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# generate an index-error\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mmotorcycles\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m'honda'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'yamaha'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'suzuki'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmotorcycles\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mIndexError\u001b[0m: list index out of range" ] } ], "source": [ "# generate an index-error\n", "motorcycles = ['honda', 'yamaha', 'suzuki']\n", "print(motorcycles[3])" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "suzuki\n" ] } ], "source": [ "# What we meant to do:\n", "print(motorcycles[-1])" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "ename": "IndexError", "evalue": "list index out of range", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# generate another index-error\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mmotorcycles\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmotorcycles\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mIndexError\u001b[0m: list index out of range" ] } ], "source": [ "# generate another index-error\n", "motorcycles = []\n", "print(motorcycles[-1])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Try it yourself" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "ename": "IndexError", "evalue": "list index out of range", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# 3-11. Intentional error: Make an index error happen.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mcoffee_shops\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m'Hungry Heart'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'Bipartisan'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'The Fillmore'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'Heart'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcoffee_shops\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m4\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mIndexError\u001b[0m: list index out of range" ] } ], "source": [ "# 3-11. Intentional error: Make an index error happen.\n", "coffee_shops = ['Hungry Heart', 'Bipartisan', 'The Fillmore', 'Heart']\n", "print(coffee_shops[4])" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Heart\n", "Heart\n" ] } ], "source": [ "# Correct the error.\n", "print(coffee_shops[3])\n", "print(coffee_shops[-1])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.6.2" } }, "nbformat": 4, "nbformat_minor": 2 }