Operators Flashcards

1
Q

A ** (double asterisk) sign is an exponentiation (power) operator.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Remember: It’s possible to formulate the following rules based on this result:

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

An * (asterisk) sign is a

A

multiplication operator.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

A / (slash) sign is a

A

divisional operator.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

The result produced by the division operator is always a float,

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

A // (double slash) sign is an integer divisional operator. It differs from the standard / operator in two details:

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

As you can see, integer by integer division gives an integer result.

A

All other cases produce floats.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

This is very important: rounding always .

A

goes to the lesser integer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Integer division can also be called

A

floor division. You will definitely come across this term in the future.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

print(12 % 4.5)

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

division by zero

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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.

A

addition

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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.

A

subtraction

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

difference between unary and binary

A

In subtracting applications, the minus operator expects two arguments: the left (a minuend in arithmetical terms) and right (a subtrahend).

For this reason, the subtraction operator is considered to be one of the binary operators, just like the addition, multiplication and division 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)

17
Q

determines the order of computations performed by some operators with equal priority, put side by side in one expression.

A

binding

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.

18
Q

print(9 % 6 % 2)

A

There are two possible ways of evaluating this expression:

from left to right: first 9 % 6 gives 3, and then 3 % 2 gives 1;

from right to left: first 6 % 2 gives 0, and then 9 % 0 causes a fatal error.

19
Q

Repeat the experiment, but now with exponentiation.

Use this snippet of code:

print(2 ** 2 ** 3)

A

2 ** 2 → 4; 4 ** 3 → 64 (THIS IS WRONG)

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.

20
Q

Priority Operator
1 **
2 +, - (note: unary operators located next to the right of the power operator bind more strongly) unary
3 *, /, //, %
4 +, - binary

A

Note: we’ve enumerated the operators in order from the highest (1) to the lowest (4) priorities.

Try to work through the following expression:

print(2 * 3 % 5)

Both operators (* and %) have the same priority, so the result can be guessed only when you know the binding direction. What do you think? What is the result?

21
Q

Operators and parentheses
Of course, you’re always allowed to use parentheses, which can change the natural order of a calculation.

In accordance with the arithmetic rules, subexpressions in parentheses are always calculated first.

You can use as many parentheses as you need, and they’re often used to improve the readability of an expression, even if they don’t change the order of the operations.

A

An example of an expression with multiple parentheses is here:

print((5 * ((25 % 13) + 100) / (2 * 13)) // 2)

Try to compute the value that’s printed to the console. What’s the result of the print() function?