Exploring Arithmetic Operators
00:00 There’s a lot of math in programming, so it’s no surprise that Python has a number of arithmetic operators. They perform mathematical calculations, use mainly standard math symbols, work with numeric values and operate as unary or binary.
00:16 Here’s the full list. The plus sign can work as either the unary positive operator, or the binary addition operator. The positive operation doesn’t really have a use.
00:26
It’s mainly a complement to unary negation. So +a just returns a, while a + b returns the sum of a and b. Unary negation with the minus sign flips the sign of an existing number.
00:39
Subtraction works as you’d expect. a - b returns b subtracted from a. The rest of the operators are also binary involving two operands.
00:49
Multiplication a * b equals the product of a and b. Division a / b returns the quotient of a divided by b.
00:58 An interesting note about division is that even when it returns a whole number, its type will be a float, never an integer. Getting a little more interesting now, the percent sign becomes the modulo operator.
01:09
a % b or a mod b returns the remainder of a divided by b. And its complement, floor division, is denoted by two slashes. a // b returns the quotient of a over b rounded down.
01:24
And lastly, the exponentiation operator denoted by two asterisks or stars. a ** b returns a raised to the power of b.
01:33 And as a general rule, when the operands are of differing types, the return type will be the one with the highest precision. For example, adding a float and an int will result in a float, but adding a float and a complex number will result in a complex number.
01:47
And examples in the REPL. Start by creating a couple variables, a with the integer value 5 and b with the integer value 2.
01:56
Now write some expressions: +a returns a, which is five; -b however, does return -2. a + b returns 7, the sum of a and b. a - b, equivalent to 5 - 2, returns 3.
02:15
And a * b returns 10, five times two. a divided by b, five over two, returns 2.5.
02:28 Remember, the modulo operation returns the remainder, so when dividing five by two, the remainder will be one.
02:34
Floor division a // b returns 2, because the division result is 2.5 and that is rounded down to 2. Finally, exponentiation a ** b returns 25, which is in fact five to the power of two.
02:51
A note about division: whether you use the division operator with ints or floats, for consistency, it will always return a float. So even if you divide the integer 10 by the integer 5,
03:03
despite having a whole number result, you still get the float 2.0. And it’s the same if one operand is a float, while the other is an integer, like 10.0 / 5, also returning 2.0.
03:16
But on the off chance one of your operands is in fact a complex number like 10 over 5j, now you get a complex number in result, -2j, which I’m just going to have to trust is the correct result.
03:30
Conversely, with floor division, for example, 10 // 4, the result is the integer 2. 10 over 4, rounded down, is 2.
03:40
And it’s the same if a negative divides a negative, like -10 // -4, also returning 2. But where it’s a bit tricky is when the result is negative, like 10 // -4, which returns -3 and you might’ve expected -2.
03:57 But because it’s always rounded down, it actually rounds to the next largest negative number. So be careful. And there’s no complex number example for this because floor division is incompatible with complex numbers.
04:10 So that was the arithmetic operators. I hope everything added up for you. Coming up next: comparison.
Become a Member to join the conversation.
