######## string ######### print('--- review string ---') s = 'a,b,c,d,e' print(s[2]) print(s.split(','), list(s)) print(s.split()) print(s.split(' ')) print(s.split('@')) print(s[1:3]) print(s[:3]) print(s[3:]) print(s[2], s[-1], s[len(s)-1]) print("s[2:2] shouldn't get anything", s[2:2]) ######## list ######### print('--- review list ---') x = [3] print(x) x[0] = 9 print(x) print(x * 2) # repeat 2 times x += ['12', '34', '5'] x[1] = '9' print(x) print(x[2], x[-1], x[len(x)-1]) # access element in list as lst[index] print(x[1:2]) # get a copy of a slice of x from index 1 up to index 2 (not include index 2) print("x[2:2] shouldn't get anything", x[2:2]) print(x) # notice, x does not change print(x.pop()) # pop() returns the value of the last item and remove it from the list print('after pop()', x) x.append(555) # add item to the end of the list print('after append(555)', x) print(x.append(666)) # append() returns None and thus will print None print('after append(666)', x) # notice mix type elements in list print('--- list can be iterated, sliced, and mutated ---\n') # remove() removes an item by value from the list but not return anything # therefore, if we call print(x.remove(555), it displays None print(x.remove(555)) # since remove(value) looks for the given value and remove it from the list, # try removing a value that's not in a list and see what exception occurs # (note: remove by value of the item, *not* by index print(x) ############## print('--- nested lists ---') lst = ['A', [0, 1], ['B', [2, 3], ['C', [4, 5]]]] # what do the following code produce? print(lst[1]) print(lst[1][0]) print(lst[1][1]) print(lst[2]) print(lst[2][0]) print(lst[2][1]) print(lst[2][2]) print(lst[2][2][0]) print(lst[2][2][1]) print(lst[2][2][1][0]) print(lst[2][2][1][1]) ######## dict ######### print('--- review dict ---') x = {1:2, 2:3, 3:4} x[1] = 9 print('x =', x) # print('can\'t slice a dict', x[2:3]) x[1] = 33 # re-assign value associated with key=1 print('after reassigment, x =', x) print('x[2] =', x[2]) # access element in dict whose key=2 print('x.keys() =', x.keys()) # access all keys in dict # keys() return a special dict_keys type (which looks similar to list but attached with dict_keys keyword) print('x.values() =', x.values()) # access all values in dict # values() return a special dict_values type (which looks similar to list but attached with dict_values keyword) print('x.items() =', x.items()) # access all key-value pairs in dict # items() return a special dict_items type (which looks similar to list of pairs/tuples but attached with dict_items keyword) print(4 in x, 4 in x.keys(), 4 in x.items(), 4 in x.values()) # Notice that when working with dict, python looks at keys in dict # therefore, the first 3 checking for 4 will result in False. # The last checking explicitly says look in values of dict. print('--- dict can be iterated, and mutated ---\n') print('--- don\'t forget to consider casting ---') x = 12345 # print(list(x)) # what's wrong? x = 12345 print(list(str(x))) # x = '12345' # print(list(x)) x = '12345' # print(dict(x)) # what's wrong?