Edexcel International Advanced Level Computer Science, point 7.1.3
An operator is a symbol (or word) that carries out an operation on one or more values (operands). You must be able to select and use each kind in Python. Check the PLS for the exact set allowed.
a. Arithmetic operators
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ |
Add | 7 + 2 |
9 |
- |
Subtract | 7 - 2 |
5 |
* |
Multiply | 7 * 2 |
14 |
/ |
Divide (always a float) | 7 / 2 |
3.5 |
// |
Integer (floor) division: DIV | 7 // 2 |
3 |
% |
Remainder (modulus): MOD | 7 % 2 |
1 |
** |
Power | 2 ** 3 |
8 |
- Precedence: brackets, then
**, then*///%, then+-. So2 + 3 * 4is 14, and(2 + 3) * 4is 20. - Floor division with negatives rounds down:
-7 // 2is -4, and-7 % 2is 1. - Uses of
%: even or odd (n % 2 == 0), last digit (n % 10), wrapping round (i % length). - Uses of
//: removing the last digit (n // 10), whole units (minutes // 60hours).
total_seconds = 3725
hours = total_seconds // 3600 # 1
minutes = (total_seconds % 3600) // 60 # 2
seconds = total_seconds % 60 # 5
print(hours, minutes, seconds)
b. Relational (comparison) operators
They compare two values and give True or False.
| Operator | Meaning | Example | Result |
|---|---|---|---|
== |
Equal to | 5 == 5 |
True |
!= |
Not equal to | 5 != 3 |
True |
< |
Less than | 3 < 5 |
True |
> |
Greater than | 3 > 5 |
False |
<= |
Less than or equal | 5 <= 5 |
True |
>= |
Greater than or equal | 4 >= 5 |
False |
=assigns, but==compares. Mixing them up is a very common mistake.- Strings are compared character by character, using their character codes (2.3.1):
"apple" < "banana"is True, and upper-case letters come before lower-case ("Z" < "a"). - Python allows chained comparisons:
1 <= x <= 10.
c. Boolean operators
They combine or reverse Boolean values (5.3.1).
| Operator | Meaning | Example |
|---|---|---|
and |
Both true | age >= 18 and has_id |
or |
At least one true | is_member or has_voucher |
not |
Reverses the value | not logged_in |
Precedence: not, then and, then or. Use brackets to make the meaning clear.
age = 17
has_licence = True
is_instructor = False
can_drive = (age >= 17 and has_licence) or is_instructor
print(can_drive) # True
Short-circuit evaluation: in a and b, if a is False, b isn't evaluated. In a or b, if a is True, b isn't evaluated. This lets you guard an expression:
# no IndexError on an empty list
if len(items) > 0 and items[0] == "x":
print("found")
d. Bitwise operators
They work on the individual bits of whole numbers (2.2.2).
| Operator | Meaning | Example (binary) | Result |
|---|---|---|---|
& |
AND | 0b1100 & 0b1010 |
0b1000 |
| |
OR | 0b1100 | 0b1010 |
0b1110 |
^ |
XOR | 0b1100 ^ 0b1010 |
0b0110 |
~ |
NOT (flips all bits) | ~5 |
-6 |
<< |
Shift left | 5 << 2 |
20 |
>> |
Shift right | 20 >> 2 |
5 |
- Shift left by multiplies by . Shift right by divides by (rounding down).
- Masks:
value & 0b1000tests bit 3.value | 0b0100sets bit 2.value ^ 0b1111flips the low four bits. - Don't confuse the bitwise
&and|with the Booleanandandor.
flags = 0b10100110
print(bool(flags & 0b00000100)) # test bit 2 -> True
flags = flags | 0b00000001 # set bit 0
flags = flags & 0b11110000 # clear the low four bits
print(bin(flags)) # 0b10100000
Worked example
Task: (a) give the value of each expression. (b) write a condition that is true for a leap year.
(a)
| Expression | Value | Why |
|---|---|---|
29 // 6 |
4 | whole number of 6s in 29 |
29 % 6 |
5 | remainder () |
2 ** 4 + 1 |
17 | power first |
not (3 > 2 and 4 < 1) |
True | 3 > 2 is True, 4 < 1 is False, so and is False, and not makes it True |
12 & 10 |
8 | 1100 AND 1010 is 1000 |
3 << 3 |
24 |
(b) A year is a leap year if it is divisible by 4, but not by 100, unless also divisible by 400:
year = 2024
leap = (year % 4 == 0 and year % 100 != 0) or year % 400 == 0
print(leap) # True
Check: 1900 gives False (divisible by 100 but not 400), 2000 gives True.
Exam tip: know // (DIV) and % (MOD) well, because they appear in many algorithms. Don't confuse = with ==, or bitwise & with and. Use brackets in Boolean expressions. Work out operator precedence step by step when evaluating.
Check yourself
What do 41 // 8 and 41 % 8 give?
5 and 1.
What is the difference between = and ==?
= assigns a value to a variable. == compares two values and gives True or False.
What is the result of 5 | 2 using bitwise OR?
7 (101 OR 010 is 111).
What is the value of not (True and False)?
True.
How can the bitwise operators test whether bit 3 of a number is set?
AND the number with 0b1000 (8). If the result isn't zero, bit 3 is set.