Python Operators

Last updated: 2026-09-22

Python Operators

A category-grouped reference of Python operators: arithmetic, assignment, comparison, membership & identity, logical, and bitwise. Each entry comes with a syntax glance and a runnable example.

Arithmetic Operators

OperatorMeaningExample
+Addition / concatenation — + sums numbers and concatenates sequences.a + b
-Subtraction / set difference — - subtracts numbers and computes set difference.a - b
*Multiplication / repetition — * multiplies numbers and repeats sequences.a * b
/True division — / always returns float; use // for integer division.a / b
//Floor division — // returns the floor of division.a // b
%Modulo — % returns the remainder of division.a % b
**Power — ** computes a raised to the power b.a ** b
@Matrix multiply — @ is the matrix-multiplication operator (PEP 465); used by NumPy etc.a @ b

Comparison Operators

OperatorMeaningExample
==Equal — == tests equality (delegated to __eq__).a == b
!=Not equal — != is the negation of ==.a != b
<Less than — < tests a < b (via __lt__).a < b
>Greater than — > tests a > b (via __gt__).a > b
<=Less or equal — <= tests a <= b (via __le__).a <= b
>=Greater or equal — >= tests a >= b (via __ge__).a >= b

Assignment Operators

OperatorMeaningExample
=Simple assignment — = binds the value of the right-hand expression to the name on the left.name = expression
+=Addition assignment — += adds the right value to the left variable, equivalent to x = x + y; for a list it appen...x += y
-=Subtraction assignment — -= subtracts the right value from the left variable, equivalent to x = x - y.x -= y
*=Multiplication assignment — *= multiplies the left variable by the right value, equivalent to x = x * y; for str/list ...x *= y
/=Division assignment — /= divides the left variable by the right value, returning a float, equivalent to x = x / ...x /= y
//=Floor division assignment — //= floor-divides the left variable by the right value (rounding down), equivalent to x = ...x //= y
%=Modulo assignment — %= takes the modulo of the left variable by the right value, equivalent to x = x % y.x %= y
**=Power assignment — **= raises the left variable to the power of the right value, equivalent to x = x ** y.x **= y
@=Matrix multiplication assignment — @= performs an in-place matrix multiplication of the left variable by the right, equivalen...x @= y
&=Bitwise AND assignment — &= computes a bitwise AND of the left variable with the right value, equivalent to x = x &...x &= y
|=Bitwise OR assignment — |= computes a bitwise OR of the left variable with the right value, equivalent to x = x | ...x |= y
^=Bitwise XOR assignment — ^= computes a bitwise XOR of the left variable with the right value, equivalent to x = x ^...x ^= y
<<=Left shift assignment — <<= shifts the bits of the left variable left by the amount of the right value, equivalent...x <<= y
>>=Right shift assignment — >>= shifts the bits of the left variable right by the amount of the right value, equivalen...x >>= y
:=Walrus operator — := (PEP 572) assigns a value as part of an expression and returns it.name := value

Logical Operators

OperatorMeaningExample
andBoolean AND (short-circuit) — and is short-circuit boolean AND; returns first falsy or last truthy.a and b
orBoolean OR (short-circuit) — or is short-circuit boolean OR; returns first truthy or last falsy.a or b
notBoolean NOT — not returns the boolean negation (True / False).not a

Bitwise Operators

OperatorMeaningExample
&Bitwise AND / set intersection — & is bitwise AND; on sets it's intersection.a & b
|Bitwise OR / set union — | is bitwise OR; on sets it's union.a | b
^Bitwise XOR / set symmetric difference — ^ is bitwise XOR; on sets it's symmetric difference.a ^ b
~Bitwise NOT — ~ inverts all bits: ~x == -(x+1).~a
<<Left shift — << shifts bits left by n; equivalent to a * (2**n).a << n
>>Right shift — >> shifts bits right by n; equivalent to floor(a / (2**n)).a >> n

Membership Operators

OperatorMeaningExample
inMembership test — in checks containment; also drives for-loops.x in container
not inNegated membership — not in is the negation of in.x not in container

Identity Operators

OperatorMeaningExample
isIdentity test — is tests identity (same object in memory).a is b
is notNegated identity — is not is the negation of is.a is not b

Operator Precedence

OperatorDescription
(...), [...], {k: v}, {...}Parentheses / list / dict / set
x[i], x[i:j], x(args), x.attrSubscription, slice, call, attribute
await xawait expression
**Exponentiation
+x, -x, ~xUnary plus, minus, bitwise NOT
*, @, /, //, %Multiply, matmul, divide, floor-divide, modulo
+, -Addition, subtraction
<<, >>Shift left, shift right
&Bitwise AND
^Bitwise XOR
|Bitwise OR
in, not in, is, is not, <, <=, >, >=, !=, ==Comparisons, membership, identity
not xLogical NOT
andLogical AND
orLogical OR
if ... elseConditional expression
lambdalambda expression
:=Assignment expression (walrus)
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏