Showing posts with label variables. Show all posts
Showing posts with label variables. 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.

Thursday, September 9, 2010

Declaring Variables w/ Python

Hello again! It's Thursday, which is one of my favorite days, if only because it means Friday is almost here! I'd like to end my Thursday with a lesson on variables. Before we get started however I thought it would be a good learning experience, for myself and perhaps others, to do my logic work in psedocode, and actual code (and perhaps a flowchart). For the "real code" I'm going to use Python.

There are a few really good links for help on Python programming. One is from the Python website itself here. As well as from the Introduction to Computer Science and Programming course on MIT's OpenCourseWare (OCW) website. The MIT OCW website has many classes and programs you can go through free of charge and look very interesting. Harvard also has a similar setup here and it looks like they use C whereas MIT is using Python in instruction.  

If you want to follow along with me you should go to the Python website and download version 2.7 If you prefer to use 3.0 that's fine but 2.7 is backward and forward compatible whereas 3.0 has gotten rid of some things that I may be using since I'm following the MIT OCW course, recorded in 2008.

Once you've installed Python, bring up the Python Shell also called IDLE (Python GUI) which should look like this:
IDLE (Python GUI) / Python Shell
From here you can input all the commands which I will preface with >>> to look the same as the Python Shell.

Now, onto the lesson: Variables are memory locations, the contents of which may vary throughout the program. For example:

          >>>numberOne = 1 #Hit Return

*Note - # is Python for comment. Meaning, typing it would not hurt, but does nothing for you. I am only letting you know here to hit return to enter 1 as the value for numberOne.

This sets a memory location, nicknamed "numberOne" to the value of 1. To write this in pseudocode you can write it the same "numberOne = 1" since this is a simple statement, or to be more clear:

          set numberOne = 1

Remember I said variables can change. We can now type in the Shell:

          >>>numberOne = numberOne + 1

This may look weird, but it should be simple enough to see what is happening. We are taking numberOne and setting it equal to numberOne + 1, or just adding one to it. If you type:

          >>>print numberOne

You should now get the result: 2.

The last thing I want to mention briefly is the data type of this number:

2

The number 2 is an integer or int, and has no decimal value. In Python, and most other programming languages, this is important. The type is decided when you initialized the variable. To make this make sense try typing 7/2 into the Python Shell and hit return. Notice the answer is not 3.5 but 3! An integer has no decimal value, and your attempt at dividing, which would create a decimal is ignored and it throws out the remainder.

If you are writing a program that requires the decimal values, you can use what is called a floating point or float. To make it a float, just type 7.0 (the .0 part making it float) and divide that by 2. What do you see? 3.5!

Be careful when writing your programs that you keep track of this. There are ways in many programming languages to change the type, but doing so unnecessarily can cause leaks and bugs in your program.

Lastly, a neat trick to see the remainder of the integer division is by using the % sign. Using % divides one side by the other and gives the remainder.

          >>>7%2
          1

Tomorrow I'll try and post something a little more... interesting to program. Until then enjoy the tutorials and let me know what you think!