

Prepara tus exámenes y mejora tus resultados gracias a la gran cantidad de recursos disponibles en Docsity
Gana puntos ayudando a otros estudiantes o consíguelos activando un Plan Premium
Esta página no es visible en la vista previa
¡No te pierdas las partes importantes!


Python dictionaries store information associating each data to a value (called key , in bold in the example). Here we will use text values as keys, but you can use other types too. dictio = {' name ':'Owen', ' phone ':324551532, ' age ':25} In order to access the values in a dictionary, you have to give the key in brackets and quotes (if it is a text variable): print(dictio['name']) Dictionaries are dynamic structures, therefore it is possible to add new elements after creating them. An alternate way of making a dictionary is as follows:
dictio = { } dictio['name'] = 'Owen' dictio['phone'] = 324551532 dictio['age'] = 25
dictio['name'] = input('Type your name:') ...
Often you will want to store several dictionaries in a single data structure. Lists are the usual solution to this. They are very similar to vectors but they can store any data type and it is easy to add new elements to them.
myList = [ ]
myList.append({'shape':'square' , 'color':'red' }) myList.append({'shape':'triangle', 'color':'yellow'}) myList.append({'shape':'circle' , 'color':'blue' }) Lists are traversed in the same way as vectors, with a for that accesses each of its elements (i.e. each dictionary in this case): Unidad 7 Computer Science Lab - EUETIB - UPC 1 Samir Kanaan Izquierdo - 2014
for piece in myList: print(piece['shape'], piece['color']) You can also traverse the list by indices, and perform searches on it with while.
Write a program that reads the data of two rice batches (variety, caliber, area) and stores them in two dictionaries. It should compare them and tell if they are equal or, if there are any differences, what are these differences (in variety, caliber or area).
Write a program that reads the coordinates of three points in space (x, y, z), each one on a dictionary. Then it should compute its geometrical center and it should store it in another dictionary.
Write a program that reads the appointments of a calendar during a week, with a dictionary for each appointment (day number, time of day, place, person). Show everything on screen.
With the calendar from the previous exercise, the program should ask the name of a person and print the appointments with that person.
Now the program should ask a day number and print how many appointments are scheduled for that day.
The program should ask a place name and print if there are any appointments in that place or not. Unidad 7 Computer Science Lab - EUETIB - UPC 2 Samir Kanaan Izquierdo - 2014