###################### ## Arrays ## ## and Dictionaries ## ###################### # Basically array is a fancy name for list. # An array is a type of variable that contains multiple values instead of only one. ##> a = [4,78,"Some text"] # This creates an array 'a' containing the integers 4 and 78 and the string "Some text" # # In programming you don't count 1, 2, 3 but you start counting from 0: 0, 1, 2, 3, ... # #> print( a[0] ) # This prints the first value of a: 4 #> print( a[2] ) prints the third value of a: "Some text" # # After having created the array you can still change, remove and add entries #> a[0] = True # This changes the first entry to the boolean value True #> a[] = "New value" # This adds "New value" to the end of an the array 'a' #> del a[1] # This removes the second value: 78 # Arrays can contain not only strings, True or False, integers, but also arrays: ##> b = [[2, 'Two'], [458, True], ['Water', [1.6, 948]], [False, 0]] # This type of array is sometimes also referred to as a Matrix #> print( a[0][1] ) prints the second value of the first value: 'Two' #> print( a[-2][-1][0] ) prints the first value of the last value of the second-last value: 1.6 # A dictionary has two values per entry: key and value ##> eng_ger = {"green" : "gruen", "yellow" : "gelb", "blue" : "blau"} # In this case "gruen" is the value that maps onto its key "green". # The german translation of blue can be printed by: #> print( eng_ger["blue"] ) # Adding a new entry / translation can be done like follows: #> eng_ger["black"] = "schwarz" # Dictionaries cannot only link string values to other string values. # They can also link to other dictionaries. For example: #> translate = {"eng_ger" : eng_ger, "eng_spa" : eng_spa} #> print( translate["eng_ger"]["yellow"] ) outputs "gelb" # Task1: Create a dictionary called 'cake' containing the recipe for lemon-coconut-cake (edu.blibspace.com/cake) # eg: "baking powder" - "0.5 packs" # Task2: Create another dictionary called 'cookie' containing the recipe for chocolate-chip-cookies (edu.blibspace.com/cookie) # Task3: Create a recipe booklet linking to the two dictionaries 'cake' and 'cookie' #> print(recipe["cake"]) should output the ingredients and how much of them are needed # Task4: You want to bake two cakes: make a shoppinglist (array) and name it 'bday_carl' # eg: "0.5 packs of baking powder", "125g flower", "butter", ...