Tutorial A5¶
1 Make a list of data objects, each of a different type you know. Print the elements of the list and their type to the screen by index.
2 Create a dictionary that represents a shopping list and store it in a variable: Use three things you need to buy as keys and add how much you want to buy of them as values. Than add another item to the existing dictionary. Print the dictionary keys to the screen.
Advanced 1 Take the points (0, 0)
, (0, 1)
, (1, 0)
and (1, 1)
.
Store each point in a variable as a tuple. Make an empty list and append each point to the list.
Change the second point
(0, 1)
to(0, -1)
by overwriting the variable in which it is stored. What happens to this point in the list?
Change the third point
(1, 0)
to(-1, 0)
by indexing this point stored in the list. What happens to this point outside of the list?
Now store each of the initial points ((0, 0)
, (0, 1)
, (1, 0)
and (1, 1)
) in variables as lists and append them to a list.
Change the second point
[0, 1]
to[0, -1]
by overwriting the y-component of the variable in which it is stored. What happens to this point in the list?
Change the third point
[1, 0]
to[-1, 0]
by indexing this point stored in the list and overwriting the its x-component. What happens to this point outside of the list?
Advanced 2 Take the string "High Performance Computing"
and - convert this into a list.
Use the tuple
tuple("abcdefghijklmnopqrstuvwxyz ")
to map the letters in the string to their index in the alphabet. Tipp: Use the methodtuple.index
and the built-in functionmap
. Map takes a function as the first argument and a sequence (e.g. a string as the second. The function is applied to each element of the sequence. Convert the return value ofmap()
to a list. Usestring.casefold()
to map also upper case characters.
Create a dictionary from the map and the list that represents this mapping (
d = {"letter": mapped_index}
). Tipp: Usezip()
.
Advanced 3 Consider the following dictionary of random floating values associated with random string keys:
[1]:
dictionary = {
'afsgfabkpetv': 0.50996581835599997,
'ajpjggwdnbbv': 0.48394650530200001,
'bbsqfnzsaerh': 0.94967251027800004,
'bmkkubxijsgoi': 0.73021130082789998,
'bmmqvyintpoat': 0.47911570513349999,
'ccgasifxzngp': 0.32569065293400001,
'cebmdkidwwyt': 0.97307019405200001,
'cismykmadevcya': 0.20194133057088001,
'cjktphzhhbvrn': 0.27046062498669998,
'ckdgztgvksmid': 0.3741114083232,
'cwnukrruweshiuh': 0.53529824780703295,
'cwthembmixxmg': 0.089824943543700006,
'dasjwthqotarfgz': 0.48673599515272398,
'dgoxsygqjkkiv': 0.49586035923279997,
'dijwhwnubajohkf': 0.074657439369638007,
'dszizjhbozuuy': 0.19034104761039999,
'xcvhdthqotarfgk': 0.48673599515272398,
'ehuzgmflwhxcnmv': 0.027337878930186001,
}
Use the built-in properties of sets to find out whether any two values are the same. How could you check if a set already contains a dictionary value? Could two keys ever be the same?
Compute the sum of all values in the dictionary.
Make a set of unique letters found in all dictionary keys. Tipp: Use the string method
str.join()
.