A Deep Dive into Python Operators
Operators are the special symbols in Python that carry out arithmetic, logical, and
other types of computation. The value that the operator works on is called the
operand. This guide provides a comprehensive overview of every major operator
category in Python.
Arithmetic Operators
These are the most familiar operators, used to perform standard mathematical
calculations.
Operator Name Description Example (a=13, Result
b=5)
+ Addition Adds two a+b 18
operands
together.
- Subtraction Subtracts the a-b 8
right operand
from the left.
* Multiplication Multiplies two a*b 65
operands.
/ Division Divides the left a/b 2.6
operand by the
right one.
Always results
in a float.
// Floor Division Performs a // b 2
division and
returns only the
integer part of
the quotient,
discarding the
remainder.
% Modulus Returns the a%b 3
remainder after
division.
** Exponent Raises the left a ** b 371293
operand to the
power of the
right operand.
Assignment Operators
Assignment operators are used to assign, or re-assign, a value to a variable. The basic
assignment operator is =, but Python provides several compound operators that
combine an arithmetic or bitwise operation with assignment.
Operator Example Equivalent to
= x = 10 x = 10
+= x += 5 x=x+5
-= x -= 5 x=x-5
*= x *= 5 x=x*5
/= x /= 5 x=x/5
//= x //= 5 x = x // 5
%= x %= 5 x=x%5
**= x **= 5 x = x ** 5
&= x &= 3 x=x&3
` =` `x
^= x ^= 3 x=x^3
>>= x >>= 2 x = x >> 2
<<= x <<= 2 x = x << 2
Comparison Operators
Comparison operators are used to compare two values. The expression always
evaluates to a Boolean value: True or False. They are the foundation of
decision-making in programs.
Operator Description Example (a=10, Result
b=10, c=5)
== Equal to: Checks if a == b True
the values of two
operands are equal.
!= Not equal to: Checks a != c True
if the values of two
operands are not
equal.
> Greater than: a>c True
Checks if the left
operand's value is
greater than the
right's.
< Less than: Checks if c<a True
the left operand's
value is less than the
right's.
>= Greater than or a >= b True
equal to: Checks if
the left operand's
value is greater than
or equal to the
right's.
<= Less than or equal c <= a True
to: Checks if the left
operand's value is
less than or equal to
the right's.
Logical Operators
Logical operators are used to combine or modify conditional statements (expressions
that evaluate to True or False).
Operator Description Example (x=True, Result
y=False)
and Logical AND: x and y False
Returns True only if
both operands are
true.
or Logical OR: Returns x or y True
True if at least one of
the operands is true.
not Logical NOT: not y True
Reverses the logical
state of its operand.
Identity Operators
Identity operators (is, is not) are fundamentally different from comparison operators
(==, !=). They do not compare the values of the objects; they compare their identity.
An object's identity is its unique memory address.
Operator Description Example
is Returns True if two variables x is y
point to the exact same
object in memory.
is not Returns True if two variables x is not y
point to different objects in
memory.
Key Example:
# list_a and list_b have identical values but are separate objects.
list_a = [1, 2, 3]
list_b = [1, 2, 3]
# list_c is a reference to the same object as list_a.
list_c = list_a
print(list_a == list_b) # True (values are the same)
print(list_a is list_b) # False (they are different objects in memory)
print(list_a == list_c) # True (values are the same)
print(list_a is list_c) # True (they are the exact same object)
Note: For simple types like small integers and strings, Python may reuse objects in
memory for efficiency, which can sometimes make is behave like ==. However, you
should not rely on this behavior. Use is for checking object identity (is None) and ==
for checking value equality.
Membership Operators
Membership operators (in, not in) are used to test whether a value or variable is found
within a sequence (like a string, list, tuple, or dictionary).
Operator Description Example Result
in Returns True if a 2 in [1, 2, 3] True
value is found in the
sequence.
not in Returns True if a 'a' not in 'hello' True
value is not found in
the sequence.
Bitwise Operators
Bitwise operators act on operands as if they were strings of binary digits. They
perform operations bit by bit. These are typically used in lower-level programming,
such as working with device drivers, network packets, or binary file formats.
Let a = 60 (binary 0011 1100) and b = 13 (binary 0000 1101).
Operator Name Description Example Result Binary
& Bitwise AND Sets each bit a&b 12 0000 1100
to 1 only if
both
correspondi
ng bits are 1.
` ` Bitwise OR Sets each bit `a b`
to 1 if at
least one of
the
correspondi
ng bits is 1.
^ Bitwise XOR Sets each bit a^b 49 0011 0001
to 1 only if
the
correspondi
ng bits are
different.
~ Bitwise NOT Inverts all ~a -61 1100 0011
the bits (0
becomes 1,
and 1
becomes 0).
The result is
the two's
complement
of the
number.
<< Left Shift Shifts the a << 2 240 1111 0000
bits of the
left operand
to the left by
the number
of places
specified by
the right
operand.
>> Right Shift Shifts the a >> 2 15 0000 1111
bits of the
left operand
to the right
by the
number of
places
specified by
the right
operand.