Introduction to Python Programming - Python Mathematical Operators (Page 4 of 4 )
Here is a pretty table listing the Mathematical Operators in Python:
Operator | Symbol | What it Does |
Addition | + | 2+2 == 4 |
Subtraction | - | 4-4 == 0 |
Division | / | 12 / 3 == 4 |
Multiplication | * | 2 * 4 == 8 |
Exponentiation | ** | 10 ** 2 == 100 (Basically you are saying 10 times 10; if you wrote 10 ** 3 it would be 10 times 10 times 10 or 1000) |
Remainder or Modulus | % | Returns the remainder from a division. 17 % 4 == 1. If there is no remainder, the value would be 0. Ie; 16 % 4 == 0. |
Also, when working with math note that if you do not enter an equation in as a decimal, the result will not have a decimal. So if I said 14 / 3, it would give me the result of 4. If I used the expression 14.0 / 3.0 it would return 4.66666666667. In addition, you give mathematical equations precedence by using parentheses(). So: 3 * 2 + 2 would equal 8, whereas 3 * (2+2) would equal 12. This is because Python sees the parentheses and knows to calculate that portion first, then multiply by 3.
Commenting
You may have noticed earlier that I used the # symbol followed by some text. This is known as a comment. When Python sees the # it gets scared and runs away to the next line. It won't even read what you have written. Comments are used to leave notes for yourself or other programmers who may eventually view your code. They are typically used to describe what a particular snippet of code does. That way when you view your code six years later at four A.M. you don't rip your arm hair out trying to remember what the heck that function was supposed to do.
Well that is it for this episode. In our next action-packed show, we will discuss how to get data from the user and how to use it, and my good buddy the variable. As always, thanks for dropping in and hope to see you soon.
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. |