Order of Operations |
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:
- Variable names cannot begin with a number.
- All numbers are legal (Just not to begin the variable name)
- Variable names cannot contain the character $.
- Variable names cannot be a keyword (see list below)
- Multiple variabls cannot share the same name.
- Variable names are case sensitive. (i.e. name is separate from Name)
- Variable names cannot include a space.
- 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