Python Programming/Basic Math

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search
Previous: Variables and Strings Index Next: Arrays

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!

mass_kg = int(raw_input("What is your mass in kilograms?" ))
mass_stone = mass_kg * 2.2 / 14
print("You weigh %d stone." % mass_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

[edit] 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 a \div b\, floor division (e.g. 5/2=2) - Available in Python 2.2 and later
a%b a \bmod b\, modulo
-a -a\, negation
abs(a) |a|\, absolute value
a**b a^b\, exponent
sqrt(a) \sqrt{a}\, square root

NOTE:
In order to use the sqrt() function, you must explicitly tell Python that you want it to load this function. To do that, write

from math import sqrt

at the top of your file.

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]

[edit] 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 print statement prints numbers to 10 significant figures. 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 significant figures.

print ("You weigh %f stone." % round(mass_stone, 2))

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.")

[edit] Notes

  1. What's New in Python 2.2
  2. PEP 238 -- Changing the Division Operator
Previous: Variables and Strings Index Next: Arrays