But first, since you’ve no doubt been busy and the last part of this Python series went live in early December of last year, here is a view of the available operators:
Symbol | Type | What it Does |
+ | Mathematical | Addition |
– | Mathematical | Subtraction |
* | Mathematical | Multiplication |
/ | Mathematical | Division |
// | Mathematical | Truncating Division |
** | Mathematical | Powers |
% | Modulos | Returns the remainder from a division |
<< | Shift | Left Shift |
>> | Shift | Right Shift |
& | Logical | And |
| | Logical | Or |
^ | Logical | Bitwise XOR |
~ | Logical | Bitwise Negation |
< | Comparison | Less than |
> | Comparison | Greater than |
‘==’ | Comparison | Equal to |
!= | Comparison | Not Equal To |
>= | Comparison | Greater than or Equal To |
<= | Comparison | Less than or Equal To |
‘=’ | Assignment | Assigns a value |
+= | Assignment | Adds and assigns a value |
-= | Assignment | Subtracts and Assigns a value |
*= | Assignment | Multiplies and assigns a value |
/= | Assignment | Divides and assigns a value |
//= | Assignment | Truncate Divides and assigns a value |
**= | Assignment | Powers and assigns |
%= | Assignment | Modulus and assigns |
>> | Assignment | Shifts and assigns |
<< | Assignment | Shifts and assigns |
And | Boolean | |
Or | Boolean | |
Not | Boolean |
{mospagebreak title=The Prestigious Mathematical Operators}
Most of the mathematical operators you should be familiar with, at least if you graduated the third or fourth grade, which judging by that confused look on your face, perhaps you haven’t. That’s okay. I’ll use little words.
In the example below we will use each of math operators to demonstrate how they function:
#!/usr/local/bin/python
print "Here is an example of using addition: 2+2 =", 2+2
print "Here is an example of subtraction: 4-2 =", 4-2
print "Here is an example of multiplication: 2*4 =", 2*4
print "Here is an example of division: 8/4 =", 8/4
print "Here is an example of truncated division: 5//3 =", 5//3
print "Here is an example of using powers: 2**9 =", 2**9
print "Here is an example of using modulos: 5%3 =", 5%3
The result of this code is:
Here is an example of using addition: 2+2 = 4
Here is an example of subtraction: 4-2 = 2
Here is an example of multiplication: 2*4 = 8
Here is an example of division: 8/4 = 2
Here is an example of truncated division: 5//3 = 1
Here is an example of using powers: 2**9 = 512
Here is an example of using modulos: 5%3 = 2
A few things to note: presently, division works the same as truncated division, though this will change in the future. Truncated division means that if you divide two integers, and the result is say, 1.25, then the program will round down to 1.
Regarding Modulos, it is used to return the remainder of a division. In the above example we divide five by three, which is 1 with a remainder of 2. So thus, and thusly, modulos would return 2.
{mospagebreak title=The Judgmental Comparison Operators}
Comparison operators do what they sound like they do; compare things. It’s like your girlfriend. She compares you to her ex-boyfriends all the time. And worse still, she probably compares you to guys she sees walking down the street. Let’s face it; you’re a loser and she is looking for a real man. But don’t worry. When you finish learning the Python I’m teaching you, you can build a hot chick like the one in Weird Science that digs my…I mean your…nerdiness.
By the way…nerdiness is not in my spell checker. But nerd is oddly enough…
#!/usr/local/bin/python
beers = 99
if beers == 100:
print "Did I die? This doesn’t look like Heaven…but all that beer tells me it must be."
elif beers >100:
print "When I get done with all this beer, yer gonna look pretty."
else:
print "There aren’t enough beers here to make you look good. Fortunately for you I am desperate."
The above program assigns the value 100 to the variable beers. I know, it sounds delicious, but try to pay attention. Next, the program enters into an If statement that states if the value of beers is equal to (==) 100, print some text. If not, then if the value of beers is greater than 100, print some other text. Finally, if beers is equal to anything else, print a different text. Let’s say that we set the value of beers to 99; here is what would print out:
There aren’t enough beers here to make you look good. Fortunately for you I am desperate.
Here is another program showcasing some of the other comparison operators:
#!/usr/local/bin/python
beers = 98
if beers >= 100:
print "Did I die? This doesn’t look like Heaven…but all that beer tells me it must be."
elif beers <=98:
print "When I get done with all this beer, yer gonna look pretty."
else:
print "There aren’t enough beers here to make you look good. Fortunately for you I am desparate."
This program works pretty similar to our previous one. Only here we use the equal to or greater than (>=) and the less than or equal to (<=) operators. You will not that we assigned the value of 98 to our variable beers. I did this to showcase what can happen if you don’t pay close attention to your operators. Since our operators are assigned as >=100 and <=98, it leaves a space for a loop hole, the number 99. Fortunately we put in an else clause to handle any values not covered by our greater than/less than/equal to operators. If you run the program, it will print out:
There aren’t enough beers here to make you look good. Fortunately for you I am desperate.
And lastly, if we are really finicky, we can use the not equal to (!=) operator:
#!/usr/local/bin/python
beers = 100
if beers != 100:
print "Did I die? This doesn’t look like Heaven…but all that beer tells me it must be."
else:
print "There aren’t enough beers here to make you look good. Fortunately for you I am desparate."
In the above example, it would only execute the Else clause if the value of beers was equal to 100.
{mospagebreak title=Boolean is Not a Type of Broth}
Boolean operators let us test if a value or more than one value is true. The AND operator says if this AND that are true, do this. The OR says if this OR that is true, do this. And Not is for inverse values.
Let’s apply these to our majestic beer program:
#!/usr/local/bin/python
beers = 0
timmy = 20
tommy = 20
drunk = timmy+tommy
if beers == 0 and drunk < 100:
print "Did you guys drop a beer or something?"
elif beers <20 or drunk >80:
print "There aren’t enough beers here to make you look good. Fortunately for you I am desparate."
else:
print "Glug glug glug"
Bitwise and Shift
We are going to skip the Bitwise and Shift functions for now, as those are mainly for low-level programming, and mostly what we will be learning is high-level programming. For now, just know that they exist on a plane far cooler than the one you do.
{mospagebreak title=Assignment Operators}
These little guys are what make it all worthwhile. You’ve already worked with the ‘=’ assignment operator, which assigns a value. Now we will learn to work with the rest of these handy hooligans:
#!/usr/local/bin/python
beers = 20
print beers
beers +=5
print beers
beers -=5
print beers
beers *=10
print beers
beers /=10
print beers
beers **=5
print beers
beers %=9
print beers
The result of this is:
20
25
20
200
20
3200000
5
Note that in the above example we used all of what are called Augmented Assignment operators. If we said beers +=1 for example, what we are really saying is beers = beers + 1. In the above example that would mean beers value is now 21. The only augmenter we left out was the //= which would just have given a similar result to the /= (remember, for now the division operator truncates, but that will change in the future).
And Lastly, Operator Precedence
Operator Precedence determines in which order operations are evaluated. You can control this with parentheses(). Consider this:
2+10*10 = 120 in normal math.
2+ (10*10) = 102 because the parentheses gives precedence to the equation 10*10.
That isn’t all there is to say about operator precedence; there is a whole table’s worth of operator precedence I could show you, but at the moment we are out of time and really, that table is quite boring.
So come back often as we continue our death-defying discussion of…what were we talking about again? Oh yeah, Python.
Till then…