</>CodexBash
Python
PythonBeginner9 min

Operators and expressions

Arithmetic including the three kinds of division, comparisons, and, or and not, shorthand assignment, and the order Python works things out in.

Taking a number apart with % and /step 1/6
4
0
7
1
n = 4071digit = -
Start with n = 4071. n % 10 gives the last digit; n / 10 (// in Python) drops it.

An expression is anything that produces a value: 3 + 4, price * 2, age >= 18. Operators are the symbols that combine values into expressions.

Arithmetic#

OperatorMeaningExampleResult
+add7 + 29
-subtract7 - 25
*multiply7 * 214
/divide7 / 23.5
//divide, keep the whole part7 // 23
%remainder7 % 21
**power7 ** 249

/ always gives a float, even for 8 / 2, which is 4.0. When you want a whole number, use //.

The three that matter most for problems#

// and % together take a number apart. For any whole number n, n % 10 is its last digit and n // 10 is everything else:

n = 4071
print(n % 10)     # 1
print(n // 10)    # 407

% tests divisibility. n % 2 == 0 means even; n % 3 == 0 means divisible by three.

% wraps around. To step through positions 0 to k - 1 in a circle, (i + 1) % k returns to 0 after k - 1.

Comparisons#

These produce True or False:

5 == 5      # equal
5 != 3      # not equal
5 > 3       # greater
5 <= 5      # less or equal

= assigns and == compares. Mixing them up is one of the most common beginner mistakes.

Python lets you chain comparisons the way you would write them in maths:

0 <= x < 10       # is x between 0 and 9?

and, or, not#

age = 20
has_ticket = True

age >= 18 and has_ticket    # True: both are true
age < 13 or has_ticket      # True: at least one is true
not has_ticket              # False

Python stops as soon as it knows the answer. In a and b, if a is false, b is never looked at. That lets you write checks safely:

items = []
len(items) > 0 and items[0] == "x"    # False, and items[0] is never read

Shorthand assignment#

score = 10
score += 5     # score = score + 5  ->  15
score -= 3     # 12
score *= 2     # 24
score //= 5    # 4

There is no ++ in Python. Write count += 1.

Order of operations#

Python follows the usual maths order: brackets, then **, then * / // %, then + -, then comparisons, then not, and, or.

2 + 3 * 4        # 14, not 20
(2 + 3) * 4      # 20
-2 ** 2          # -4: the power happens before the minus

When in doubt, add brackets. They cost nothing and make your intent clear to the next reader.

Key points#

  • / gives a float, // gives the whole part, % the remainder.
  • n % 10 and n // 10 take a number apart one digit at a time.
  • == compares; = assigns.
  • and and or stop as soon as the answer is known.
  • Use += instead of ++, and brackets whenever the order is not obvious.

Next: working with text, and reading what the user types.

Practise it

The same idea, in the order it is worth meeting it.

  1. Not startedTurn the digits aroundEasy
  2. Not startedReads the same backwardsEasy
Create a free account to track your progress