Python Operators: Arithmetic to Assignment and Beyond

Operators and Operands

An operator is a symbol that performs an action on one or more values. In the expression 7 + 3, the numbers 7 and 3 are operands, while + is the operator.

Arithmetic Operators

Symbol Meaning Example
+ addition 12 + 7 == 19
- subtraction / negasion 12 - 7 == 5
* multiplication / repetition 3 * 4 == 12
/ true division 7 / 2 == 3.5
// floor division 7 // 2 == 3
% modulo (remainder) 7 % 2 == 1
** power 2 ** 3 == 8
x, y = 14, 3
print("add:", x + y)        # 17
print("sub:", x - y)        # 11
print("mul:", x * y)        # 42
print("truediv:", x / y)    # 4.666...
print("floordiv:", x // y)  # 4
print("mod:", x % y)        # 2
print("pow:", x ** y)       # 2744

Comparison (Relational) Operators

Symbol Meaning Example
== equal 5 == 5True
!= not equal 5 != 3True
< less than 3 < 5True
> greater than 5 > 3True
<= less or equal 3 <= 3True
>= greater or equal 5 >= 5True
a, b = 21, 10
tests = [
    a == b,
    a != b,
    a < b,
    a > b,
    a <= b,
    b >= a
]
for res in tests:
    print(res)
# False True False True False True

Assignment and Compound Assignment

Symbol Expanded form
= c = a + b
+= c = c + a
-= c = c - a
*= c = c * a
/= c = c / a
//= c = c // a
%= c = c % a
**= c = c ** a
:= Walrus operator (Py 3.8+)
total = 10
total += 5      # 15
total *= 2      # 30
total //= 3     # 10
total **= 2     # 100

# Walrus operator
if (n := len("hello")) > 3:
    print(n)    # 5

Bitwise Operators

Bitwise operators treat integers as binary strings.

Symbol Meaning Example
& AND 0b1100 & 0b1010 == 0b1000
| OR 0b1100 | 0b1010 == 0b1110
^ XOR 0b1100 ^ 0b1010 == 0b0110
~ NOT ~0b1100 == -0b1101
<< left shift 0b0010 << 2 == 0b1000
>> right shift 0b1000 >> 2 == 0b0010
a, b = 0b111100, 0b001101
print("AND:", bin(a & b))
print("OR :", bin(a | b))
print("XOR:", bin(a ^ b))
print("NOT:", bin(~a & 0xFF))  # mask to 8 bits
print("SHL:", bin(a << 2))
print("SHR:", bin(a >> 2))

Logical Operators

Operator Result
x and y first falsy value or y
x or y first truthy value or y
not x True if x is falsy else False
flag1, flag2 = True, False
print(flag1 and flag2)  # False
print(flag1 or flag2)   # True
print(not flag1)        # False

Membership Operators

  • in3 in [1, 2, 3]True
  • not in4 not in [1, 2, 3]True
nums = {2, 4, 6}
print(4 in nums)      # True
print(3 not in nums)  # True

Identity Operators

  • is — same object identity
  • is not — different object identity
lst1 = [1, 2]
lst2 = lst1
lst3 = lst1.copy()
print(lst1 is lst2)   # True
print(lst1 is lst3)   # False
print(lst1 == lst3)   # True (value equality)

Operator Precedence (highest → lowest)

  1. () [] {} — grouping, subscripts, dict/set literals
  2. ** — exponentiation (right-to-left)
  3. +x -x ~x — unary plus/minus, bitwise NOT
  4. * / // % — multiplication, division, modulo
  5. + - — addiiton, subtraction
  6. << >> — bit shifts
  7. & — bitwice AND
  8. ^ — bitwise XOR
  9. | — bitwise OR
  10. in not in is is not < <= > >= != == — comparisons & membership
  11. not — logical NOT
  12. and — logical AND
  13. or — logical OR
  14. if-else — conditional expression
  15. lambda — lambda expression
  16. := — assignment expression
result = 2 + 3 * 4 ** 2  # 2 + 3 * 16 == 50
print(result)

print(True or False and False)  # True

Tags: python Operators arithmetic bitwise comparison

Posted on Sun, 21 Jun 2026 16:52:13 +0000 by andr923