----------------- Global and local Variables count = 0 def countUp(): count += 1 return count print(countUp) ----------------- ----------------- Dictionary is like a list but with named indices called keys The dictionary is map between keys and values Every item is a key-value pair "one" => "uno" "two" => "dos" "three" = > "tres" >>> eng2sp = {"one":"uno", "two":"dos", "three":"tres"} >>> print(eng2sp) >>> {'three': 'tres', 'two': 'dos', 'one': 'uno'} ----------------- ----------------- We can check if a key exists in the dictionary by using the in operator if 'one' in eng2sp: print("true") if 'uno' in eng2sp: print("true") if 'uno' not in eng2sp: print("true") ----------------- dict.values() returns a list of all the values in the dictionary values = eng2sp.values() print(values) ["uno", "dos", "tres"] if 'uno' in values: print("true") ----------------- ----------------- Counting the occurence of letters in a string def histogram(string): # countLetters takes 1 argument, any string # it returns a dictionary with the frequency of each letter in the string # create an empty dictionart d = dict() # iterate through each character in the string for char in string: # if it is the first time we see the character, set its count to 1 if char not in d: d[char] = 1 # if the count for character is already in the dictionary, add 1 to it else: d[char] += 1 # return the resulting dictionary so we can save it and use it later return d ----------------- ----------------- # Print the histogram def print_histogram(h): # Iterate thrugh each key, and print the key and the value for key in h: print(key, h[key]) ----------------- ----------------- Opening a file in your directory file_handle = open("filename", mode) Store the entire file in a string whole_thing = file_handle.read() Storing it as a list of lines lines = file_handle.readlines() file_handle.close() ----------------- ----------------- Modes "r" Read from a file, crash if it does not exits "w" Write to a file, create it if it does not exist and overwrite it if it already exists "a" Append to a file, start at the end, if a file does not exist create it "r+" Read and Write to a file, crash if it does not exist "w+" Write and Read from a file, create it if it does not exist and overwrite it if it already exists "a+" Append and Read from a file, start at end and create it if it does not exits ----------------- ----------------- Exercise -----------------