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:
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[edit  edit source]
Here are some commonly used mathematical operators
Syntax  Math  Operation Name 

a+b 
addition  
ab 
subtraction  
a*b 
multiplication  
a/b 
division (see note below)  
a//b 
floor division (e.g. 5//2=2)  Available in Python 2.2 and later  
a%b 
modulo  
a 
negative value  
abs(a) 
absolute value  
a**b 
exponent  
math.sqrt(a) 
square root 
Beware that due to the limitations of floating point arithmetic, rounding errors can cause unexpected results. For example:
>>> print(0.6/0.2)
3.0
>>> print(0.6//0.2)
2.0
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.^{[1]}^{[2]}
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 integertype) 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:
>>> 0.6 == 0.2 * ( 0.6 // 0.2 ) + 0.6 % 0.2
True
>>> 0.6 // 0.2
2.0
>>> 0.6 % 0.2
0.19999999999999996
The difference between the operations /
and //
when applied to decimal numbers is due to the way decimal numbers are stored in Python and rounding.
>>> print(0.6 / 0.2)
3.0
>>> 0.6 / 0.2
2.9999999999999996
>>> 2.0 + ( 0.6 % 0.2 ) / 0.2
3.0
>>> 0.6 / 0.2 == ( 0.6 // 0.2 ) + ( 0.6 % 0.2 ) / 0.2
False
>>> round( 0.6 / 0.2 ) == ( 0.6 // 0.2 ) + ( 0.6 % 0.2 ) / 0.2
True
Order of Operations[edit  edit source]
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[edit  edit source]
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 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:
>>> 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.