Python Programming/Basic Math

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

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  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  is , 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.)

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: What is your mass in kilograms? 65 You weigh 10.2142857143 stone.

By default, Python's  statement prints numbers to 10 decimal places. But what if you only want one or two? We can use the  function, which rounds a number to the number of decimal points you choose. takes two arguments: the number you want to round, and the number of decimal places to round it to. For example:

>>> print (round(3.14159265, 2)) 3.14

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.