Beginner

Operators in Python

Learn arithmetic, comparison, logical, and assignment operators in Python, plus operator precedence, with Docker-ready examples

Operators are the verbs of a programming language—they let you combine values into expressions that compute results, compare quantities, and make decisions. Python provides a rich, readable set of operators that mostly look like the math you already know, with a few pleasant surprises.

Because Python is a dynamically and strongly typed, multi-paradigm language, operators behave according to the types of their operands, decided at runtime. The same + that adds two integers concatenates two strings, and the language refuses to silently mix incompatible types (you can’t add a string to an integer). Under the hood, every operator maps to a special method (like __add__), which is why operators feel consistent across built-in types and can be extended to your own classes.

In this tutorial you’ll work through arithmetic, comparison, logical, identity, and membership operators, the augmented assignment shortcuts, and the precedence rules that decide what happens first. Each example is a small, runnable program you can execute with Docker.

Arithmetic Operators

Python’s arithmetic operators include two that often surprise newcomers: // for floor division (rounding toward negative infinity) and ** for exponentiation. Note that / always produces a float, even when the operands divide evenly.

Create a file named arithmetic.py:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# Arithmetic operators in Python
a = 17
b = 5

print("a + b  =", a + b)    # addition
print("a - b  =", a - b)    # subtraction
print("a * b  =", a * b)    # multiplication
print("a / b  =", a / b)    # true division (always a float)
print("a // b =", a // b)   # floor division
print("a % b  =", a % b)    # modulo (remainder)
print("a ** b =", a ** b)   # exponentiation

# Floor division and modulo work with floats too
print("7.5 // 2 =", 7.5 // 2)
print("7.5 % 2  =", 7.5 % 2)

Comparison, Logical, and Membership Operators

Comparison operators return a boolean (True/False). Python uniquely supports chained comparisons like 1 < x < 100, which read like mathematical notation. The logical operators are spelled out as words—and, or, not—rather than symbols. Python also offers in/not in for membership and is/is not for identity (whether two names refer to the same object).

Create a file named comparison_logical.py:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# Comparison operators return booleans
x = 10
y = 3

print("x == y :", x == y)
print("x != y :", x != y)
print("x >  y :", x > y)
print("x <  y :", x < y)
print("x >= y :", x >= y)
print("x <= y :", x <= y)

# Python allows chained comparisons
print("1 < x < 100 :", 1 < x < 100)

# Logical operators: and, or, not
logged_in = True
is_admin = False
print("logged_in and is_admin :", logged_in and is_admin)
print("logged_in or is_admin  :", logged_in or is_admin)
print("not logged_in          :", not logged_in)

# Membership and identity operators
nums = [1, 2, 3]
print("2 in nums      :", 2 in nums)
print("5 not in nums  :", 5 not in nums)
print("nums is nums   :", nums is nums)

Assignment and String Operators

Augmented assignment operators (+=, -=, *=, //=, **=, and so on) update a variable in place using its current value. The arithmetic operators also do double duty on strings: + concatenates and * repeats. Python 3.8 added the walrus operator :=, which assigns a value and returns it within a larger expression.

Create a file named assignment.py:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
# Augmented assignment operators
total = 100
total += 50      # total = total + 50
print("after += 50 :", total)
total -= 30
print("after -= 30 :", total)
total *= 2
print("after *= 2  :", total)
total //= 7
print("after //= 7 :", total)
total **= 2
print("after **= 2 :", total)

# Strings: + concatenates, * repeats
greeting = "Hi" + ", " + "Python"
print(greeting)
print("=" * 20)

# Walrus operator (:=) assigns within an expression (Python 3.8+)
if (n := len(greeting)) > 5:
    print("greeting has", n, "characters")

Operator Precedence

When an expression mixes operators, precedence decides the order of evaluation. Exponentiation (**) binds tightest and is right-associative, multiplication and division come next, then addition and subtraction, and comparison/logical operators are evaluated last. When in doubt, add parentheses—they make intent explicit and cost nothing.

Create a file named precedence.py:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Precedence: ** is tightest, then * / // %, then + -
print("2 + 3 * 4      =", 2 + 3 * 4)        # * before +
print("(2 + 3) * 4    =", (2 + 3) * 4)      # parentheses override
print("2 ** 3 ** 2    =", 2 ** 3 ** 2)      # ** is right-associative
print("10 - 4 - 3     =", 10 - 4 - 3)       # - is left-associative

# Comparison and logical operators rank below arithmetic
print("2 + 2 == 4 and 3 > 1 :", 2 + 2 == 4 and 3 > 1)

# Exponentiation outranks unary minus
print("-2 ** 2       =", -2 ** 2)

Running with Docker

You can run every example above without installing Python locally.

1
2
3
4
5
6
7
8
# Pull the official image
docker pull python:3.13-alpine

# Run each example
docker run --rm -v $(pwd):/app -w /app python:3.13-alpine python arithmetic.py
docker run --rm -v $(pwd):/app -w /app python:3.13-alpine python comparison_logical.py
docker run --rm -v $(pwd):/app -w /app python:3.13-alpine python assignment.py
docker run --rm -v $(pwd):/app -w /app python:3.13-alpine python precedence.py

Expected Output

Running arithmetic.py:

a + b  = 22
a - b  = 12
a * b  = 85
a / b  = 3.4
a // b = 3
a % b  = 2
a ** b = 1419857
7.5 // 2 = 3.0
7.5 % 2  = 1.5

Running comparison_logical.py:

x == y : False
x != y : True
x >  y : True
x <  y : False
x >= y : True
x <= y : False
1 < x < 100 : True
logged_in and is_admin : False
logged_in or is_admin  : True
not logged_in          : False
2 in nums      : True
5 not in nums  : True
nums is nums   : True

Running assignment.py:

after += 50 : 150
after -= 30 : 120
after *= 2  : 240
after //= 7 : 34
after **= 2 : 1156
Hi, Python
====================
greeting has 10 characters

Running precedence.py:

2 + 3 * 4      = 14
(2 + 3) * 4    = 20
2 ** 3 ** 2    = 512
10 - 4 - 3     = 3
2 + 2 == 4 and 3 > 1 : True
-2 ** 2       = -4

Key Concepts

  • / always returns a float, while // performs floor division and % gives the remainder—use them together to split a value into quotient and remainder.
  • ** is exponentiation and is right-associative, so 2 ** 3 ** 2 evaluates as 2 ** (3 ** 2) = 512.
  • Logical operators are words (and, or, not), not symbols, and they short-circuit: or stops at the first truthy operand, and at the first falsy one.
  • Chained comparisons like 1 < x < 100 work the way they do in math—no need to write 1 < x and x < 100.
  • + and * are overloaded by type: with numbers they do arithmetic, with strings they concatenate and repeat. Strong typing means Python won’t mix a string and a number without an explicit conversion.
  • is checks identity, == checks equality: is asks whether two names point to the same object, while == compares values.
  • Augmented assignment (+=, *=, etc.) is concise shorthand for updating a variable from its own current value.
  • Parentheses always win. When precedence is unclear, group expressions explicitly for readability rather than memorizing every rule.

Running Today

All examples can be run using Docker:

docker pull python:3.13-alpine
Last updated:

Comments

Loading comments...

Leave a Comment

2000 characters remaining