Skip to content
BENIBIX Learn

Operators in Python

Level 2 · Values and variables

The signs that do maths, compare two values, and join true or false answers. Why == is not =, and what and, or and not do.

Course contentsLesson 9 of 18

After this lesson you can

  • Do maths with + - * / and also // and %
  • Compare two values with == and !=
  • Join tests with and, or and not

12 mincovers 4 glossary entries

Worth reading first: Data Types in Python

Which signs do maths, compare and combine

An operator is a sign that works on values and gives back a result. In 7 + 2, the + is the operator and the answer is 9. Python's operators fall into three groups: ones that do maths, ones that compare, and ones that join true and false answers together.

The arithmetic operators do maths. + adds, - takes away, * multiplies and / divides. Two more are worth learning early. // divides and drops the remainder, so 7 // 2 gives 3. % gives only the remainder, so 7 % 2 gives 1. ** raises to a power, so 2 ** 10 gives 1024.

% is more useful than it looks. A number is even when n % 2 gives 0. A clock wraps round with hour % 12. Python also follows the usual order from school maths: 2 + 3 * 4 gives 14, not 20, because the multiplying comes first. Brackets change the order.

The comparison operators compare two values and answer True or False. == asks if two values are equal and != asks if they are different. >, <, >= and <= compare sizes. So 5 > 3 gives True, and "cat" == "Cat" gives False, because capital letters count.

The logical operators join true and false answers. In Python they are the words and, or and not. a and b is True only if both are True. a or b is True if at least one is. not flips True to False. So age >= 13 and age <= 19 is True for a teenager.

Python stops early when it can. In a and b, if a is False, the whole answer must be False, so b is never checked. That lets you write safe tests like name and name[0] == "A", where the second part runs only when name is not empty.

The classic mistake is writing = when you mean ==. One sign stores a value. Two signs compare. Python refuses if x = 5: with a SyntaxError, which is a kindness, because some languages run it and quietly store 5 instead.

The three kinds of operator

KindSignsExample and result
Arithmetic+ - * / // % **7 // 2 gives 3
Comparison== != > < >= <=5 > 3 gives True
Logicaland or notTrue and False gives False
Maths signs give numbers back. Comparison and logical signs give True or False back.

Worked example

Emma splits a bill

Emma has 23 sweets to share between 4 friends. 23 // 4 gives 5 each, and 23 % 4 gives 3 left over.

She also checks who gets a free drink: age < 12 or age > 65. For a 70 year old the test gives True, and for a 30 year old it gives False.

The words for this lesson4

Check yourself

What does 17 % 5 give in Python?

Finished this lesson?

Marking it complete also marks its 4 glossary entries learned, which updates your roadmap and your saved page. 0 are already marked.