Difference between revisions of "CSC111 Lab 12 2010"
(→Playing with Dictionaries) |
(→Problem #2) |
||
Line 88: | Line 88: | ||
==Problem #2== | ==Problem #2== | ||
+ | |||
+ | ::<font color="lightblue">'''Smith College Museum of Art (SCMA) jest odbiorcą jednym z najbardziej głośnych przez wybitny przywódca ashcan School of American malarstwo realistyczne'''.</font> | ||
+ | |||
+ | What language is the sentence above written in? | ||
+ | |||
+ | To find out, let's write a python program to do so! | ||
+ | |||
+ | ===Introduction=== | ||
+ | |||
+ | First, go over the Wikipedia page on '''[http://en.wikipedia.org/wiki/Letter_frequency letter frequency]'''. | ||
+ | |||
+ | |||
http://tnlessone.wordpress.com/2007/05/13/how-to-detect-which-language-a-text-is-written-in-or-when-science-meets-human/ | http://tnlessone.wordpress.com/2007/05/13/how-to-detect-which-language-a-text-is-written-in-or-when-science-meets-human/ |
Revision as of 13:10, 21 April 2010
This lab deals with dictionaries. |
Dictionaries
Dictionaries are data structures in Python that have the following properties:
- they key track of pairs of elements. The first one is the key, the second the value
- all the keys are unique
- dictionaries allow fast searching, insertion and retrieval of information.
Playing with Dictionaries
- Use Python in interactive mode and enter the different Python statements shown below.
- Observe the output, and make sense of how dictionaries work in Python
>>>
>>> # create an empty dictionary
>>> D = {}
>>> D
>>> # create a dictionary with a few key:value pairs
>>> D = { "apple":30, "pear":10, "banana":5 }
>>> D
>>> # inspect some of the contents
>>> D[ 'pear' ]
>>> D[ 'apple' ]
>>> # we are getting 25 more bananas...
>>> D[ 'banana' ] = D[ 'banana' ] + 25
>>> D[ 'banana' ]
>>> D
>>> # we're getting a new shipment of pineapples... 100 of them
...
>>> D[ 'pineapple' ] = 100
>>> D
>>> # we want the name of the fruits (keys) we carry...
>>> D.keys()
>>> for fruit in D.keys():
... print fruit
...
>>>
>>> # we want to print the full inventory
...
>>> for key in D.keys():
... print D[ key ], "units of", key
...
- Now that you better understand how dictionaries work, try to figure out how to answer the following question in Python (use the interactive mode:
- Question 1
- How many bananas do we have?
- Question 2
- We sell half of the bananas. Remove half of the bananas from your inventory.
- Question 3
- Print the fruits for which we have more than 50 units.
Problem #2
- Smith College Museum of Art (SCMA) jest odbiorcą jednym z najbardziej głośnych przez wybitny przywódca ashcan School of American malarstwo realistyczne.
What language is the sentence above written in?
To find out, let's write a python program to do so!
Introduction
First, go over the Wikipedia page on letter frequency.