Python Operators

+ - * work as usual, ** is power (2 ** 8 = 256, but ^ is not power: it is another operation called XOR). There are three ways to divide. / always gives a…

Mr. Darto has 17 cups of coffee and trays that hold 5 cups each. How many trays do the 17 cups fill? How many of those trays are full? How many cups are left over? The three questions have three different answers: 3.4 trays, then 3 trays, then 2 cups. Python has one sign for each question: /, // and %.

Signs such as / and % are called operators: signs that work on values, such as adding, dividing or comparing.

Arithmetic operators

The operators for maths:

Operator Meaning Example Result
+ add 15 + 5 20
- subtract 50 - 45 5
* multiply 15 * 3 45
/ divide 17 / 5 3.4
// divide, rounded down (floor division) 17 // 5 3
% remainder (modulo) 17 % 5 2
** power 2 ** 8 256

The order is the same as in maths: ** first, then *, /, //, %, and after that + and -. So 2 + 3 * 4 is 14. Anything in parentheses is calculated first, so (2 + 3) * 4 is 20.

Three ways to divide

Here is Mr. Darto's calculation with the cups and trays:

cups = 17
per_tray = 5

print(cups / per_tray)       # 3.4
print(cups // per_tray)      # 3, the full trays
print(cups % per_tray)       # 2, the cups left over
print(10 / 5)                # 2.0

10 / 3 is 3.3333333333333335. A computer stores a float in binary (numbers made of only 0 and 1) with about 16 digits, so the last digit can look strange. This is not an error. It is how Python writes that result.

Power is **, not ^

2 ** 8 means 2 to the power of 8, which is 256. In Python the ^ sign means something else: XOR, an operation on bits (binary digits). So 2 ^ 8 gives 10, with no error. One more thing: -2 ** 2 gives -4, because the power is calculated before the minus sign. Write (-2) ** 2 if you mean 4.

Negative numbers: // rounds down

The same three ways to divide, now with -17 divided by 5:

print(-17 / 5)    # -3.4
print(-17 // 5)   # -4
print(-17 % 5)    # 3

Floats aren't always exact

Three results with floats:

print(0.1 + 0.2)          # 0.30000000000000004
print(0.1 + 0.2 == 0.3)   # False
print(89 / 3)             # 29.666666666666668

On the second line, == checks whether two values are equal. The result is False, because 0.1 + 0.2 is not exactly 0.3. A computer stores floats in binary, and 0.1 can't be stored exactly in binary. In the same way, 1/3 can't be written exactly in decimal: 0.333... never ends. That is why prices and money at the till are stored as an int: 15, a whole number of dollars, not 15.0. A long result, such as the $89 bill shared by 3 people on the third line, is made tidy when it is shown. The next lesson, Strings in Python, shows how.

There are three options for numbers that are not whole. The last two need an import line at the top of the code, which the lesson Modules & Imports explains:

Comparisons and logic

Comparison operators compare two values. Logic operators combine the results:

total = 89
print(total >= 50)                          # True
print(total >= 50 and total < 100)          # True
print(total < 50 or total > 100)            # False
print(not total >= 50)                      # False

📌 The walrus :=

The := operator (Python 3.8 and later) sticks a name onto a value and uses that value in the same place:

data = [1, 2, 3, 4, 5]
if (n := len(data)) > 3:
    print(n)   # 5

⚠️ Signs that look alike but mean different things

TL;DR: + - * work as usual, ** is power (2 ** 8 = 256, but ^ is not power: it is another operation called XOR). There are three ways to divide. / always gives a float (10 / 5 = 2.0, 10 / 3 = 3.3333333333333335). // rounds down (17 // 5 = 3, -17 // 5 = -4). % gives the remainder (17 % 5 = 2). Floats aren't always exact (0.1 + 0.2 = 0.30000000000000004), so money is stored as an int. Comparisons give True/False, and you can combine them with and, or, not. The walrus := sticks a name onto a value and uses that value at the same time.

⚡ Three ways to divide

Write:

cups = 17
per_tray = 5

print(cups / per_tray)       # 3.4
print(cups // per_tray)      # 3, the full trays
print(cups % per_tray)       # 2, the cups left over
print(10 / 5)                # 2.0

Meaning: / always gives a float (a fractional number), even when the division leaves no remainder: 10 / 5 is 2.0. // divides, then rounds down. % gives the remainder.

⚡ Power is **

Guess first: what is 2 ^ 8?

10, with no error. In Python, ^ is not power. It is XOR, another operation on bits (the 0s and 1s inside a computer).

Meaning: Power is written **: 2 ** 8 means 2 to the power of 8, which is 256.

⚡ Floats aren't always exact

Guess first: == checks whether two values are equal. What does 0.1 + 0.2 == 0.3 give?

False. 0.1 + 0.2 gives 0.30000000000000004, because a computer stores numbers in binary (only 0 and 1), and 0.1 can't be stored exactly.

Meaning: Float calculations aren't always exact. That is why prices at the till are stored as an int: 15, not 15.0.

⚡ Comparing

Meaning: == (equal to), != (not equal to), <, >, <= (less than or equal to) and >= (greater than or equal to) compare two values. The result is True or False. You can combine results with and (both are True), or (at least one is True) and not (turns True into False, and False into True). Remember: = sticks a name onto a value, == compares.

What you will learn