Python Note 3
Python Note 3
For example, just as in arithmetic, the + (plus) sign is the operator which is
able to add two numbers, giving the result of the addition.
Not all Python operators are as obvious as the plus sign, though, so let's go
through some of the operators available in Python, and we'll explain which
rules govern their use, and how to interpret the operations they perform.
We'll begin with the operators which are associated with the most widely
recognizable arithmetic operations:
+ , - , * , / , // , % , **
The order of their appearance is not accidental. We'll talk more about it once
we've gone through them all.
Classical mathematics prefers notation with superscripts, just like this: 23.
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:
The value in front of the slash is a dividend, the value behind the slash,
a divisor.
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.
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 .
Its graphical representation in Python is the % (percent) sign, which may look
a bit confusing.
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.
Take a look at the snippet - try to predict its result and then run it:
print(14 % 4)
Operators: addition
The addition operator is the + (plus) sign, which is fully in line with
mathematical standards.
operators
But the minus operator may be used in a different (unary) way - take a look at
the last line of the snippet below:
print(-4 - 4)
print(4. - 8)
print(-1.1)
Most of Python's operators have left-sided binding, which means that the
calculation of the expression is conducted from left to right.
This simple example will show you how it works. Take a look:
print(9 % 6 % 2)
The result should be 1 . This operator has left-sided binding. But there's one
interesting exception.
print(2 ** 2 ** 3)
2 ** 2 → 4 ; 4 ** 3 → 64
2 ** 3 → 8 ; 2 ** 8 → 256
Run the code. What do you see?
The result clearly shows that the exponentiation operator uses right-
sided binding.
Key takeaways
3. Arithmetic operators in
Python: + (addition), - (subtraction), * (multiplication), / (classic division ‒
always returns a float), % (modulus ‒ divides left operand by right operand
and returns the remainder of the operation, e.g., 5 % 2 =
1 ), ** (exponentiation ‒ left operand raised to the power of right operand,
e.g., 2 ** 3 = 2 * 2 * 2 = 8 ), // (floor/integer division ‒ returns a number
resulting from division, but rounded down to the nearest whole number,
e.g., 3 // 2.0 = 1.0 )