Wednesday, December 7, 2016

CST 205 Week 6

CST 205 Week 6

This week I learned about dictionaries in Python. This seems to be a very usefull tool. A dictionary represents a mapping of keys to values. An example would be for mapping english to spanish words such as: 
>>> eng2sp = dict()
>>> eng2sp
{}
And then to add an item we would just use:
>>> eng2sp['one'] = 'uno'
To create multiple key-values you would do something like.
>>> eng2sp = {'one': 'uno', 'two': 'dos', 'three': 'tres'}
The order of key value pairs is random.
o see whether something appears as a value in a dictionary, you can use the method values, which returns a collection of values, and then use the in operator:
>>> vals = eng2sp.values()
>>> 'uno' in vals
True
I learned about using try and except in python to catch exceptions and allow the user to fix
the problem or end it gracefully. These are used similarly to if else statements.

Using the pickle module in Python, allows you to use other data types other than Strings and bytes in
databases. It translates almost any type of object into a string suitable for storage in a 
database, and then translates strings back into objects. 


No comments:

Post a Comment