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.
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#
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) # power22 12 85
3.4
3
2
289% 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:
x = 10
x += 5 # 15
x *= 2 # 30
x -= 4 # 26
x //= 3 # 8
print(x)8Every arithmetic operator has an assignment version: +=, -=, *=, /=, //=, %=, **=. Python has no ++ or --; write x += 1.
Comparison operators#
Comparisons produce True or False:
print(5 == 5, 5 != 3, 7 > 9, 3 <= 3)
print("apple" < "banana", "Zebra" < "apple")True True False True
True TrueStrings 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:
age = 25
print(18 <= age <= 30) # TrueLogical operators#
and, or and not combine conditions:
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) # reversesTrue
True
Falseand 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:
print(0 or "default", "" or "empty", 5 and 10, 0 and 10)default empty 10 0That is why name = user_input or "Guest" is a common Python idiom for default values.
Membership: in and not in#
print("py" in "python", 3 in [1, 2, 3], "x" not in "abc")True True Truein 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?":
a = [1, 2]
b = [1, 2]
c = a
print(a == b, a is b, a is c)True False Truea 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:
print(2 + 3 * 4, (2 + 3) * 4, 2 ** 3 ** 2, -2 ** 2)14 20 512 -4From highest to lowest priority (simplified):
**(evaluated right to left, so2 ** 3 ** 2is2 ** 9)- unary
-(so-2 ** 2is-(2 ** 2)) *,/,//,%+,-- comparisons (
==,<,in,is, …) not, thenand, thenor
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
seconds = 7384
hours = seconds // 3600
minutes = (seconds % 3600) // 60
secs = seconds % 60
print(f"{hours}h {minutes}m {secs}s")2h 3m 4s7384 // 3600 = 2 hours; 7384 % 3600 = 184 seconds left; 184 // 60 = 3 minutes; 7384 % 60 = 4 seconds.
Key takeaway#
Finished this lesson?