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:
| |
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:
| |
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:
| |
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:
| |
Running with Docker
You can run every example above without installing Python locally.
| |
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, so2 ** 3 ** 2evaluates as2 ** (3 ** 2)= 512.- Logical operators are words (
and,or,not), not symbols, and they short-circuit:orstops at the first truthy operand,andat the first falsy one. - Chained comparisons like
1 < x < 100work the way they do in math—no need to write1 < 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.ischecks identity,==checks equality:isasks 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
Comments
Loading comments...
Leave a Comment