Skip to content
ComputerCS
Start learning
Computer Science & Entrepreneurship/Curriculum/2.3.1–2.3.3, 2.3.5–2.3.6

Python Programming · Lesson 2.3.1–2.3.3, 2.3.5–2.3.6

Arithmetic, Assignment and Expressions

The arithmetic seven (+ − * / // % **), comparisons that return True/False, and the precedence ladder that decides which operation goes first.

01

Learning objectives

  • Use all seven arithmetic operators and predict each result, including // and % on negatives-free examples.
  • Explain the difference between / (true division, float) and // (floor division, int).
  • Apply the precedence ladder: ** first, then * / // %, then + −, left to right.
  • Use comparison operators (== != > < >= <=) and state that they produce bool values.
  • Combine conditions with and, or, not and use % to test even/odd and divisibility.
02

Lesson notes

An expression is a combination of values, variables and operators that Python evaluates down to a single value. The arithmetic set has seven members. The familiar four: + addition, - subtraction, * multiplication, / division. Then three that make board papers interesting: // floor division (divide, drop the fraction), % modulo (the remainder after division), and ** exponent (power). One line covers the most-tested fact of the chapter: 17 / 5 is 3.4 (always a float), 17 // 5 is 3 (floored), and 17 % 5 is 2 (because 17 = 5 × 3 + 2).

The modulo operator % deserves special affection because it answers real questions directly. Is a number even? n % 2 == 0. Does 7 divide 91? 91 % 7 == 0. What digit does a number end in? n % 10. Extract the last two digits of a year? year % 100. Whenever a problem talks about remainders, cycles, or every-kth-item, % is the tool. Note also that % and // form a pair: for any division, (a // b) * b + a % b rebuilds a exactly — a neat check examiners expect you to know.

Precedence decides which operator binds first when an expression mixes several. The ladder, highest first: brackets (), then **, then * / // %, then + -, all evaluated left-to-right within the same rung. So 4 + 3 * 2 ** 2 becomes 4 + 3 × 4 = 16, NOT (4+3)×4 = 28. The classic safety net is brackets: when in doubt, add parentheses to make your intention the interpreter's instruction — and in traces, write one operation per line to show the order you used.

Comparison operators ask questions instead of computing quantities: == equal to (TWO equals signs — one means assignment!), != not equal, >, <, >=, <=. Every comparison returns a bool: 5 > 3 is True, marks == 88 is True only when marks holds exactly 88. The single most expensive typo in beginner Python is writing if marks = 88: — assignment inside a condition is a SyntaxError, precisely because the two jobs must never blur.

Comparisons chain and combine. Python allows the mathematical range form directly: 0 <= marks <= 100 is legal and reads beautifully. For combining independent questions, the logical operators and (both sides must be True), or (at least one side True) and not (flips True/False) build any compound condition you need: percentage >= 80 and attendance >= 75 gates an A grade on both requirements. Chapter 4 turns these into decisions; for now, practise evaluating them to a single True or False on paper.

03

Worked code examples

The division family on one number pair

/ gives the exact quotient 3.4 as a float; // drops the fraction to 3; % keeps the remainder 2. The final line shows the pair identity: 3 × 5 + 2 = 17.

a, b = 17, 5
print(a / b)    # true division
print(a // b)   # floor division
print(a % b)    # remainder
print((a // b) * b + a % b)   # rebuilds a
Output
3.4
3
2
17

Precedence, step by step

2 ** 2 first (4), then 3 * 4 (12), then 4 + 12 = 16. Brackets force (4+3) first, giving 28. Even ** chains go right-to-left: 2 ** (3 ** 2) = 2 ** 9 = 512.

print(4 + 3 * 2 ** 2)
print((4 + 3) * 2 ** 2)
print(2 ** 3 ** 2)   # ** is right-to-left!
Output
16
28
512

Comparisons and the even/odd test

n % 2 == 0 is the even test. The range check needs and; the choice needs or; not flips the answer — 42 < 100 is True, so not makes it False.

n = 42
print(n % 2 == 0)
print(n > 40 and n < 50)
print(n == 42 or n == 43)
print(not n < 100)
Output
True
True
True
False
04

Key terminology

expression

Values, variables and operators combined into something Python evaluates to one value.

modulo (%)

The remainder after floor division; the standard tool for even/odd and divisibility tests.

floor division (//)

Division that drops the fractional part, returning an int when both operands are ints.

precedence

The fixed priority order deciding which operator in an expression is applied first.

comparison operator

==, !=, <, <=, >, >= — operators that ask a question and return True or False.

05

Common misconceptions

Common mistake

/ and // do the same job.

Correct understanding

/ always returns the exact float quotient (17 / 5 = 3.4); // returns the floored int (17 // 5 = 3). Exams test this difference constantly.

Common mistake

% calculates a percentage.

Correct understanding

% is the remainder operator: 17 % 5 is 2. Percentages need arithmetic like obtained / total * 100.

Common mistake

In an expression without brackets, Python works strictly left to right.

Correct understanding

Precedence outranks position: ** beats * / // % which beat + -. Only within the same rung does left-to-right apply.

06

Check your understanding

1. The value of the expression 17 % 5 is:

2. Following Python's operator precedence, what is 4 + 3 * 2 ** 2?

3. Which expression correctly tests whether n is an odd number?

NERDLYY · FEEDBACK

Help shape Nerdlyy

Your feedback goes directly to the team. We read every submission.

No reply needed — you can send feedback without sharing contact details.