Python Operators are Mathematical!

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.

Dr. Minus makes a minor adjustment:

Dr. Asterisk has determined that the robot is European and multiplies the voltage by a factor of 2:

At the last moment, Dr. Slash rushes in and saves the patient from circuit overload:

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.

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:

OperatorEnglishOperandOperationResult
+Addition12, 10812 + 108120
Subtraction120, 10120 -10110
*Multiplication110, 2110 * 2220
/Division220, 44220 / 445
**Exponentiation5, 25**225

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.

Multiply the result by 2.

Subtract 4.

Divide by 2.

Subtract the original number.

Mathematical! Or should I say, mathemagical? Use this magic link to learn our next trick: operator precedence.

Python Operators are Mathematical! was posted by on . Jared likes to make things. He really wants you to watch The Hello World Program so you can learn the skills you need to build an awesome future. His contributions to the show include puppetry, 3D animation, doodling and speaking in a bad British accent. And yes, that is a fox sitting on his face.

Discuss this article in the Comments below, or and it with your friends. Learn more about Python by subscribing to The Hello World Newsletter .

Join the Discussion

Subscribe to our Mailing List

Stay up to date on the latest videos and tutorials with our monthly newsletter

The email entered is invalid
×