Arithmetic Operators and Expressions
There are seven arithmetic operators in Python, which are all binary, meaning they take two arguments each. Both arguments of an operator are often called its operands. For example, the
+ (plus) operator adds the left operand,
5, to the right operand,
In Python, it’s customary to surround operators like this with a single space character on both sides. The
- (minus) operator, on the other hand, subtracts the right operand from the left one. Note that both the
- operators have their unary counterparts, which take just one operand.
- flips the sign of a number, while a unary
+ has no effect but exists for symmetry. You can only use the unary form of these operators as a prefix, which appears to the left of the number.
* (star) operator multiplies one operand by another. There’s also a unary
* operator in Python, but it has a different meaning outside the arithmetic context, so you’re not going to cover it in this course.
The result of such a division is always a floating-point number, even when both operands are integers. If you’d like Python to automatically round the result of a division to the nearest whole number, then you can use the
// (integer division), also known as the floor division. Floor is a mathematical function, which does the rounding.
The last arithmetic operator in Python is the
% (modulus), which is closely related to division. It calculates the remainder of a division, so in this example,
5 divided by
2 has a remainder of
02:08 It’s worth noting that all of these operators will accept integers as well as floating-point numbers as operands. Most will also work with complex numbers, fractions, and decimals. You can even mix them together in a single operation, which will make some of them subject to promotion.
02:29 When you add two integers, then you also get an integer result. However, once you add an integer to a floating-point number or the other way around, then Python will first promote that integer to a float and return a floating-point result.
03:03 It’s possible to promote an integer to a float without any loss of information, but the reverse isn’t true, because you lose the fractional part. Promotion works for all arithmetic operators, even those that are normally expected to return integers, such as the in integer division or the modulus.
03:31 The modulus, on the other hand, can sometimes give you a fractional remainder. The regular division is kind of special because it always returns a floating-point result, even when both of its operands are integers.
03:44 There’s one exception though. What happens when you try dividing a number by zero? In mathematics, such an operation is undefined. When you try this in Python, then you’ll immediately get an error, so watch out for it.
Regarding the integer division, you might be thinking that all it does is discard or truncate any fractional part of the resulting number. You can simulate this behavior by calling the built-in
int() function on the division’s result. For example, when you divide
2, you get
04:52 This has to do with the floor function used for rounding behind the scenes, which behaves differently for positive and negative numbers. Note there are a few different strategies for rounding numbers in Python.
** operator needs a little explanation too. Normally, it’s a shorthand notation for multiplying a number by itself a few times. For instance,
5 raised to the power of
3 gives the same result as
5 * 5 * 5. When you use negative exponents, the result will be the same as
1 over the corresponding positive exponent.
16 is divisible by
8, so it has no remainder, in which case the modulus returns
0. Noted that because the modulus performs a division, its right operand cannot be equal to
0, as that will give you a familiar error. Also, you might get surprising results when you start using negative operands.
The formula for the
% operator in Python takes advantage of the integer division, which uses the floor function for rounding. That’s why you’re getting seemingly inconsistent results when using negative numbers as operands. Although Python comes with a floor function, you won’t cover it in this course. If you’d like to learn more about it, then you can check out the Real Python tutorial on rounding numbers in Python, which was mentioned earlier in the overview of the numeric types.
07:07 Okay, now that you know how to use the individual operators in Python, you can start making some more complicated arithmetic expressions with them. For example, you can combine addition and multiplication in a single expression.
There’s no upper limit on the number of operators you can use per line of code. The order in which Python evaluates the individual operations in an expression like this follows a well defined operator precedence. The
* operator, for example, has a higher precedence than the
+ operator, so it gets evaluated before the addition, even though it appears further to the right.
07:44 Python follows the rules of everyday arithmetic that you may remember from school. If you’d like to change the default order or make it more explicit, then you can group a given subexpression using parentheses. Now, the expression has been evaluated to a different result because you changed the order of calculations.
08:13 You can visit the official Python language reference to learn about its operators and their precedence. When you do, you’ll find that Python has many kinds of operators, not just the arithmetic ones, but at this stage you can ignore most of them while only remembering those that you already know. As you can see, the exponentiation operator has the highest precedence, while the addition and subtraction ones have the lowest precedence.
08:39 Some operators appear on the same row in a table because they share equal precedence, in which case Python will evaluate them left to right. To change the default order, you can group the operators using parentheses, which always take precedence.
08:57 Knowing about Python’s math operators and their precedence lets you use IDLE as a rudimentary calculator. However, unlike a pocket calculator that you might be used to, Python can sometimes lie to you due to the representation error of floating-point numbers, which you’ll learn about in the next lesson.
Become a Member to join the conversation.