Operators Flashcards
A ** (double asterisk) sign is an exponentiation (power) operator.
Its left argument is the base, its right, the exponent.
Classical mathematics prefers notation with superscripts, just like this: 2’3. Pure text editors don’t accept that, so Python uses ** instead, e.g., 2 ** 3.
Remember: It’s possible to formulate the following rules based on this result:
when both ** arguments are integers, the result is an integer, too;
when at least one ** argument is a float, the result is a float, too.
An * (asterisk) sign is a
multiplication operator.
A / (slash) sign is a
divisional operator.
The result produced by the division operator is always a float,
regardless of whether or not the result seems to be a float at first glance: 1 / 2, or if it looks like a pure integer: 2 / 1.
A // (double slash) sign is an integer divisional operator. It differs from the standard / operator in two details:
its result lacks the fractional part - it’s absent (for integers), or is always equal to zero (for floats); this means that the results are always rounded;
it conforms to the integer vs. float rule.
As you can see, integer by integer division gives an integer result.
All other cases produce floats.
This is very important: rounding always .
goes to the lesser integer
Look at the code below and try to predict the results once again:
print(-6 // 4)
print(6. // -4)
Note: some of the values are negative. This will obviously affect the result. But how?
The result is two negative twos. The real (not rounded) result is -1.5 in both cases. However, the results are the subjects of rounding. The rounding goes toward the lesser integer value, and the lesser integer value is -2, hence: -2 and -2.0.
Integer division can also be called
floor division. You will definitely come across this term in the future.
Its graphical representation in Python is the % (percent) sign, which may look a bit confusing.
Try to think of it as of a slash (division operator) accompanied by two funny little circles.
modulo
The result of the operator is a remainder left after the integer division.
In other words, it’s the value left over after dividing one value by another to produce an integer quotient.
Note: the operator is sometimes called modulo in other programming languages.
Take a look at the snippet - try to predict its result and then run it:
print(14 % 4)
14 // 4 gives 3 → this is the integer quotient;
3 * 4 gives 12 → as a result of quotient and divisor multiplication;
14 - 12 gives 2 → this is the remainder.
print(12 % 4.5)
3.0 - not 3 but 3.0 (the rule still works: 12 // 4.5 gives 2.0;
2.0 * 4.5 gives 9.0;
12 - 9.0 gives 3.0)
division by zero
does not work
Do not try to:
perform a division by zero;
perform an integer division by zero;
find a remainder of a division by zero.
operator is the + (plus) sign, which is fully in line with mathematical standards.
Again, take a look at the snippet of the program below:
print(-4 + 4)
print(-4. + 8)
The result should be nothing surprising. Run the code to check it.
addition
operator is obviously the - (minus) sign, although you should note that this operator also has another meaning - it can change the sign of a number.
subtraction