Wednesday, October 22, 2008

Chapter 5

At the beginning of this Chapter, we learn how to make our code into a file. I will not touch up on this because it is just like saving any other type of file. The main focus of the chapter is along the lines of try and catch. This is the reason why I had trouble because I have not dealt with this in Java yet so it was all new to me. After speaking with Dr. Piercy, I started to understand this material. If you know how to use a try and catch, then this chapter will be easy for you. For those of you who don't, try and catch is a function you can use in Java. The try portion is along the lines of a method. You want the function to "try" and execute it. The Catch portion is the area where you right an exception to the action. An example of this is trying to hook up to a database. Your code could be perfect in the "try" area but you the file you are trying to reach isn't there. The catch functions catches that the file is up and you can have it return back to you as "Cannot connect to file." This is helpful because it saves you time looking at your code trying to find out what the problem is when really, your code is fine.
With this understanding we move onto Python. If you want to write out this try catch function this is how you can go abouts.

def in_fridge ();
" " " This is the comment area where you can describe your definition" " "
try:
count = fridge(wanted_food)
except KeyError:
count = 0
return count

The problem that I was running into was when entering this data, I kept getting errors. The example above is straight out of the book. What you must do is have your try and except line up with one another while the actions inside must be indented in. This is just a structure that Python abides by. I recommend you create your own example over and over to understand the structure and the set up.
With the definition above, you see that we put a parameter which is wanted_food. Say you wanted to find how many bananas were in the fridge. All you would have to type out is:

wanted_food = "bananas"
(You are assuming that you added values to fridge earlier. For instance, fridge = {"bananas" : 3, "oranges" : 4})

Thursday, October 16, 2008

Chapter 5 - Andrew

I'm with Joseph at the moment. I'm going to need a little more time reading and playing with Chapter 5 before I can post about it. I want to understand it a little better then I'll write about it.

I still need to post the schedule and about the difference between mutability and immutability. Also, I didn't know there was a meeting yesterday, I apologize for missing it.

Chapter 4 Conclusion

In the last post, I covered everything from Chapter 4 except for loops. There are two types of loops that we learn about in Chapter 4. These are “for…in” loops and “while” loops.

In a “while” loop, the operation will first check and see if it’s test condition is True. If it is, it will evaluate the indented statements. When it is done, it will go back and see if the test condition is still true. If it is, it will continue going through this process until the test condition returns False and then the program will move on.

A “for…in” loop is similar, but has fewer steps. In the first part, you give a name that you’ll use inside the indented code. In the second part, you give a sequence which takes each element and assigns the value of the element to the name you provided in the first part.

I don’t completely understand what the book is saying exactly and it just sort of throws in the pop method in the “while” loop without really saying what exactly it does.

Sometimes in writing these loops you run into an infinite loop that doesn’t stop. In order to stop this, you can use “break” at the end of the indented section.

Both “for…in” and “while” loops can have an else statement, as long as they’re not stopped by a “break”. This is used when the test condition is False and allows an alternative event to take place.

Another feature that is used a good bit with loops is “continue”. Instead of terminating the program like “break” does, “continue” just tells the loop that you want to skip the rest of the current repetition of the loop.

This concludes Chapter 4.

Wednesday, October 15, 2008

Chapter 5

I have run into a little problem with Chapter 5. Seeing I have been learning Java and Python at the same time, I have been fortunate enough to learn the same methods at the same time. This chapter is brand new to me and I am somewhat confused. Today's meeting will allow me to discuss the chapter more in depth to gain a better understanding and will be able to post sometime today. Sorry for this problem.

Thursday, October 9, 2008

Chapter 4 Continued

Since it won’t allow me to go back and edit any of Joseph’s posts this is a continuation of Joseph’s on Chapter 4.

It is possible to have an if statement within another. For example:

sandwich_ingredients = {“ham”:3, “cheese”:2}
fridge_contents = {“ham”:10, “milk”:5, “pepper”:1, “cheese”:3}
have_ingredients = [True]
if fridge_contents[“ham”] > sandwich_ingredients[“ham”]:
have_ingredients[0] = False
have_ingredients.append(“ham”)

if fridge_contents[“cheese”] > sandwich_ingredients[“cheese”]:
if have_ingredients[0] == True:
have_ingredients[0] = False
have_ingredients.append(“cheese”)

In order for the code to continue on to the second if, also known as a nested if, the first one must be true.


That’s all I have for now. I’ll come back later and add while and for loops as well as if-else statements.

Chapter 3 Continued

Since it won't let me go into Joseph's old posts, I am creating an entirely new one with related data.

This post goes into more detail with some of the things Joe touched on in his previous post for Chapter 3. In creating variables, there are a few certain words that you can’t use because Python has them saved for special functions. These words are: and, assert, break, class, continue, def, del, elif, else, except, exec, finally, for, from, global, if, import, in, is, lambda, not, or, pass, print, raise, return, try, while, or yield. You also can not start your variable with a number or a non-alphabetical character such as a dollar sign, a pound sign, an asterisk, etc. You can, however, use the underscore character.

Other information types, aside from numbers and strings, which are built into Python are tuples, lists, and dictionaries. Tuples are sequences of values that are individually accessible. Tuples are used when you want to assign values to match more than one format specifier in a string. They are created by using normal parenthesis.

A list is similar to a tuple. Lists are created using square brackets. Other than the brackets, the main difference between a list and a tuple is that a list can be changed after it has been created. To add one item to the end of a list, you use the append method. For example if you had a list named vehicles, you would add a jet ski to the list by coding the following:
vehicle.append(“jet ski”)

If you want to add more than one item to the end of a list, you must use the extend method. For example:
Vehicle.extend(“jet ski”, “moped”, “train”)

There is another way to grow a list. That is through appending a sequence. If you had a list that you wanted to combine with another list, you can just add it at the end. Say you took your vehicle list and wanted to add a list of brand names to it. It can be done as follows:

vehicle = []
vehicle.extend(brand_name)
vehicle
[‘jet ski’, ‘moped’, ‘train’, ‘Kawasaki’, ‘Sea Doo’]

A third type of class is dictionaries. They are similar to tuples and lists. The difference between dictionaries and tuples & lists is that data is stored by names that you choose rather than their numeric order. Ex:

movie_choices = {}
movie.choices[“action”] = “Terminator 2”
movie.choices[“comedy”] = “Superbad”
movie.choices[“drama”] = “The Count of Monte Cristo”

Another way to create a dictionary while assigning keys (the words within the square brackets) and values (what comes after the equals sign) is as follows:

movie.choices = {“action” : “Terminator 2” , “comedy” : “Superbad” , “drama” : “The Count of Monte Cristo”

To find out what the keys of a dictionary are, you have to type the following:

movie_choices.keys()
[‘action’, ‘comedy’, ‘drama’]

The same thing works for finding the values:

Movie_choices.values( )
[‘Terminator 2’ , ‘Superbad’ , ‘The Count of Monte Cristo’]

Wednesday, October 1, 2008

Chapter 4

Comparing Values is the theme for this chapter. Comparing values is simple, all you have to do is double equal signs to see if it is the same.
1==1
True
1==2
False

You can do the same with strings not just numbers. If you want to see if they are not equal, the symbol is  != . 
1 !=0
True

When you have strings, you can access all methods that a string normally has. The example in the book is good:
gourd = "Calabash"
gourd.lower()
  "calabash"
gourd.uppper()
  "CALABASH"
Lower makes it lower case while Upper makes it all uppercase. 

The word not provides the opposite of the truth value that follows it. The not operation applies to any test that will result in a true or false outcome. 

not 9 > 2
 False

Now we are onto IF statements. This if statement will allow you to get decisions made.

if 5 > 8:
   print "No, it is not"

if 5 <>
   print "Yes, it is"

You can place the if statements within tests. 

Chapter 3 Continued..

The type of bracket you use is very important. You have seen us use () when we are inserting data. When you print off the data inside the brackets like  monkey = ("banana", "smoothie", "tastes good"), you get everything inside of that bracket. In Python, you can reference the values inside.

print "I want to access the second value which is %s" %monkey[1]
I want to access the second value which is smoothie

Remember that the counter starts at 0 , so if you wanted to get the word banana the ending would say %monkey[0] .

If you forget how many values are inside the bracket, you can type in the len to find out how many is in there. An example is print "%d" % len(monkey) which will spit out the result 3

You actually access a tuple through another tuple. Don't try to picture it now, it is better to look at it in layers.

monkey = ("banana", "smoothie", "tastes good")
cheetah = (monkey, "fast")
print "%s" % cheetah [0][0]
    banana
print "%s" %cheetah[0][1]
    smoothie

One specific rule involving tuples is that if you have only one element, you must follow it will a comma.    monkey = ("banana",)


Chapter 3

In Chapter 3, we will be learning to use variables. We start off with assigning values to names. Python makes it is to do so. An example of creating variables is:

first_string = "Boston"
second_string = "New York"
first_number = 2
second_number = 3
print " The first variables are %s, %s, %d, %d" % (first_string, second_string, first_number, second_number)

'The first variables are Boston, New York, 2, 3

The name in which you use doesn't relate it to the data that you are inputting. You can name it whatever way you want, but we named it in the above example in correlation what the data was. I could have named "Boston" this: first_city or first_number . This would have still given the data.

A great example of how to label for you own needs is:

lightbulbs_in_closet = 10
lightbulbs_in_lamps = 12

You can also alter the named values. It is quite simple:

lightbulbs_in_closet = 10
lightbulbs_in_closet = lightbulbs_in_closet + 4
lightbulbs_in_closet
       14

The only thing that you cannot due with naming can be found at the bottom of pg. 29. This gives you a list of words that will not work because they are built-in words that are reserved for special use to prevent any confusion.