Python Operators

Python operators are symbols and keywords that perform operations on values, such as adding numbers, comparing values or combining conditions. Python groups its operators into a few families, and this chapter walks through each one.

Arithmetic Operators

Use + - * / for the basic operations, // for floor division, % for the remainder (modulus) and ** for powers.

a, b = 17, 5

print(a + b, a - b, a * b)
print(a / b)
print(a // b, a % b)
print(2 ** 10)
22 12 85
3.4
3 2
1024

Assignment Operators

Besides =, Python has shortcut operators that update a variable in place: x += 3 means x = x + 3. The same pattern works with -=, *=, /=, //=, %= and **=.

balance = 100
balance += 50
balance -= 30
balance *= 2
print(balance)

# The walrus operator assigns inside an expression
if (n := len("python")) > 5:
    print("Long word with", n, "letters")
240
Long word with 6 letters

Comparison Operators

Comparison operators return True or False: == equal, != not equal, > greater than, < less than, >= and <=. Python also lets you chain comparisons the way you would in maths.

age = 25

print(age == 25, age != 30)
print(18 <= age < 65)
True True
True

Python Logical Operators

and is True only when both sides are True. or is True when at least one side is True. not flips a boolean. Python stops evaluating as soon as the answer is known (short-circuiting).

has_ticket = True
is_vip = False

print(has_ticket and is_vip)
print(has_ticket or is_vip)
print(not is_vip)

name = ""
print(name or "Guest")
False
True
True
Guest

The last line shows a common trick: or returns the first truthy value, so name or "Guest" gives a default when name is empty.

Identity and Membership Operators

is checks whether two names point to the very same object, while == checks whether the values are equal. in and not in check whether a value is inside a sequence or collection.

a = [1, 2, 3]
b = [1, 2, 3]
c = a

print(a == b)
print(a is b)
print(a is c)

print(2 in a)
print("z" not in "python")
True
False
True
True
True

Operator Precedence

When an expression has several operators, Python follows a fixed order. From highest to lowest, the main levels are:

  • () parentheses
  • ** exponent
  • unary + and - (for example -x)
  • * / // %
  • + -
  • comparisons, is, in
  • not, then and, then or

For example 2 + 3 * 4 is 14, not 20, because multiplication happens first. When in doubt, add parentheses; they make the intent obvious to readers too.

Frequently asked questions

What is the difference between is and == in Python?

== compares values, while is checks whether both sides are the exact same object in memory. Use is mainly for comparing with None, True or False.

Does Python have ++ and -- operators?

No. Use x += 1 and x -= 1 instead. Writing ++x is valid but does nothing, because it is just two unary plus signs.

What does % do in Python?

With numbers, % returns the remainder of a division, so 10 % 3 is 1. It is often used to check whether a number is even with n % 2 == 0.

What is the walrus operator in Python?

The := operator, added in Python 3.8, assigns a value to a variable as part of a larger expression, such as inside an if or while condition.