Skip to content

Instantly share code, notes, and snippets.

@Michael0x2a
Created July 23, 2013 03:06
Show Gist options
  • Save Michael0x2a/6059559 to your computer and use it in GitHub Desktop.
Save Michael0x2a/6059559 to your computer and use it in GitHub Desktop.
Programming Lessons 2013: Lesson 4, Lists
{
"metadata": {
"name": "lesson-4"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Lesson 4 #\n",
"Saturday, July 20, 2013"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Today's goals ##\n",
"\n",
"* Learn about lists\n",
"* Practice using lists\n",
"* Write a game of hangman\n",
"\n",
"## About lists ##\n",
"\n",
"What is a list?\n",
"\n",
"A list is a collection of strings, numbers, or other data all in a row. An example of a list might be a todo list, or a shopping list:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"shopping_list = [\"bacon\", \"milk\", \"lettuce\"]"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As you notice, you can make a list by using two square `[ ]` brackets, and putting any string, number, or variable inside. Anything you put inside must be separated by commas.\n",
"\n",
"> You might be wondering why this is useful. Remember when we learned about `for`-loops and `while`-loops? They helped us avoid repeating code, and avoid copy-and-pasting. Lists are another tool that helps us avoid repeating code. As we do the exercises below, hopefully you'll learn why lists are so useful.\n",
"\n",
"You can get individual items in a list by putting square `[ ]` brackets at the **end** of the variable, and putting in the number item you want to get."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"first_item = shopping_list[0]\n",
"second_item = shopping_list[1]\n",
"\n",
"print first_item\n",
"print second_item\n",
"print shopping_list[2]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"bacon\n",
"milk\n",
"lettuce\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Notice how the number `0` gets the first item. That's because computers start counting from the number zero, not the number one. You can also change items inside lists:\n",
"\n"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print shopping_list\n",
"shopping_list[2] = \"eggs\"\n",
"print shopping_list"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"['bacon', 'milk', 'lettuce']\n",
"['bacon', 'milk', 'eggs']\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Remember how I said variables were like labeled boxes where you can put in strings or numbers?\n",
"\n",
"Well, a list is like a bunch of labeled boxes in a row -- sort of like a bunch of mailboxes. Doing `my_list = [\"cat\", \"bye\", \"dog\"]` is sort of like making a bunch of mailboxes, calling the entire thing `my_list`, and putting strings into each of the boxes.\n",
"\n",
" \"cat\" \"bye\" \"dog\"\n",
" | | |\n",
" V V V\n",
" +-------+-------+-------+\n",
" | Box 0 | Box 1 | Box 2 |\n",
" +-------+-------+-------+\n",
"\n",
" my_list"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise: ###\n",
"\n",
"What does the below code do?"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"todo_list = [\"wake up\", \"brush teeth\", \"watch tv\"]\n",
"print todo_list\n",
"print todo_list[2]\n",
"print todo_list[0]\n",
"\n",
"todo_list[0] = \"break the alarm clock\"\n",
"print todo_list\n",
"print todo_list[0]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"['wake up', 'brush teeth', 'watch tv']\n",
"watch tv\n",
"wake up\n",
"['break the alarm clock', 'brush teeth', 'watch tv']\n",
"break the alarm clock\n"
]
}
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Other things you can do with lists ##\n",
"\n",
"You can create empty lists:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"foo = []"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can add new data to lists by using the `append` command:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"foo.append(\"hello\")\n",
"foo.append(\"world\")\n",
"foo.append(25)\n",
"\n",
"test = \"what is this\"\n",
"foo.append(test)\n",
"\n",
"print foo"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"['hello', 'world', 25, 'what is this']\n"
]
}
],
"prompt_number": 6
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You might be wondering what that period in `foo.append(25)` is there for. Basically, it means, \"call the `append` function that belongs to `foo`\". It's sort of like `turtle.forward(100)` -- \"call the `forward` function that belongs to the `turtle`\".\n",
"\n",
"To do the opposite (remove the very last item in a list), use the `pop` command:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"vanishing = [\"Ricardo\", \"the\", \"amazing\", \"rabbit\"]\n",
"print vanishing\n",
"vanishing.pop()\n",
"print vanishing"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"['Ricardo', 'the', 'amazing', 'rabbit']\n",
"['Ricardo', 'the', 'amazing']\n"
]
}
],
"prompt_number": 7
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can use the `len` function to get how many items there are inside a list:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print foo\n",
"print len(foo)\n",
"\n",
"print shopping_list\n",
"print len(shopping_list)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"['hello', 'world', 25, 'what is this']\n",
"4\n",
"['bacon', 'milk', 'eggs']\n",
"3\n"
]
}
],
"prompt_number": 8
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(You might be wondering why it isn't `foo.len`. It's because you can get the length of a lot of different things, beyond only lists.)\n",
"\n",
"There are a lot more things you can do with lists, including inserting a new value in the middle of a list, deleting an item, getting a smaller list from an existing list, and combining lists.\n",
"\n",
"Let me know if there's anything in particular you want to know about/need, and I'll give you a quick demo."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise ###\n",
"\n",
"Let's write a shopping list program!\n",
"\n",
"Instructions:\n",
"\n",
"Imagine you're going shopping, and don't want to figure out how much you're going to pay. Write a program that:\n",
"\n",
"1. Asks the user for the name of what they want to buy, and then asks for the price.\n",
"2. Keep asking the user until the user types in \"quit\" instead of a grocery item.\n",
"3. Then, display all the items the user wants to buy and their total cost.\n",
"\n",
"Bonus challenge:\n",
"\n",
"1. Remove the last grocery item if the user types in \"undo\" instead of what they want to buy."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Other useful features of lists ##\n",
"\n",
"### Lists and strings ###\n",
"\n",
"If you think about it, a string is sort of like a list that contains just a bunch of letters. In fact, you can use strings the same way you use lists."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"name = \"Daniel\"\n",
"print name[0]\n",
"print name[1]\n",
"print name[2]\n",
"print len(name)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"D\n",
"a\n",
"n\n",
"6\n"
]
}
],
"prompt_number": 9
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"However, strings are not identical to lists. The `append` and `pop` functions will not work on strings. (You have to use the `+` sign: `var = \"Testin\" + \"g\")`"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Lists and `for`-loops ###\n",
"\n",
"Did you know, you can use `for`-loops to loop over a list?"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"gibberish = [\"abc\", \"def\", \"ghi\"]\n",
"for word in gibberish:\n",
" print word\n",
" \n",
"vowels = \"aeoiu\"\n",
"for v in vowels:\n",
" print v"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"abc\n",
"def\n",
"ghi\n",
"a\n",
"e\n",
"o\n",
"i\n",
"u\n"
]
}
],
"prompt_number": 10
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Remember earlier, when we did something like:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"for i in range(5):\n",
" print i"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"0\n",
"1\n",
"2\n",
"3\n",
"4\n"
]
}
],
"prompt_number": 11
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Well, `range` is actually a special function that returns a list."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print range(5)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[0, 1, 2, 3, 4]\n"
]
}
],
"prompt_number": 12
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The below two loops are 100% identical:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"for i in range(5):\n",
" print i"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"0\n",
"1\n",
"2\n",
"3\n",
"4\n"
]
}
],
"prompt_number": 13
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"loopy = [0, 1, 2, 3, 4]\n",
"for i in loopy:\n",
" print i"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"0\n",
"1\n",
"2\n",
"3\n",
"4\n"
]
}
],
"prompt_number": 14
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Testing what's inside a list ###\n",
"\n",
"You can find out if something already exists inside a list by using the `in` operator or the `not in` operator:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"favorite_numbers = [0, 1, 4, 42]\n",
"print 4 in favorite_numbers\n",
"print 13 in favorite_numbers"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"True\n",
"False\n"
]
}
],
"prompt_number": 15
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print 4 not in favorite_numbers\n",
"print 5 not in favorite_numbers"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"False\n",
"True\n"
]
}
],
"prompt_number": 16
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise ###\n",
"\n",
"Write a program that first asks the user to enter in any random word.\n",
"Replace every vowel in the word with an underscore, and return the new word.\n",
"\n",
"For example, if I type in `notebook`, the program will print `n_t_b__k`."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Project: Hangman ##\n",
"\n",
"Write a program that plays hangman. \n",
"\n",
"This might be a little difficult. That's fine -- I deliberately picked something that was a little challenging. Try doing the below steps first before starting to write your program.\n",
"\n",
"1. Write down in English how to play hangman. Keep changing the instructions until they are very specific.\n",
"2. Brainstorm all the different pieces or functions you might need to play hangman. For example, you might need function that picks a random word. You might need a function that takes a word, letters the user has guessed before, and replaces letters the user hasn't guessed with underscores. You might need a function to tell if you've won. etc. Brainstorm for at least 5 minutes.\n",
"3. If you get stuck or need a hint, come and ask me, but **only after** first trying steps 1 and 2."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## "
]
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment