Thursday, June 6, 2013

Lesson 6 Dictionaries

Lesson 6 Dictionaries was very good for today.
I am very happy to have learned Dictionaries in Python Programing at this great institute/organization ILabLiberia.
Prof Zane was good to us in this class.
Below are some practice of this lesson.
"""
Learning Python
Lesson 6 : List & Dictionaries
Dictionaries

Simple programming exercises from Codecademy
"""


# Exercise 0 : This Next Part is Key
"""
    Print the value stored under the 'Sloth' key
    Print the value stored under the 'Burmese Python' key.
"""
residents = {'Puffin' : 104, 'Sloth' : 105, 'Burmese Python' : 106}


# Print the value stored under the 'Sloth' key
print residents ["Sloth"]



# Print the value stored under the 'Burmese Python' key
print residents ["Burmese Python"]






# Exercise 1 : New Entries
"""
    Add at least three key-value pairs to the menu variable,
    with the dish name (as a "string") for the key and the
    price (a float or integer) as the value. Here's an example:

    menu['Rice'] = 2.50
"""

menu = {}
menu['Chicken'] = 14.50


# Add three dish-price pairs to menu!
menu['Fish'] = 10.50
menu['Pizza'] = 17.50
menu['Rosted Meat'] = 10.99
print menu






# Now print the menu to see the new items





# Exercise 2 : Changing Your Mind
"""
    Delete the 'Sloth' and 'Bengal Tiger'
    items from zoo_animals using del.

    Set the value associated with 'Rockhopper Penguin'
    to anything other than 'Arctic Exhibit'.
"""

# key - animal_name : value - location
zoo_animals = { 'Unicorn' : 'Cotton Candy House',
'Sloth' : 'Rainforest Exhibit',
'Bengal Tiger' : 'Jungle House',
'Atlantic Puffin' : 'Arctic Exhibit',
'Rockhopper Penguin' : 'Arctic Exhibit'}

del(zoo_animals['Unicorn'])

print zoo_animals



# Delete the 'Sloth' and 'Bengal Tiger' items from zoo_animals using del.
del (zoo_animals ['Sloth'])

del (zoo_animals ['Bengal Tiger'])

print zoo_animals



# Set the value associated with 'Rockhopper Penguin' to something else.
zoo_animals ['Rockhopper Penguin'] = 'Lion Dane'


# Now print this zoo_animals dictionary
print zoo_animals

Answers below
Python 2.7.3 (default, Aug  1 2012, 05:16:07)
[GCC 4.6.3] on linux2
Type "copyright", "credits" or "license()" for more information.
==== No Subprocess ====
>>>
105
106
>>>
105
106
>>>
105
106
{'Chicken': 14.5, 'Rosted Meat': 10.99, 'Fish': 10.5, 'Pizza': 17.5}
>>>
105
106
{'Chicken': 14.5, 'Rosted Meat': 10.99, 'Fish': 10.5, 'Pizza': 17.5}
>>>
105
106
{'Chicken': 14.5, 'Rosted Meat': 10.99, 'Fish': 10.5, 'Pizza': 17.5}
Traceback (most recent call last):
  File "/home/ilab-1/Downloads/Lesson6-Dictionaries.py", line 86, in <module>
    print zoo_animal

106
{'Chicken': 14.5, 'Rosted Meat': 10.99, 'Fish': 10.5, 'Pizza': 17.5}
{'Atlantic Puffin': 'Arctic Exhibit', 'Rockhopper Penguin': 'Arctic Exhibit'}
>>>
105
106
{'Chicken': 14.5, 'Rosted Meat': 10.99, 'Fish': 10.5, 'Pizza': 17.5}
{'Bengal Tiger': 'Jungle House', 'Sloth': 'Rainforest Exhibit', 'Atlantic Puffin': 'Arctic Exhibit', 'Rockhopper Penguin': 'Arctic Exhibit'}
{'Atlantic Puffin': 'Arctic Exhibit', 'Rockhopper Penguin': 'Arctic Exhibit'}
>>>
105
106
{'Chicken': 14.5, 'Rosted Meat': 10.99, 'Fish': 10.5, 'Pizza': 17.5}
{'Atlantic Puffin': 'Arctic Exhibit', 'Rockhopper Penguin': 'Lion Dane'}
>>>
105
106
{'Chicken': 14.5, 'Rosted Meat': 10.99, 'Fish': 10.5, 'Pizza': 17.5}
{'Bengal Tiger': 'Jungle House', 'Sloth': 'Rainforest Exhibit', 'Atlantic Puffin': 'Arctic Exhibit', 'Rockhopper Penguin': 'Arctic Exhibit'}
{'Atlantic Puffin': 'Arctic Exhibit', 'Rockhopper Penguin': 'Arctic Exhibit'}
{'Atlantic Puffin': 'Arctic Exhibit', 'Rockhopper Penguin': 'Lion Dane'}
>>>

"""
Learning Python
Lesson 6 : List & Dictionaries

Simple programming exercises from Codecademy
"""


# Exercise 0 : BeFOR We Begin
"""
    Use a for loop to print out all of the elements in the list names.
"""

names = ["Adam","Alex","Mariah","Martine","Columbus"]
for word in names:
    print word





   
# Exercise 1 : This is KEY!
"""
    Use a for loop to go through the webster dictionary
    and print out all of the definitions.
"""

webster = {
    "Aardvark" : "A star of a popular children's cartoon show.",
    "Baa" : "The sound a goat makes.",
    "Carpet": "Goes on the floor.",
    "Dab": "A small amount."
}
for words in webster:
    print webster[words]








# Exercise 2 : Control Flow and Looping
"""
    Loop through list a and only print out the even numbers.
"""
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
for numbers in a:

    if numbers % 2 == 0:
        print numbers
       


Answers below

>>>
Adam
Alex
Mariah
Martine
Columbus
>>>
Adam
Alex
Mariah
Martine
Columbus
A star of a popular children's cartoon show.
Goes on the floor.
A small amount.
The sound a goat makes.
>>>
Adam
Alex
Mariah
Martine
Columbus
A star of a popular children's cartoon show.
Goes on the floor.
A small amount.
The sound a goat makes.
0
1
2
3
4
5
6
7
8
9
10
11
12
13
>>>
Adam
Alex
Mariah
Martine
Columbus
A star of a popular children's cartoon show.
Goes on the floor.
A small amount.
The sound a goat makes.
0
2
4
6
8
10
12
>>>

No comments:

Post a Comment