We want to look at performing some simple math in Python. To do so, we’ll first look at our last example from our introduction to Python examples.
todaysTemp = input('Current temperature?')
print('It is currently: ', todaysTemp)
We took in a number, but in reality, we took in a string that had numbers in it. If we want to treat the value as a number, we’d need to use either the int() or the float() functions to convert from a string to a number.
Let’s try a program with an error, and then see how we can fix it.
todaysTemp = input('Current temperature?')
todaysTemp = (todaysTemp - 32) * 5 / 9
print('It is currently: ', todaysTemp)
Notice the error it gives when we try to run it. Also notice when it gives the error – when it encounters it. This is due to Python being interpreted. It only finds errors as it encounters them at run time, meaning that code may have errors and you may not know about it if you don’t do good testing.
Note: if you didn’t get an error that just means your system is set up to auto-convert strings to numbers when it senses the need arises. This is usually right 99% of the time it seems.
To fix it, we’ll use the int() function. This takes a string, and converts it to an integer.
todaysTemp = input('Current temperature?')
todaysTemp = (int(todaysTemp) - 32) * 5 / 9
print('It is currently: ', todaysTemp)
Now we can test out the function, and it should work as we put in different values to test, such at 88, 32, 0, and -40. Any valid integer (think back to your Algebra days) should work for us.
We can see how we’re using a variable (todaysTemp) to run a formula. The order of operations work in Python, like they did in Algebra. So that should make working with math in Python a lot easier.
We use the star key (* – <shift> + 8) to indicate multiplication. You must use the * key. You cannot put two variables together to have it work, or a number next to a parentheses like you would in Algebra.
Floating Point Numbers
The forward slash is division. With integers, you won’t get the remainder, just the main number. If you are using floating point numbers, then you will get the number and the remainder as a decimal.
Floating point numbers are numbers which have a decimal in them. Such as 88.3. If you input that number into your test program, it would have failed with an error about using the wrong data type as seen below:
ValueError: invalid literal for int() with base 10: '88.3'
Python sees the decimal in the inputted number, and gets confused since a decimal cannot exist in an integer.
The solution is to use the float() function. This will allow Python to understand a number with a decimal point. In Math it was referred to as a real number, in computers, we call them floating point numbers. One thing to know about a floating point number, is that it is an approximation. Generally, they are accurate for several digits, then the accuracy will start to fall off. This isn’t important for most circumstances, however, when working with very large, or very small numbers, it may become important.
But what happens if you are doing math with a floating point and an integer? Can Python make it work, or does it have to be told each time.
Let’s say your performing a math function on two numbers, like addition. If both numbers are integers, then the result will be an integer. If both numbers are floating point numbers, the result will be a floating point number.
However, we will often run into cases where we need to mix numbers. Well Python is smart enough to temporarily convert an integer into a float, so that it result will be a float, and no data will be lost.
Order of Precedence
We’ve looked at some simple math functions, lets look at some more, so we get a full set. I’ve included them in the order of operations, so you know which order Python will execute them in.
Algebraic Expression | Python | Example |
---|---|---|
Parenthesis – () | ( ) | 7 * ( 3 + 4 ) |
Exponent | ** | 3.14 * r ** 2 |
Multiplication / Division / Remainder | ||
Multiplication | * | 3 * 4 |
Division | / | 12 / 3 |
Remainder | % | 12 % 5 |
Addition / Subtraction | ||
Addition | + | 12 + 7 |
Sutraction | – | 9 – 5 |
Math in Python was originally found on Access 2 Learn