An Introduction to Python Operators
Ohidur Rahman Bappy
MAR 22, 2025
An Introduction to Python Operators
Operators are constructs used to manipulate the value of operands. For example, in the expression 4 + 5 = 9
, 4
and 5
are operands, and +
is the operator.
Types of Operators
Python supports the following types of operators:
- Arithmetic Operators
- Comparison (Relational) Operators
- Assignment Operators
- Logical Operators
- Bitwise Operators
- Membership Operators
- Identity Operators
Let's explore each in detail.
Python Arithmetic Operators
Assume variable a
holds the value 10
and variable b
holds 21
:
| Operator | Description | Example |
| --- | --- | --- |
| +
| Addition | a + b = 31
|
| -
| Subtraction | a - b = -11
|
| *
| Multiplication | a * b = 210
|
| /
| Division | b / a = 2.1
|
| %
| Modulus | b % a = 1
|
| **
| Exponent | a**b = 10 to the power of 20
|
| //
| Floor Division | 9 // 2 = 4
, -11 // 3 = -4
|
Python Comparison Operators
Comparison operators assess the relationship between values. Assume a = 10
and b = 20
:
| Operator | Description | Example |
| --- | --- | --- |
| ==
| Equals | (a == b)
is False |
| !=
| Not Equal | (a != b)
is True |
| >
| Greater Than | (a > b)
is False |
| <
| Less Than | (a < b)
is True |
| >=
| Greater or Equal | (a >= b)
is False |
| <=
| Less or Equal | (a <= b)
is True |
Python Assignment Operators
Assume a = 10
and b = 20
:
| Operator | Description | Example |
| --- | --- | --- |
| =
| Assign | c = a + b
|
| +=
| Add and Assign | c += a
|
| -=
| Subtract and Assign | c -= a
|
| *=
| Multiply and Assign | c *= a
|
| /=
| Divide and Assign | c /= a
|
| %=
| Modulus and Assign | c %= a
|
| **=
| Exponent and Assign | c **= a
|
| //=
| Floor Divide and Assign | c //= a
|
Python Bitwise Operators
These operators perform operations at the bit level. Assume a = 60
and b = 13
:
a & b = 12
a | b = 61
a ^ b = 49
~a = -61
a << 2 = 240
a >> 2 = 15
Python Logical Operators
Assume a = True
and b = False
:
| Operator | Description | Example |
| --- | --- | --- |
| and
| Logical AND | (a and b)
is False |
| or
| Logical OR | (a or b)
is True |
| not
| Logical NOT | not (a and b)
is True |
Python Membership Operators
These check for membership in sequences like strings, lists, or tuples:
| Operator | Description | Example |
| --- | --- | --- |
| in
| Exists in sequence | x in y
|
| not in
| Does not exist in sequence | x not in y
|
Python Identity Operators
Memory location comparison:
| Operator | Description | Example |
| --- | --- | --- |
| is
| Same object | x is y
|
| is not
| Different object | x is not y
|
Python Operators Precedence
Operator precedence determines evaluation order:
- Exponentiation
- Unary plus and minus
- Multiplication, division, modulus, floor division
- Addition and subtraction
- Bitwise shifts
- Bitwise AND
- Bitwise XOR and OR
- Comparison
- Equality
- Assignment
- Identity
- Membership
- Logical
Understanding these will help in constructing expressions correctly and using Python operators effectively.