Python Operators - Assignment Operators
(Page 5 of 5 )
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...
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |