Thursday, June 13, 2013

Lesson 6 : List & Dictionaries "Grocery Store Project"

It was very good again for today's lesson.


"""
Learning Python
Lesson 6 : List & Dictionaries
Grocery Store Project

Simple programming exercises from Codecademy
"""


# Exercise 0 : Shopping at the Market
"""
    First, make a list (not a dictionary!) with the name groceries.
    Insert a "banana", "orange", and "apple".
"""
groceries = {"banana", "orange", "apple", "egg", "bread"}



# Exercise 1 : Making a Purchase
"""
    Write a function compute_bill that takes an argument food as input
    and computes your bill by looping through your food list and adding
    the costs of each item in the list.

    If an item on your food list is not in stock, don't worry about it for now.
"""

stock = {
    "banana": 6,
    "apple": 2,
    "orange": 32,
    "pear": 15,
    "egg": 10,
    "bread": 15
}
   
prices = {
    "banana": 4,
    "apple": 2,
    "orange": 1.5,
    "pear": 3,
    "bread": 15,
    "egg": 10
}

# Write your code below!
def compute_bill(food):
    total = 0
    for item in food:
        if stock[item] > 0:
          total = total + prices[item]
          stock[item] = stock[item] - 1
    return total

print compute_bill(groceries)


Python 2.7.3 (default, Apr 20 2012, 22:44:07)
[GCC 4.6.3] on linux2
Type "copyright", "credits" or "license()" for more information.
==== No Subprocess ====
>>>
set(['orange', 'apple', 'banana'])

5.5
>>>
30.5
>>>
32.5
>>>

Tuesday, June 11, 2013

Grocery Store Project

As you go deep in programing, it become very hard or harder.
This project, going through or learning it was very difficult for me but with the help of your able Instructor "Zane Cochran", I was able to learned it.
These are few codes below.
"""
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 allnumbers in a:
    if allnumbers %2 == 0:
       print allnumbers
   
# 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"]


def fizz_count(x):
    count = 0
    for item in x:
        if item == "fizz":
            count = count+1
           
    return count
              
print fizz_count(sample)



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 ====
>>>
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
5
>>>
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

"""
Learning Python
Lesson 6 : List & Dictionaries
Grocery Store Project

Simple programming exercises from Codecademy
"""


# Exercise 0 : Your Own Store!
"""
    Create a dictionary called prices
    Give it the following key/value pairs:

    "banana": 4
    "apple": 2
    "orange": 1.5
    "pear": 3
"""
prices = {"banana":4, "apple":2, "orange":1.5, "pear":3}


# Exercise 1 : Investing in Stock
"""
    Create a dictionary called stock
    Give it the following key/value pairs:

    "banana": 6
    "apple": 0
    "orange": 32
    "pear": 15
"""
stock = {"banana":6, "apple":0, "orange":32, "pear":15}




# Exercise 2 : Keeping Track of the Produce 
"""
    Use a for loop to print out information about every item in your store

    Print the answer in the following format:

    item
    "price: x"
    "stock: x"

    So when you print out the data for apples, print out:

    apple
    price: 2
    stock: 0

    Each of these values should be in a different print statement.
    Remember to convert numbers to strings before trying to combine them.
"""
for item in stock:
    print item
    print "price: %s" % prices[item]
    print "stock: %s" % stock [item]
   

# Exercise 3 : Something of Value  
"""
    Use a for loop to determine how much money you would make
    if you sold all the food you had in stock.

    Print out the result.
"""
total=0
for item in stock:
    sales = prices[item]* stock[item]
    total = total + sales
   
print total

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
fizz
buzz
fizz
buzz
fizz
fizz
fizz
None
>>>
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
5
>>>
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
1
2
3
4
5
5
>>>
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
fizz
fizz
fizz
fizz
fizz
5
>>>
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
5
>>>
>>>
orange
pear
banana
apple

orange
1.5
32
pear
3
15
banana
4
6
apple
2
0
>>>
orange
price: 1.5
stock: 32
pear
price: 3
stock: 15
banana
price: 4
stock: 6
apple
price: 2
stock: 0

>>>
orange
price: 1.5
stock: 32
pear
price: 3
stock: 15
banana
price: 4
stock: 6
apple
price: 2
stock: 0
117.0
>

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
>>>

Tuesday, June 4, 2013

List lesson

During my fist day in my Intermediate Python program class, I learned about List, the various kinds of list.


 """
Learning Python
Lesson 6 : Lists & Dictionaries
List Functions

Simple programming exercises from Codecademy
"""


# Exercise 0 : Appending & List Length
"""
    Append 3 more items to the suitcase list.
    Then, set list_length equal to the length of suitcase.
"""

suitcase = []
suitcase.append("sunglasses")

# Append 3 more items to the suitcase
suitcase.append("hat")
suitcase.append("shorts")
suitcase.append("shoes")
suitcase.append("beachwear")

# Create a variable called list_length and assign it to the length of suitcase
list_length = len(suitcase)
print "There are %d items in the suitcase." % list_length
print suitcase




# Exercise 1 : List Slicing
"""
    Use list slicing to slice suitcase into 3 parts.
"""

suitcase = ["sunglasses", "hat", "passport", "laptop", "suit", "shoes"]

# Create a variable called first and give it the first 2 items in the suitcase
first = suitcase[0:2]

# Create a variable called middle and give it the next 2 items in the suitcase
middle = suitcase[2:4]

# Create a variable called last and give it the last 2 items in the suitcase
last = suitcase[4:]

# Print your variables: first, middle and last to check your code
print first
print middle
print last





# Exercise 2 : Slicing Lists and Strings
"""
    Assign each variable a slice of animals that
    spells out that variable's name.
"""
animals = "catdogfrog"

# Create a variable called cat and slice the list to get the first three characters
cat= animals[0:3]


# Create a variable called dog and slice the list to get the next three characters
dog= animals[3:6]


# Create a variable called frog and slice the list to get the last four characters
frog= animals[6:]


# Print your variables: cat, dog and frog to check your code
print cat
print dog
print frog



# Exercise 3 : Maintaining Order
"""
    Create a variable called duck_index.
    Use the index() function to assign duck_index to the index of "duck".
    Then insert the string "cobra" at that index.  
"""

animals = ["aardvark", "badger", "duck", "emu", "fox"]


# Assign duck_index to the index of "duck". Use the index() function
duck_index= animals.index("duck")


# Now insert "cobra" into the list of animals at the duck_index
animals.insert(duck_index, "cobra")


# Print the list of animals and see what happens
print animals




# Exercise 4 : For One and All
"""
    Write a statement in the indented part of the for loop
    that prints a number equal to 2 * number for every list item.
"""

my_list = [1,9,3,8,5,7]

for number in my_list:
    # Your code here
    print number * 2




# Exercise 5 : For One and All
"""
    Write a for loop that appends values to square_list
    with items that are the square (x ** 2) of
    each item in start_list. Then sort square_list!
"""

start_list = [5, 3, 1, 2, 4]
square_list = []

# Write a for loop that goes through each number in the start_list
# Append the square of each number to square_list
for number in start_list:
    square_list.append(number ** 2)
    print "Adding the square of %d" % number
    print square_list

# Sort square_list
square_list.sort()


# Print square_list
print square_list





"""
# ************************************************************
# TESTS : DO NOT MODIFY THESE
# ************************************************************
print("")
if list_length == 4 : print("Exercise 0 : Test Passed!")
if first == suitcase[0:2] and middle == suitcase[2:4] and last == suitcase[4:6]: print("Exercise 1 : Test Passed!")
if cat == "cat" and dog == "dog" and frog == "frog" : print("Exercise 2 : Test Passed!")
if animals[duck_index] == "cobra" : print("Exercise 3 : Test Passed!")
if square_list == [1, 4, 9, 16, 25]: print("Exercise 5 : Test Passed!")
"""

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 ====
>>>
Traceback (most recent call last):
  File "/tmp/guest-A6n3zJ/Downloads/Lesson6-ListFunctions.py", line 27, in <module>
    print "There are %d items in the suitcase." % list_length
NameError: name 'list_length' is not defined
>>>
There are 5 items in the suitcase.
['sunglasses', 'hat', 'shorts', 'shoes', 'beachwear']
['sunglasses', 'hat']
['passport', 'laptop']
['suit', 'shoes']
>>>
There are 5 items in the suitcase.
['sunglasses', 'hat', 'shorts', 'shoes', 'beachwear']
['sunglasses', 'hat']
['passport', 'laptop']
['suit', 'shoes']
cat
dog
frog
>>>
There are 5 items in the suitcase.
['sunglasses', 'hat', 'shorts', 'shoes', 'beachwear']
['sunglasses', 'hat']
['passport', 'laptop']
['suit', 'shoes']
cat
dog
frog
['aardvark', 'badger', 'cobra', 'duck', 'emu', 'fox']
>>>
There are 5 items in the suitcase.
['sunglasses', 'hat', 'shorts', 'shoes', 'beachwear']
['sunglasses', 'hat']
['passport', 'laptop']
['suit', 'shoes']
cat
dog
frog
['aardvark', 'badger', 'cobra', 'duck', 'emu', 'fox']
2
18
6
16
10
14
>>>
There are 5 items in the suitcase.
['sunglasses', 'hat', 'shorts', 'shoes', 'beachwear']
['sunglasses', 'hat']
['passport', 'laptop']
['suit', 'shoes']
cat
dog
frog
['aardvark', 'badger', 'cobra', 'duck', 'emu', 'fox']
2
18
6
16
10
14
>>>
There are 5 items in the suitcase.
['sunglasses', 'hat', 'shorts', 'shoes', 'beachwear']
['sunglasses', 'hat']
['passport', 'laptop']
['suit', 'shoes']
cat
dog
frog
['aardvark', 'badger', 'cobra', 'duck', 'emu', 'fox']
2
18
6
16
10
14
[1, 4, 9, 16, 25]
>>>
There are 5 items in the suitcase.
['sunglasses', 'hat', 'shorts', 'shoes', 'beachwear']
['sunglasses', 'hat']
['passport', 'laptop']
['suit', 'shoes']
cat
dog
frog
['aardvark', 'badger', 'cobra', 'duck', 'emu', 'fox']
2
18
6
16
10
14
Adding the square of 5
[25]
Adding the square of 3
[25, 9]
Adding the square of 1
[25, 9, 1]
Adding the square of 2
[25, 9, 1, 4]
Adding the square of 4
[25, 9, 1, 4, 16]
[1, 4, 9, 16, 25]
>>>