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
    
  
  
    
  | # 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
    
  
  
    
  | # 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
    
  
  
    
  | # 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
    
  
  
    
  | ''' | |
| 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
    
  
  
    
  | ''' | |
| 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
    
  
  
    
  | # What if we went to Los Angeles for 5 days and brought an extra 600 dollars of spending money? | |
| def hotel_cost(nights): | |
| return 140 * nights | |
| def plane_ride_cost(city): | |
| if city == "Charlotte": | |
| return (183) | 
  
    
      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
    
  
  
    
  | ## Don't forget the colon at the end of the function definition! | |
| def shut_down(s): | |
| if s=="yes": | |
| return("Shutting down") | |
| elif s=="no": | |
| return("Shutdown aborted") | 
  
    
      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
    
  
  
    
  | import math # Imports the math module | |
| everything = dir(math) # Sets everything to a list of things from math | |
| print everything # Prints 'em all! | 
  
    
      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
    
  
  
    
  | """ | |
| Here is a Python module named math that includes a number of useful variables and functions, | |
| and sqrt() is one of those functions. In order to access math, all you need is the import keyword. | |
| """ | |
| #Generic import: Ask Python to print sqrt(25) | |
| import math | |
| print math.sqrt(25) |