# Python Programming/Basic Math

| Previous: Operators | Index | Next: Control Flow |
Now that we know how to work with numbers and strings, let's write a program that might actually be useful! Let's say you want to find out how much you weigh in stone. A concise program can make short work of this task. Since a stone is 14 pounds, and there are about 2.2 pounds in a kilogram, the following formula should do the trick:
$m_{stone}={\frac {m_{kg}\times 2.2}{14}}$
So, let's turn this formula into a program!
Run this program and get your weight in stone! Notice that applying the formula was as simple as putting in a few mathematical statements:

## Mathematical Operators

Here are some commonly used mathematical operators
| Syntax | Math | Operation Name |
| a+b | $a+b\,$ | addition |
| a-b | $a-b\,$ | subtraction |
| a*b | $a\times b\,$ | multiplication |
| a/b | $a\div b\,$ | division (see note below) |
| a//b | $\lfloor a\div b\,\rfloor$ | floor division (e.g. 5//2=2) - Available in Python 2.2 and later |
| a%b | $a~{\bmod {~}}b\,$ | modulo |
| -a | $-a\,$ | negative value |
| abs(a) | $|a|\,$ | absolute value |
| a**b | $a^{b}\,$ | exponent |
| math.sqrt(a) | ${\sqrt {a}}\,$ | square root |
Beware that due to the limitations of floating point arithmetic, rounding errors can cause unexpected results. For example:
For the Python 2.x series, / does "floor division" for integers and longs (e.g. 5/2=2) but "true division" for floats and complex (e.g. 5.0/2.0=2.5). For Python 3.x, / does "true division" for all types.
This can be fixed by putting a round([math]-0.5) around a normal division sign, because of a Python error causing round(0.5) to round down.
The operator // always performs Euclidean (or integer-type) division, which includes a quotient term (obtained from the // operator) and a remainder term (obtained from the % operator). In the previous example we have seen that the quotient term 0.6 // 0.2 is 2.0, which can be verified by extending the above example:
The difference between the operations / and // when applied to decimal numbers is due to the way decimal numbers are stored in Python and rounding.

## Order of Operations

Python uses the standard order of operations as taught in Algebra and Geometry classes at high school or secondary school. That is, mathematical expressions are evaluated in the following order (memorized by many as PEMDAS), which is also applied to parentheticals.
(Note that operations which share a table row are performed from left to right. That is, a division to the left of a multiplication, with no parentheses between them, is performed before the multiplication simply because it is to the left.)
| Name | Syntax | Description | PEMDAS Mnemonic |
| Parentheses | ( ... ) | Before operating on anything else, Python must evaluate all parentheticals starting at the innermost level. (This includes functions.) | Please |
| Exponents | ** | As an exponent is simply short multiplication or division, it should be evaluated before them. | Excuse |
| Multiplication and Division | * / // % | Again, multiplication is rapid addition and must, therefore, happen first. | My Dear |
| Addition and Subtraction | + - | | Aunt Sally |

## Formatting output

Wouldn't it be nice if we always worked with nice round numbers while doing math? Unfortunately, the real world is not quite so neat and tidy as we would like it to be. Sometimes, we end up with long, ugly numbers like the following:
By default, Python's print statement prints numbers to 10 decimal places. But what if you only want one or two? We can use the round function, which rounds a number to the number of decimal points you choose. round takes two arguments: the number you want to round, and the number of decimal places to round it to. For example:
Now, let's change our program to only print the result to two decimal places.
This also demonstrates the concept of nesting functions. As you can see, you can place one function inside another function, and everything will still work exactly the way you would expect. If you don't like this, you can always use multiple variables, instead:
To learn about more formatting options, see section Formatting in chapter Input and Output.

## Notes

* ↑ What's New in Python 2.2
* ↑ PEP 238 -- Changing the Division Operator
| Previous: Operators | Index | Next: Control Flow |