Python Operators Overview

What Are Python Operators?

Python operators are special syntactical sugar to run basic operations without calling their respective methods. For example, you can use the + operator in a + b instead of the more clunky .add() method in a.add(b). Each operator has a unique symbol that is placed between the two arguments called operands.

A simple example is given next where the + operator is applied to operands 40 and 2 to generate the sum 42:

>>> 40 + 2
42

Comparison Operators

Comparison operators are applied to comparable objects and they return a Boolean value (True or False).

OperatorNameDescriptionExample
>Greater ThanReturns True if the left operand is greater than the right operand3 > 2 == True
<Less ThanReturns True if the left operand is smaller than the right operand3 < 2 == False
==Equal ToReturns True if the left operand is the same as the right operand(3 == 2) == False
!=Not Equal ToReturns True if the left operand is not the same as the right operand(3 != 2) == True
>=Greater Than or Equal ToReturns True if the left operand is greater than or equal to the right operand(3 >= 3) == True
<=Less Than or Equal ToReturns True if the left operand is less than or equal to the right operand(3 <= 2) == False

You can check out a full guide on all of those comparison operators in the following blog tutorial on the Finxter blog.

Related Tutorial: Python Comparison Operators

Arithmetic Operators

Arithmetic operators are syntactical shortcuts to perform basic mathematical operations on numbers.

OperatorNameDescriptionExample
+AdditionCalculating the sum of the two operands3 + 4 == 7
SubtractionSubtracting the second operand from the first operand4 - 3 == 1
*MultiplicationMultiplying the first with the second operand3 * 4 == 12
/DivisionDividing the first by the second operand3 / 4 == 0.75
%ModuloCalculating the remainder when dividing the first by the second operand7 % 4 == 3
//Integer Division, Floor DivisionDividing the first operand by the second operand and rounding the result down to the next integer8 // 3 == 2
**ExponentRaising the first operand to the power of the second operand2 ** 3 == 8

Logical Operators

Logical operators are shortcuts to perform Boolean comparisons, but can be used on integers and other objects as well.

OperatorDescriptionExample
andReturns True if both operands are True, and False otherwise.(True and True) == True
orReturns True if at least one of the two operands is True, and False otherwise.(False or True) == True
notReturns True if the single operand is False, and False otherwise.(not True) == False

You can read the full article with video on Python logical operators on the Finxter blog.

Related Tutorial: Python Logical Operators

Bitwise Operators

Bitwise operators perform operations on the binary (bit) representation of integers.

Background: Each integer is first written as a binary number that is a sequence of digits 0 or 1. For example:

  • 0 is written as “0”
  • 1 is written as “1”
  • 2 is written as “10”
  • 3 is written as “11”
  • 4 is written as “100”
  • 5 is written as “101”

The bitwise operators are then applied on those binary representations. The resulting binary number is then converted back to the integer representation (decimal number).

In the following table, we exemplify all bit-wise operators with variables x and y defined as follows:

>>> x = 4  # 100
>>> y = 5  # 101

As a comment, we also provide the binary representation 100 for the decimal integer 4, and 101 for the decimal integer 5.

OperatorNameDescriptionExample
x = 4, y = 5
&Bitwise ANDPerforms logical AND on a bit-by-bit basisx & y
# b100 & b101 == b100 == 4
|Bitwise ORPerforms logical OR operation on a bit-by-bit basisx | y
# b100 | b101 == b101 == 5
~Bitwise NOTPerforms logical NOT on a bit-by-bit basis, inverting each bit so that 0 becomes 1 and 1 becomes 0. Same as -x-1.~x
# -4-1 == -5
^Bitwise XORPerforms logical “exclusive or” operation on a bit-by-bit basisx ^ y
# b100 ^ b101 == b001 == 1
>>Bitwise right shiftShifts binary of left operand to the right by the number of positions specified in right operandx >> 2
# b100 == b010 == b001 == 1
<<Bitwise left shiftShifts binary of left operand to the left by the number of positions specified in right operandx << 2
# b100 == b1000 == b10000 == 16

You can read the full article with video on Python bitwise operators on the Finxter blog.

Related Tutorial: Python Bitwise Operators

In-Place Assignment Operators

In-place assignment operators (also called compound assignment operators) perform an operation in-place on a variable provided as first operand. They overwrite the value of the first operand variable with the result of the operation when performing the operator without assignment. For example, x += 3 is the same as x = x + 3 of first calculating the result of x +3 and then assigning it to the variable x.

OperatorNameShort ExampleEquivalent Long Example
=In-place Assignmentx = 3
+=In-place Additionx += 3x = x + 3
-=In-place Subtractionx -= 3x = x - 3
*=In-place Multiplicationx *= 3x = x * 3
/=In-place Divisionx /= 3x = x / 3
%=In-place Modulox %= 3x = x % 3
//=In-place Integer Divisionx //= 3x = x // 3
**=In-place Powerx **= 3x = x ** 3
&=In-place Bitwise Andx &= 3x = x & 3
|=In-place Bitwise Orx |= 3x = x | 3
^=In-place Bitwise XORx ^= 3x = x ^ 3
>>=In-place Bitwise Shift Rightx >>= 3x = x >> 3
<<=In-place Bitwise Shift Leftx <<= 5x = x << 5

Identity Operators

Python has two identity operators: is and is not. They are used to check if two values or variables reside at the same memory location, i.e., refer to the same object in memory. However, if two variables are equal, this doesn’t imply that they are identical. For example, two lists [1, 2, 3] and [1, 2, 3] may be different objects (not identical) but they’re equal in value.

OperatorMeaningExample
isReturns True if both operands refer to the same object in memory and False otherwise.x is True
is notReturns False if both operands refer to the same object in memory and True otherwise.x is not True

Membership Operators

Python has two membership operators in and not in that test whether a value or variable exists in a collection such as string, list, tuple, set, and dictionary.

OperatorMeaningExample
inTrue if value/variable is found in the sequence5 in x
not inTrue if value/variable is not found in the sequence5 not in x

Python Operators Precedence

The following table describes Python’s operator precedence relationship, from highest to lowest precedence.

Operators in the same row have the same precedence. For example, comparisons, membership, and identity have the same precedence. In case of a conflict, they have a left-to-right precedence resolution scheme. In other words, the left operator has precedence over the right operator if both have the same theoretical precedence level.

NameOperators
Parentheses()
Exponentiation**
Bitwise Not~
Multiplication, Division, Modulo, Integer Division* / % //
Addition, Subtraction+ -
Bitwise Shift Right and Left>> <<
Bitwise AND&
Bitwise XOR and OR^ |
Comparison<= < > >=
Equality<> == !=
In-Place Assignments= %= /= //= -= += *= **=
Identity, Membershipis, is not, in, not in
Logicalnot, and, or
Table: Python operator precedence from high to low.

Related Article: Operator Precedence [Blog + Video]