Saturday, September 11, 2010

Order of Operations and Naming Conventions

Order of Operations
Yesterday we went over operators and operands. Today I'd like to go over Order of Operations. When you were going through elementary or middle school math you may have learned a similar way to remember it and mathematical order of operations is the same here. I learned it as, Please Excuse My Dear Aunt Sally, or Parenthesis, Exponents, Multiplication/Division (both have the same precedence), Addition/Subtraction (so do they). You can also remember, as offered by Thinking Like a Computer Scientist, the acronym PEMDAS. I like mnemonics so the former works better for me. I made it a point to list them in order in the last post.

Here are a few examples. Try to figure out what the results should be. You can also test it (and me) by putting the operation into the Python Shell to see for yourself what it comes out to.

8*1**4 = ?
3-6/3 = ?
(8*2)**4 = ?
12*2/4+4-2 = ?

The first example comes out to... 8! Because 1 exponent 4 is still one, times 8 is still 8. The second example is 1, because we first divide 6 by 3 then subtract the result from 3. Making sense? This next one is the same as the first one but notice the parenthesis. This will now do the parenthesis first, giving us 16 and taking 16 to the exponent 4 which gives us 65536. For the last one we have multiple operations with the same precedence. How will Python evaluate it? The same as you would read it, left to right. It would take 12*2 to get 24, 24/4 to get 6, 6+4 to get 10, and 10-2 to get 8.

We've also gone over variables, but not the naming conventions of them. There are some rules, and there are some style suggestions. Some rules are:

  1. Variable names cannot begin with a number.
  2. All numbers are legal (Just not to begin the variable name)
  3. Variable names cannot contain the character $.
  4. Variable names cannot be a keyword (see list below)
  5. Multiple variabls cannot share the same name.
  6. Variable names are case sensitive. (i.e. name is separate from Name)
  7. Variable names cannot include a space. 
  8. Variable name must be on the left side of the assignment operator.


False      class      finally    is         return
None       continue   for        lambda     try
True       def        from       nonlocal   while
and        del        global     not        with
as         elif       if         or         yield
assert     else       import     pass
break      except     in         raise 

As for style, there are a few general suggestions. One is to use meaningful names. This helps you when you have to go back to your code later on and interpret it, and it helps others know what is going on. This goes along with leaving comments in your code. Another suggestion is to use _ to break up words in a variable name, such as coffee_made = no. You can also use what is called camel case. Camel case is what I prefer and is when you capitalize the first letter in each word excluding the first and include no spaces, such as coffeeMade = no. According to Wiki, if you capitalize the beginning as well it is known as Pascal case.
Sorry, I like the camel.

No comments:

Post a Comment