Python Operators are Mathematical!
Not only is math essential to programming, it’s also fascinating. Especially when you have a super-powerful calculator like Python to do the heavy-lifting for you. In this tutorial you will learn how to use Python operators to perform basic mathematical operations. You don’t have to be an Einstein to be a computer scientist (though it wouldn’t hurt). You only need to understand the basics. Then you can fake your math chops just like the rest of us. If you are just joining us, you may want to start at the beginning with our Introduction to Python.
Python operators, operands and operations
Imagine you are in an operating room at Rossum’s Universal Robot Hospital. Dr. Plus, Dr. Minus, Dr. Slash and Dr. Asterisk are working on a patient.
Our robot patient needs to maintain a certain voltage to continue running, currently 12 V. The Drs. operate.
Dr. Plus increases the voltage by 108.
>>>12 + 108
Dr. Minus makes a minor adjustment:
>>>120 - 10
Dr. Asterisk has determined that the robot is European and multiplies the voltage by a factor of 2:
>>>110 * 2
At the last moment, Dr. Slash rushes in and saves the patient from circuit overload:
>>>220 / 44
Commercial Break: Floats, Integers & Division
Why did 220/44 return 5.0 rather than simply 5? Just to make your head explode, Python 3 has two division operators.
A single slash, ‘/’, is true division. The other operator is called floor division and consists of two slashes, ‘//’. True division returns what is called in computer science a floating point. In Python we refer to these as floats. You can think of a float as a number with a decimal point bobbing about inside it. Or, because true division is a single slash, ‘/’, it is lighter than floor division and therefore floats. It’s really useful when working with exponents. Floor division on the other hand, because it is composed of two slashes, is heavier, and bottoms out at the nearest integer. For example:
What’s an integer? It’s any positive or negative whole number, including zero. You may find that you don’t want a float. You can fix that with the int() function.
And you can convert integers to floats with the float() function.
But we’re getting ahead of ourselves…
We Now Return You to Rossum’s Universal Robot Hospital
With our patient near the end of its consumer life cycle, an expert is called in.
“Paging Dr. Powers, Dr. Powers…”
Dr. Powers saunters into the operating room and performs his specialty: exponentiation.
>>> 5 ** 2
You can think of exponentiation as “times times”. In the above example, 5**2 is the same as 5 * 5. And 5**3 can be thought of as 5 * 5 * 5.
This metaphorical medical drama illustrates the basic mathematical constructs used in any programming language: operators, operands, and operations. An operator is a special symbol indicating a specific computation. An operand is a value on which an operator performs. And an operation is any statement in which one or more operands are operated upon by an operator.
Let’s dissect this on our operating table:
|+||Addition||12, 108||12 + 108||120|
|–||Subtraction||120, 10||120 -10||110|
|*||Multiplication||110, 2||110 * 2||220|
|/||Division||220, 44||220 / 44||5|
I said above that math is fascinating. Let me prove it with this magic trick. I will transform any whole number into the number 3. Follow along with your Python prompt.
Pick a number, any number, and enter it in your Python prompt. I’m going to pick lucky number 7.
Add 5 and hit Enter.
>>>7 + 5
Multiply the result by 2.
>>>12 * 2
>>>24 - 4
Divide by 2.
>>> 20 / 2
Subtract the original number.
>>> 10 - 7
Mathematical! Or should I say, mathemagical? Use this magic link to learn our next trick: operator precedence.