This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ''' | |
| Lists are a datatype you can use to store a collection of different pieces of information as a sequence under | |
| a single variable name. (Datatypes you've already learned about include strings, numbers, and booleans.) | |
| ''' | |
| # You can assign items to a list with an expression of the form | |
| list_name = [item_1, item_2] | |
| # with the items in between brackets. A list can also be empty: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ''' | |
| You can access an individual item on the list by its index. An index is like an address that identifies the | |
| item's place in the list. The index appears directly after the list name, in between brackets, like this: | |
| ''' | |
| list_name[index] | |
| ''' | |
| List indices begin with 0, not 1! You access the first item in a list like this: list_name[0]. | |
| The second item in a list is at index 1: list_name[1]. Computer scientists love to start counting from zero. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # A list doesn't have to have a fixed length. You can add items to the end of a list any time you like! | |
| letters = ['a', 'b', 'c'] | |
| letters.append('d') | |
| print len(letters) | |
| print letters | |
| # example | |
| suitcase = [] | |
| suitcase.append("sunglasses") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Sometimes you need to search for an item in a list. | |
| animals = ["ant", "bat", "cat"] | |
| print animals.index("bat") | |
| # First, we create a list called animals with three strings.. | |
| # Then, we print the first index that contains the string "bat", which will print 1. | |
| # We can also insert items into a list. | |
| animals.insert(1, "dog") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # If you want to do something with every item in the list, you can use a for loop | |
| for variable in list_name: | |
| # Do stuff! | |
| # A variable name follows the for keyword; it will be assigned the value of each list item in turn. | |
| # Then in list_name designates list_name as the list the loop will work on. The line ends with a colon (:) | |
| # and the indented code that follows it will be executed once per item in the list. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ############## | |
| # dictionary # | |
| ############## | |
| # A dictionary is similar to a list, but you access values by looking up a key instead of an index. | |
| # A key can be any string or number. Dictionaries are enclosed in curly braces, like so: | |
| d = {'key1' : 1, 'key2' : 2, 'key3' : 3} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Sometimes you need to remove something from a list. | |
| # example | |
| beatles = ["john","paul","george","ringo","stuart"] | |
| beatles.remove("stuart") | |
| print beatles | |
| backpack = ['xylophone', 'dagger', 'tent', 'bread loaf'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Let's go over a few last notes about dictionaries | |
| my_dict = { | |
| "fish": ["c", "a", "r", "p"], | |
| "cash": -4483, | |
| "luck": "good" | |
| } | |
| print my_dict["fish"][0] | |
| # 1. In the example above, we created a dictionary that holds many types of values. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # for loops allow us to iterate through all of the elements in a list from the left-most (or zeroth element) | |
| # to the right-most element. A sample loop would be structured as follows: | |
| a = ["List of some sort”] | |
| for x in a: | |
| # Do something for every x | |
| # This loop will run all of the code in the indented block under the for x in a: statement. | |
| # The item in the list that is currently being evaluated will be x. So running the following: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # You can also use a for loop on a dictionary to loop through its keys with the following: | |
| # A simple dictionary | |
| d = {"foo" : "bar"} | |
| for key in d: | |
| print d[key] # prints "bar" | |
| # Note that dictionaries are unordered, meaning that any time you loop through a dictionary, | |
| # you will go through every key, but you are not guaranteed to get them in any particular order. |