Skip to content
Laviyx Learn

Try

Python course menu

Python Operators Explained: Arithmetic, Comparison, Logical and More

Learn every common Python operator with examples: arithmetic (/, //, %, **), assignment (+=), comparison and chained comparison, logical and/or/not, membership (in), identity (is), and operator precedence.

Beginner6 min readUpdated
On this page

An operator is a symbol that performs an operation on values: + adds, > compares, and combines conditions. Python's operators fall into a few groups, and knowing them well makes everything else in the course easier.

Arithmetic operators#

Python
a, b = 17, 5
print(a + b, a - b, a * b)
print(a / b)     # division
print(a // b)    # floor division
print(a % b)     # remainder (modulo)
print(a ** 2)    # power
Output
22 12 85
3.4
3
2
289
OperatorMeaningExampleResult
+ - *add, subtract, multiply17 * 585
/division (always a float)17 / 53.4
//floor division (rounds down)17 // 53
%remainder17 % 52
**power17 ** 2289

% is surprisingly useful: n % 2 == 0 tests whether a number is even, and n % 10 gives its last digit.

Assignment operators#

Combine an operation with assignment:

Python
x = 10
x += 5    # 15
x *= 2    # 30
x -= 4    # 26
x //= 3   # 8
print(x)
Output
8

Every arithmetic operator has an assignment version: +=, -=, *=, /=, //=, %=, **=. Python has no ++ or --; write x += 1.

Comparison operators#

Comparisons produce True or False:

Python
print(5 == 5, 5 != 3, 7 > 9, 3 <= 3)
print("apple" < "banana", "Zebra" < "apple")
Output
True True False True
True True

Strings compare alphabetically, character by character, and all uppercase letters come before lowercase ones, which is why "Zebra" < "apple".

Python lets you chain comparisons, just like in maths:

Python
age = 25
print(18 <= age <= 30)   # True

Logical operators#

and, or and not combine conditions:

Python
age = 25
has_id = True
print(age >= 18 and has_id)   # both must be True
print(age < 18 or has_id)     # at least one must be True
print(not has_id)             # reverses
Output
True
True
False

and and or short-circuit: they stop as soon as the answer is known, and they return one of the actual values, not necessarily True/False:

Python
print(0 or "default", "" or "empty", 5 and 10, 0 and 10)
Output
default empty 10 0

That is why name = user_input or "Guest" is a common Python idiom for default values.

Membership: in and not in#

Python
print("py" in "python", 3 in [1, 2, 3], "x" not in "abc")
Output
True True True

in works with strings, lists, tuples, sets and dictionary keys.

Identity: is and is not#

== asks "are the values equal?"; is asks "are these the very same object?":

Python
a = [1, 2]
b = [1, 2]
c = a
print(a == b, a is b, a is c)
Output
True False True

a and b are two separate lists with equal contents. c is another name for the same list as a. Use is only to compare with None (if result is None:); use == for everything else.

Operator precedence#

When several operators appear together, Python follows an order, like the maths rule of multiplication before addition:

Python
print(2 + 3 * 4, (2 + 3) * 4, 2 ** 3 ** 2, -2 ** 2)
Output
14 20 512 -4

From highest to lowest priority (simplified):

  1. ** (evaluated right to left, so 2 ** 3 ** 2 is 2 ** 9)
  2. unary - (so -2 ** 2 is -(2 ** 2))
  3. *, /, //, %
  4. +, -
  5. comparisons (==, <, in, is, …)
  6. not, then and, then or

When in doubt, add parentheses. They make your intention obvious.

Common mistakes#

Practice#

Practice exercise

A video is 7384 seconds long. Using // and %, print its length as 2h 3m 4s.

Show hint

There are 3600 seconds in an hour. After removing whole hours with %, divide what is left by 60 for minutes.

Show solution
Python
seconds = 7384
hours = seconds // 3600
minutes = (seconds % 3600) // 60
secs = seconds % 60

print(f"{hours}h {minutes}m {secs}s")
Output
2h 3m 4s

7384 // 3600 = 2 hours; 7384 % 3600 = 184 seconds left; 184 // 60 = 3 minutes; 7384 % 60 = 4 seconds.

Key takeaway#

Finished this lesson?