# Python 3: Operators

By Xah Lee. Date: . Last updated: .

## Arithmetic Operators

Arithmetic
purposesyntaxfunction
addition`a + b``add(a, b)`
Subtraction`a - b``sub(a, b)`
Negation`- a``neg(a)`
Multiplication`a * b``mul(a, b)`
Division`a / b``truediv(a, b)`
Division`a // b``floordiv(a, b)`
Modulo`a % b``mod(a, b)`
Exponentiation`a ** b``pow(a, b)`

The “function” column is the function from the “operator” module. You can use it after you `import operator`. example:

```# -*- coding: utf-8 -*-
# Python 3

import operator

## Assignment Operators

```# -*- coding: utf-8 -*-
# Python 3

c = 0
c += 1
print(c)    # 1

# substract and assign
c = 0
c -= 2
print(c)    # -2

# multiply and assign
c = 2
c *= 3
print(c)    # 6

# exponent and assign
c = 3
c **= 2
print(c)    # 9

# divide and assign
c = 7
c /= 2
print(c)    # 3.5

# modulus (remainder) and assign
c = 13
c %= 5
print(c)    # 3

# quotient and assign
c = 13
c //= 5
print(c)    # 2```

Note: Python doesn't support `++` or `--`.

Warning: `++i` may not generate any error, but it doesn't do anything.

## Bit Operators

Bit Operators
purposesyntaxfunction
and`a & b``and_(a, b)`
or`a | b``or_(a, b)`
xor`a ^ b``xor(a, b)`
invert`~ a``invert(a)`
Right Shift`a >> b``rshift(a, b)`
Left Shift`a << b``lshift(a, b)`
```# -*- coding: utf-8 -*-
# Python 3

# 0b101 would mean binary 101
# the "{:2b}" means print in binary, and use 2 slots.

# bitwise And
print( "{:2b}".format(0b11 & 0b10) )   # binary 10

# bitwise Or
print( "{:2b}".format(0b11 | 0b10) )   # binary 11

# xor. true if both sides differ
print( "{:2b}".format(0b00 ^ 0b10) )   # binary 10

# binary invert
print( "{:2b}".format(~ 0b1) )   # binary -10  (odd result!)```

## Sequence Operators

Sequence Operators
purposesyntaxfunction
get item`obj[k]``getitem(obj, k)`
assign item`obj[k] = v``setitem(obj, k, v)`
delete item`del obj[k]``delitem(obj, k)`
join`seq1 + seq2``concat(seq1, seq2)`
if exist`obj in seq``contains(seq, obj)`
format string`s % obj``mod(s, obj)`

## Boolean Operators

Boolean Operators
purposesyntaxfunction
to boolean`obj``truth(obj)`
not`not a``not_(a)`
equal`a == b``eq(a, b)`
not equal`a != b``ne(a, b)`
less than`a < b``lt(a, b)`
less equal`a <= b``le(a, b)`
greater`a > b``gt(a, b)`
greater equal`a >= b``ge(a, b)`
same object`a is b``is_(a, b)`
not same object`a is not b``is_not(a, b)`

If you have a question, put \$5 at patreon and message me.