⚡0 XP🔥0

Operators

Lesson 8 of 33 ⏱ 7 min read ✎ 3 exercises ❓ 6-question quiz ▶ Runs Python

Arithmetic, comparison, logical, assignment, membership, identity and bitwise operators, plus operator precedence.

What is an operator?#

An operator is a symbol that performs an operation on values. In 10 + 5, the + is the operator and 10 and 5 are the operands.

Python has these groups of operators:

  1. Arithmetic
  2. Comparison (relational)
  3. Logical
  4. Assignment
  5. Membership
  6. Identity
  7. Bitwise

1. Arithmetic operators#

Operator Name Example Result
+ Addition 10 + 3 13
- Subtraction 10 - 3 7
* Multiplication 10 * 3 30
/ Division (always float) 10 / 3 3.3333...
// Floor division 10 // 3 3
% Modulus (remainder) 10 % 3 1
** Exponent (power) 10 ** 3 1000
▶ Try it YourselfPython
Ctrl + Enter to run
हिंदी में समझें
  • / हमेशा पूरा भाग देता है और जवाब float में आता है: 10 / 2 = 5.0
  • // (floor division) भाग देकर दशमलव हटा देता है: 17 // 5 = 3
  • % (modulus) शेषफल (remainder) देता है: 17 % 5 = 2 (क्योंकि 5 × 3 = 15 और 17 - 15 = 2)

% बहुत काम का है: कोई संख्या सम (even) है या नहीं - n % 2 == 0; आखिरी अंक निकालना - n % 10।

Where // and % are used in real programs#

▶ Try it YourselfPython
Ctrl + Enter to run

Operators with strings#

print("Py" + "thon")   # Python   (joining = concatenation)
print("ha" * 3)        # hahaha   (repetition)

2. Comparison operators#

They compare two values and return True or False.

Operator Meaning Example Result
== equal to 5 == 5 True
!= not equal to 5 != 3 True
> greater than 5 > 8 False
< less than 5 < 8 True
>= greater than or equal 5 >= 5 True
<= less than or equal 4 <= 3 False
Common Mistake

= assigns a value. == compares two values. Writing if x = 5: is a SyntaxError.

Python allows chained comparisons, just like maths:

▶ Try it YourselfPython
Ctrl + Enter to run

3. Logical operators#

Combine conditions:

Operator Result is True when... Example
and both sides are True age > 18 and has_id
or at least one side is True is_weekend or is_holiday
not reverses True/False not is_raining

Truth table:

A B A and B A or B not A
True True True True False
True False False True False
False True False True True
False False False False True
▶ Try it YourselfPython
Ctrl + Enter to run
Did you know?

Short-circuiting: in A and B, if A is False, Python doesn't even check B (the answer is already False). In A or B, if A is True, B is skipped. This avoids errors like x != 0 and 10 / x > 2.

4. Assignment operators#

Shortcuts for updating a variable:

Operator Same as
x += 5 x = x + 5
x -= 5 x = x - 5
x *= 5 x = x * 5
x /= 5 x = x / 5
x //= 5 x = x // 5
x %= 5 x = x % 5
x **= 2 x = x ** 2
▶ Try it YourselfPython
Ctrl + Enter to run
Did you know?

Python has no ++ or -- operators. Use count += 1.

5. Membership operators: in, not in#

Check whether a value exists inside a string, list, tuple, set or dict:

▶ Try it YourselfPython
Ctrl + Enter to run

6. Identity operators: is, is not#

is checks whether two names point to the same object in memory (not just equal values).

a = [1, 2, 3]
b = [1, 2, 3]
c = a
print(a == b)   # True  - same values
print(a is b)   # False - two different list objects
print(a is c)   # True  - same object
Tip

Use is mainly with None: write if result is None:. For comparing values, always use ==.

7. Bitwise operators (overview)#

They work on the binary (0/1) form of integers. Used in networking, graphics and competitive programming.

Operator Name Example (5 = 101, 3 = 011) Result
& AND 5 & 3 1
| OR 5 | 3 7
^ XOR 5 ^ 3 6
~ NOT ~5 -6
<< left shift 5 << 1 10
>> right shift 5 >> 1 2

Operator precedence#

When an expression has several operators, Python follows this order (highest first):

Priority Operators
1 ( ) brackets
2 ** (right to left)
3 +x, -x, ~x (unary)
4 *, /, //, %
5 +, -
6 <<, >>, &, ^, |
7 ==, !=, >, <, >=, <=, is, in
8 not
9 and
10 or
▶ Try it YourselfPython
Ctrl + Enter to run
Tip

When in doubt, use brackets. (a + b) * c is clearer than relying on rules - and clear code has fewer bugs.

Summary#

  • Arithmetic: + - * / // % ** - / always gives float, // floors, % gives remainder.
  • Comparison operators return True/False; == compares, = assigns.
  • Logical: and, or, not (with short-circuiting).
  • Assignment shortcuts: +=, -=, *=, etc. No ++ in Python.
  • in checks membership, is checks identity (use with None).
  • Brackets first, then **, then * / // %, then + -, then comparisons, not, and, or.

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

Even or odd (using %)

Read a number and print True if it is even, otherwise False. Use the % operator and a comparison - no if needed.

Test input:
48
Expected output:
True
2

Minutes to hours

Read a number of minutes and print it as hours and minutes in this format: 135 minutes = 2 hours 15 minutes

Test input:
135
Expected output:
135 minutes = 2 hours 15 minutes
3

Sum of digits of a 3-digit number

Read a 3-digit number and print the sum of its digits, using only // and % (no strings). For 472 the answer is 13.

Test input:
472
Expected output:
13

Quick Quiz

Q1.

What is 17 // 5?

Q2.

What is 17 % 5?

Q3.

What is 2 ** 3 ** 2?

Q4.

What is not (5 > 3 and 2 > 4)?

Q5.

What does "py" in "python" return?

Q6.

What is 10 + 2 * 3?