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:
formula_1
So, let's turn this formula into a program!
mass_kg = int(input("What is your mass in kilograms?" ))
mass_stone = mass_kg * 2.2 / 14
print("You weigh", mass_stone, "stone.")
Run this program and get your weight in stone! Notice that applying the formula was as simple as putting in a few mathematical statements:
mass_stone = mass_kg * 2.2 / 14
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:
 »&gt; print(0.6/0.2)
 3.0
 »&gt; print(0.6//0.2)
 2.0
For the Python 2.x series, codice_1 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, codice_1 does "true division" for all types.
This can be fixed by putting a codice_3 around a normal division sign, because of a Python error causing round(0.5) to round down.
The operator codice_4 always performs Euclidean (or integer-type) division, which includes a quotient term (obtained from the codice_4 operator) and a remainder term (obtained from the codice_6 operator). In the previous example we have seen that the quotient term codice_7 is codice_8, which can be verified by extending the above example:
 »&gt; 0.6 == 0.2 * ( 0.6 // 0.2 ) + 0.6 % 0.2
 True
 »&gt; 0.6 // 0.2
 2.0
 »&gt; 0.6 % 0.2
 0.19999999999999996
The difference between the operations codice_1 and codice_4 when applied to decimal numbers is due to the way decimal numbers are stored in Python and rounding.
 »&gt; print(0.6 / 0.2)
 3.0
 »&gt; 0.6 / 0.2
 2.9999999999999996
 »&gt; 2.0 + ( 0.6 % 0.2 ) / 0.2
 3.0
 »&gt; 0.6 / 0.2 == ( 0.6 // 0.2 ) + ( 0.6 % 0.2 ) / 0.2
 False
 »&gt; round( 0.6 / 0.2 ) == ( 0.6 // 0.2 ) + ( 0.6 % 0.2 ) / 0.2
 True
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.
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 codice_11 statement prints numbers to 10 decimal places. But what if you only want one or two? We can use the codice_12 function, which rounds a number to the number of decimal points you choose. codice_12 takes two arguments: the number you want to round, and the number of decimal places to round it to. For example:
 »&gt; print (round(3.14159265, 2))
 3.14
Now, let's change our program to only print the result to two decimal places.
print ("You weigh", round(mass_stone, 2), "stone.")
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:
twoSigFigs = round(mass_stone, 2)
numToString = str(twoSigFigs)
print ("You weigh " + numToString + " stone.")
To learn about more formatting options, see section Formatting in chapter Input and Output.