Programming · Operators
up to +100 XP
Stop 2.4 · Programming · 3 min

Operators

Master arithmetic, relational, and Boolean operators

Learning objectives:
  • Understand arithmetic, relational and Boolean operators (2.4.1–2.4.3)
  • Use, interpret, create and debug code with operators (2.4.4–2.4.7)

Arithmetic operators

OperatorPythonExampleResult
Add+7 + 29
Subtract-7 - 25
Multiply*7 * 214
Divide/7 / 23.5
Exponentiation**7 ** 249
Integer division//7 // 23
Modulus (remainder)%7 % 21

/ always gives a float. // gives the whole-number part. % is useful for checking if a number is even (n % 2 == 0).

Relational operators

These compare two values and give True or False.

MeaningPython
Equivalence (equal to)==
Not equal!=
Less than<
Greater than>
Less than or equal to<=
Greater than or equal to>=

Boolean operators

OperatorResult is True when…
notthe condition is False
andboth conditions are True
orat least one condition is True
age = 17
has_ticket = True
if age >= 16 and has_ticket:
    print("Entry allowed")

Common operator bugs

Quick check · 12 questions · up to 100 XP

Check what stuck.

+10 XP each
Question 1

Question 1 of 12

Which operator is used for exponentiation in Python?

Question 2

Question 2 of 12

What is the result of 17 // 5?

Question 3

Question 3 of 12

What is the result of 17 % 5?

Question 4

Question 4 of 12

What does 7 / 2 give?

Question 5

Question 5 of 12

Which relational operator means "not equal"?

Question 6

Question 6 of 12

What is the value of (5 > 3) and (2 > 4)?

Question 7

Question 7 of 12

What is the value of not (3 == 3)?

Question 8

Question 8 of 12

A programmer writes if age = 18:. What is the error?

Question 9

Question 9 of 12

Which condition is True only for ages from 11 to 19 inclusive?

Question 10

Question 10 of 12

What is the result of 2 + 3 * 4?

Question 11

Question 11 of 12 · Match the pairs

Match each operator to what it does.

%
//
==
!=
Question 12

Question 12 of 12 · Coding

Write is_even(n) that returns True if n is even, otherwise False.

Open the IDE
Hint

A number is even when n % 2 == 0.