Skip to content

Python Basic Operators

Operators are special symbols or keywords in Python that perform operations on values. These values are called operands.

For example, in the expression 4 + 5, the + is the operator, and 4 and 5 are the operands. The expression evaluates to 9.

Python supports several types of operators:

  • Arithmetic Operators
  • Comparison (Relational) Operators
  • Assignment Operators
  • Logical Operators
  • Bitwise Operators
  • Membership Operators
  • Identity Operators

Let’s explore each type.

These operators perform standard mathematical calculations. Assume a = 10 and b = 20.

OperatorDescriptionExample (a=10, b=20)
+ AdditionAdds the values of the operands.a + b results in 30
- SubtractionSubtracts the right operand from the left operand.a - b results in -10
* MultiplicationMultiplies the values of the operands.a * b results in 200
/ DivisionDivides the left operand by the right operand. Always results in a float.b / a results in 2.0
% ModulusReturns the remainder of the division.b % a results in 0. 13 % a results in 3.
** ExponentiationRaises the left operand to the power of the right operand.a ** 2 results in 100 (10 to the power of 2)
// Floor DivisionDivides and rounds the result down to the nearest whole number (integer).9 // 2 results in 4. 9.0 // 2.0 results in 4.0.

These operators compare two values and return a Boolean result (True or False). They are also known as Relational Operators. Assume a = 10 and b = 20.

OperatorDescriptionExample (a=10, b=20)
== Equal toReturns True if both operands are equal.a == b is False. a == 10 is True.
!= Not equal toReturns True if operands are not equal.a != b is True. a != 10 is False.
> Greater thanReturns True if the left operand is greater than the right.a > b is False.
< Less thanReturns True if the left operand is less than the right.a < b is True.
>= Greater than or equal toReturns True if the left operand is greater than or equal to the right.a >= b is False. a >= 10 is True.
<= Less than or equal toReturns True if the left operand is less than or equal to the right.a <= b is True. a <= 10 is True.

Note: The <> operator is deprecated in Python 3; use != instead.

These operators assign values to variables, often combining an arithmetic operation with assignment. Assume a = 10, b = 20, and c = 0 initially.

OperatorDescriptionExample (a=10)
= AssignAssigns the value of the right operand to the left operand.c = a + b assigns 30 to c.
+= Add ANDAdds the right operand to the left operand and assigns the result back to the left operand.c += a is equivalent to c = c + a. If c was 0, c becomes 10.
-= Subtract ANDSubtracts the right operand from the left and assigns the result to the left operand.c -= a is equivalent to c = c - a.
*= Multiply ANDMultiplies the left operand by the right and assigns the result to the left operand.c *= a is equivalent to c = c * a.
/= Divide ANDDivides the left operand by the right and assigns the result (float) to the left operand.c /= a is equivalent to c = c / a.
%= Modulus ANDTakes the modulus and assigns the result to the left operand.c %= a is equivalent to c = c % a.
**= Exponent ANDRaises the left operand to the power of the right and assigns the result to the left operand.c **= a is equivalent to c = c ** a.
//= Floor Divide ANDPerforms floor division and assigns the result to the left operand.c //= a is equivalent to c = c // a.

Bitwise operators act on operands as sequences of binary digits (bits). They are often used for low-level manipulation or optimization. Assume a = 60 and b = 13.

Binary representations:

`a = 60 -> 0011 1100`
`b = 13 -> 0000 1101`
OperatorDescriptionExample (a=60, b=13)
& Bitwise ANDSets each bit to 1 if both corresponding bits are 1.a & b results in 12 (0000 1100)
| Bitwise ORSets each bit to 1 if at least one of the corresponding bits is 1.a | b results in 61 (0011 1101)
^ Bitwise XORSets each bit to 1 if only one of the corresponding bits is 1.a ^ b results in 49 (0011 0001)
~ Bitwise NOT (Complement)Inverts all the bits (0 becomes 1, 1 becomes 0). Applied to one operand. Result depends on the number representation (e.g., two’s complement for signed integers).~a results in -61. The result is -(a + 1) for positive integers in typical two’s complement systems.
<< Left ShiftShifts the bits of the left operand to the left by the number of positions specified by the right operand. Fills with zeros from the right.a << 2 results in 240 (1111 0000)
>> Right ShiftShifts the bits of the left operand to the right by the number of positions specified by the right operand. Fills with the sign bit from the left (arithmetic shift) or zeros (logical shift - Python does arithmetic).a >> 2 results in 15 (0000 1111)

These operators combine conditional statements. Assume x = True and y = False.

OperatorDescriptionExample (x=True, y=False)
and Logical ANDReturns True if both operands are true.x and y is False.
or Logical ORReturns True if at least one operand is true.x or y is True.
not Logical NOTReturns True if the operand is false, and False if the operand is true.not x is False. not y is True.

These operators test if a sequence (like string, list, tuple, set, dictionary keys) contains a specific item. Assume my_list = [1, 2, 3].

OperatorDescriptionExample (my_list=[1, 2, 3])
inReturns True if the value/variable is found in the sequence.2 in my_list is True.
not inReturns True if the value/variable is not found in the sequence.4 not in my_list is True.

These operators compare the memory locations of two objects, checking if they refer to the exact same object instance. Assume x = [1, 2], y = [1, 2], z = x.

OperatorDescriptionExample (x=[1,2], y=[1,2], z=x)
isReturns True if both variables point to the same object in memory.x is z is True (z references the same list as x). x is y is False (x and y are different list objects, even if their contents are identical).
is notReturns True if both variables point to different objects in memory.x is not y is True. x is not z is False.

Note: Use is for identity comparison (same object), and == for equality comparison (same value).

When an expression contains multiple operators, Python evaluates them in a specific order based on precedence. Higher precedence operators are evaluated first. Parentheses () can be used to override the default precedence.

The following table lists operators from highest precedence (top) to lowest (bottom):

OperatorDescription
()Parentheses (grouping)
f(args...), x[index], x[index:slice], x.attributeFunction calls, subscripting, slicing, attribute reference
await xAwait expression
**Exponentiation (right-associative)
+x, -x, ~xPositive, negative, bitwise NOT (unary)
*, /, //, %Multiplication, division, floor division, modulus
+, -Addition and subtraction
<<, >>Bitwise left and right shifts
&Bitwise AND
^Bitwise XOR
|Bitwise OR
in, not in, is, is not, <, <=, >, >=, !=, ==Comparisons, including membership and identity tests
not xLogical NOT
andLogical AND
orLogical OR
if elseConditional expression
lambdaLambda expression
:=Assignment expression (Walrus operator)

Understanding operator precedence helps in writing predictable and correct code.