Showing posts with label operator. Show all posts
Showing posts with label operator. Show all posts

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.

Friday, September 10, 2010

Operators, Operands, and Hello, World!

Good morning! I couldn't sleep so I'm going through my Python tutorial before work. This morning we'll talk about manipulating variables. We used operators and operands yesterday, but I did not explain them. Operators are symbols, like + and - that manipulate data. The data being manipulated is the operand. So in the statement:

>>>x*3

We are multiplying the variable x (The value of a variable is substituted prior to evaluation by the computer) by 3. 3 and x are operands and * is the operator.  You can remember this easily by remember the operator of a piece of machinery is the person doing the work. In this case the * is doing the work on the two operands. The following are legal operators:

** = exponent/power
* = Multiply
 / = Divide
+ = Plus
 - = Minus

Another operator we have used already is the assignment operator or =. Using = assigns the value on the right to the variable on the left. It is similar but different from the equals sign which we will go over later. First let's remember the different types we have gone over (int, float) and let's add another one 'str.' Str, or string, types are strings of characters such as "Hello, World." The "Hello, World" program is a popular first program and so we can use it today to create our first saved program while learning about strings.

First open IDLE (Python GUI). Select File>New Window. A new window with a text editor pops up and here we can write our program.

##This is a Hello, World! program.
##It prints "Hello, World!" on the screen.
print "Hello, World!"

*Note - Remember the # is the comment designator and is not necessary, but a tip for later when writing longer more complex programs is to always include good notes in your program. You can include these comments for your own benefit, put comments of your own or simply start with print. 

Select File>Save>> HelloWorld.py (or any name followed by the .py extension) >Save. Now, let's run our program! Do this by selecting Run>Run Module, or pressing F5. If you have made changes it will let you know you need to save it to run it. When you click Run Module a Python Shell will pop up if you didn't have one up already and you should see this:
Hello, World!
 
You now have a program you can run over and over again as long as you want! It doesn't do a whole lot, but it's all yours. Hope everyone has a great Friday!