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
Here are some commonly used mathematical operators
||division (see note below)|
||floor division (e.g. 5//2=2) - Available in Python 2.2 and later|
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.
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.
// 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:
>>> 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
// 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
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.)
||Before operating on anything else, Python must evaluate all parentheticals starting at the innermost level. (This includes functions.)||Please|
||As an exponent is simply short multiplication or division, it should be evaluated before them.||Excuse|
||Again, multiplication is rapid addition and must, therefore, happen first.||My
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
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.")
- Ask the user to specify the number of sides on a polygon and find the number of diagonals within the polygon.
- Take the lengths of two sides of a right-angle triangle from the user and apply the Pythagorean Theorem to find the hypotenuse.