Thursday, June 6, 2013
My first day in the Intermediate Python Programing
"""
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['Dog'] = 5.90
menu['Snake'] = 67
menu['cake'] = 50.9
# Now print the menu to see the new items
print menu
# 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']
# Delete the 'Sloth' and 'Bengal Tiger' items from zoo_animals using del.
del (zoo_animals['Sloth'])
del (soo_animals['Bengal Tiger'])
print(zoo_animals)
# Set the value associated with 'Rockhopper Penguin' to something else.
zoo_animals['Rockhopper Penguin'] = 'Bengal Tiger'
# Now print this zoo_animals dictionary
print zoo_animals
# Exercise 3 : It's Dangerous to Go Alone! Take This
"""
Final challenge! Add code that modifies the dictionary
inventory in the following ways:
Add a key to inventory called 'pocket'
Set the value of 'pocket' to be a list consisting of the
strings 'seashell', 'strange berry', and 'lint'
.sort() the items in the list stored under the 'backpack' key
Remove 'dagger' from the list of items stored under the 'backpack' key
Add 50 to the number stored under the 'gold' key
"""
inventory = {'gold' : 500,
'pouch' : ['flint', 'twine', 'gemstone'], # Assigned a new list to 'pouch' key
'backpack' : ['xylophone','dagger', 'bedroll','bread loaf']}
# Adding a key 'burlap bag' and assigning a list to it
inventory['burlap bag'] = ['apple', 'small ruby', 'three-toed sloth']
# Sorting the list found under the key 'pouch'
inventory['pouch'].sort()
# Your code here!
"""
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 word in webster:
print webster [word]
# 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 number in a:
if numbers % 2 == 0:
print numbers
# Exercise 3 : Lists & Functions
"""
Write a function called fizz_count that takes a list x
as input and returns the count of the string "fizz" in
that lise. For example,
fizz_count(["fizz", "buzz", "fizz"]) should return 2.
Then give your fizz_count the argument sample and see what it returns:
print fizz_count(sample)
"""
sample = ["fizz", "buzz", "fizz", "buzz", "fizz", "fizz", "fizz"]
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
>>>
105
106
{'cake': 50.9, 'Chicken': 14.5, 'Dog': 5.9, 'Snake': 67}
>>>
105
106
{'cake': 50.9, 'Chicken': 14.5, 'Dog': 5.9, 'Snake': 67}
>>>
105
106
{'cake': 50.9, 'Chicken': 14.5, 'Dog': 5.9, 'Snake': 67}
>>>
105
106
{'cake': 50.9, 'Chicken': 14.5, 'Dog': 5.9, 'Snake': 67}
Traceback (most recent call last):
File "/home/ilab-5/Downloads/Lesson6-Dictionaries.py", line 77, in <module>
del (soo_animals['Bengal Tiger'])
NameError: name 'soo_animals' is not defined
>>>
105
106
{'cake': 50.9, 'Chicken': 14.5, 'Dog': 5.9, 'Snake': 67}
Traceback (most recent call last):
File "/home/ilab-5/Downloads/Lesson6-Dictionaries.py", line 77, in <module>
del (soo_animals['Bengal Tiger'])
NameError: name 'soo_animals' is not defined
>>>
105
106
{'cake': 50.9, 'Chicken': 14.5, 'Dog': 5.9, 'Snake': 67}
Traceback (most recent call last):
File "/home/ilab-5/Downloads/Lesson6-Dictionaries.py", line 77, in <module>
del (soo_animals['Bengal Tiger'])
NameError: name 'soo_animals' is not defined
>>>
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]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
[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
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.
Traceback (most recent call last):
File "/home/ilab-5/Downloads/Lesson6-ForLoops.py", line 52, in <module>
if numbers % 2 == 0:
NameError: name 'numbers' is not defined
>>>
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.
Traceback (most recent call last):
File "/home/ilab-5/Downloads/Lesson6-ForLoops.py", line 52, in <module>
if numbers % 2 == 0:
NameError: name 'numbers' is not defined
>>>
http://ilab.zanecochran.com/?page_id=133
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment