Operators in Python

Understand the various operators in Python and how to use them in expressions and calculations.

30 min read
Beginner

Introduction to Operators

Operators are characters or symbols that are used to perform various operations in Python.

For example, the '+' operator in Mathematics is used to add two values together and the same can be done in Python.

python
4 + 6

There are several kinds of operators in Python and we will discuss about them in detail in this lesson:

  1. Arithmetic Operators (+, -, /, *, //, %)🥷Ninja Jump
  2. Boolean Operators (and, or, not)🥷Ninja Jump
  3. Bitwise Operators (>>, <<, &, |)🥷Ninja Jump
  4. Assignment Operators (=, +=, -=, *=, /=)🥷Ninja Jump

Let's go over each of them one by one.

Arithmetic Operators

Arithmetic operators are used to perform basic arithmetic operations in Python.

  • Addition (+): Used for adding two numbers.
python
10 + 68

We can also perform operations on variables and get the same result.

python
x = 10
y = 68

x + y
  • Subtraction (-): Used for subtracting two numbers.
python
10 - 10
  • Multiplication (*): Used for multiplying two numbers.
python
10 * 10
  • Division (/): Used for dividing one number by another.
python
10 / 2
  • Modulus (%): Used for finding the remainder after division.
python
10 % 10
python
11 % 10
  • Floor division (//): Used for rounding the result of division to the nearest whole number.
python
19 // 10
python
20 // 10

Operator Precedence

When multiple operators are used in a single expression, Python follows a specific order of operations known as operator precedence to determine which operation to perform first.

The order of precedence for arithmetic operators in Python is as follows (from highest to lowest):

  1. Parentheses ()
  2. Exponentiation **
  3. Multiplication *, Division /, Floor Division //, Modulus %
  4. Addition +, Subtraction -

Operators with the same precedence level are evaluated from left to right.

Example: In the expression 3 + 5 * 2, multiplication has a higher precedence than addition, so the multiplication is performed first, resulting in 3 + 10, which equals 13.

Now that you have started to get a sense of how arithmetic operators work, let's see if you can answer this:

To change the order of operations, you can use parentheses. For instance, in the expression (3 + 5) * 2, the addition inside the parentheses is performed first, resulting in 8 * 2, which equals 16.

Boolean Operators

Boolean operators are used to check conditions between two variables and return either 'True' or 'False'. There are two kinds of boolean operators:

  • a. Comparison Operators
  • b. Logical Operators

a. Comparison Operators

Comparison operators are used to compare two values in Python. Let us go over some commonly used comparison operators.

  • equal to (==)
python
4 == 2
python
4 == 4
  • not equal to (!=)
python
4 != 2
python
4 != 4
  • greater than (>)
python
4 > 2
python
2  > 4
  • less than (<)
python
2 < 4
python
4 < 2
  • greater than or equal to (>=)
python
4 >= 2
python
2 >= 4
python
4 => 4
  • less than or equal to (<=)
python
2 <= 4
python
4 <= 2
python
4 <= 4

b. Logical Operators

Logical operators are used to chain multiple boolean operations together. The three most used Logical operators are:

  • AND logical operator
  • OR logical operator
  • NOT logical operator

'AND' logical operator

It is used to find an overlapping relationship between two boolean values. The 'AND' operator results in True if both the evaluated conditions are True, otherwise it results in False.

AND Operator
X
Y
X and Y
FalseFalseFalse
FalseTrueFalse
TrueFalseFalse
TrueTrueTrue

Let's look at an example to understand this better:

python
x = 4 < 2 # False
y = 4 > 2 # True

# False and True results in False
print(x and y)

This is the same as writing:

python
# False and True results in False
4 < 2 and 4 > 2

'OR' Operator

It is used to find a non-overlapping relationship between two boolean values. The 'OR' operator results in True if any of the conditions are True, otherwise it results in False.

OR Operator
X
Y
X or Y
FalseFalseFalse
FalseTrueTrue
TrueFalseTrue
TrueTrueTrue

Let's look at an example:

python
x = 4 < 2 # False
y = 4 > 2 # True

# False or True results in True
print(x or y)

'NOT' Operator It is used to find the negation of a boolean value. The 'NOT' operation results in True if the boolean value is False and vice-versa.

NOT Operator
X
Not X
TrueFalse
FalseTrue
python
x = 4 < 2 # False
print(not x)

Bitwise Operators

Bitwise operators work directly on the binary representation of numbers.

Instead of comparing whole numbers, they compare individual bits (0s and 1s) at each position.

These operators are not commonly used in everyday programming, so it’s normal if they feel unintuitive at first. The goal here is to understand how the calculation happens, not to memorize results.

Let’s start by defining two numbers.

python
x = 13
bin(x)
python
y = 10
bin(y)

So in binary form:

  • x = 13 → 1101
  • y = 10 → 1010

Each position represents a power of 2.

Binary Representation
Decimal
8
4
2
1
x = 131101
y = 101010

Bitwise AND (&)

The AND (&) operator returns 1 only if both bits are 1.

Rule:

  • 1 & 1 → 1
  • Anything else → 0
AND Operation
Bit Position
8
4
2
1
x (1101)1101
y (1010)1010
x & y1000

The result is 1000 in binary, which equals 8 in decimal.

python
x & y

Bitwise OR (|)

The OR (|) operator returns 1 if either bit is 1.

Rule:

  • 0 | 0 → 0
  • Anything else → 1
OR Operation
Bit Position
8
4
2
1
x (1101)1101
y (1010)1010
xy1111

The result is 1111, which equals 15 in decimal.

python
x | y

Bitwise XOR (^)

The XOR (^) operator returns 1 when the bits are different.

Rule:

  • 1 ^ 0 → 1
  • 0 ^ 1 → 1
  • Same bits → 0
XOR Operation
Bit Position
8
4
2
1
x (1101)1101
y (1010)1010
x ^ y0111

The result is 0111, which equals 7 in decimal.

python
x ^ y

Bitwise NOT (~)

The NOT (~) operator flips every bit:

  • 1 becomes 0
  • 0 becomes 1

Python uses two’s complement to represent negative numbers, which is why the result may look unfamiliar.

NOT Operation on x
Bit Position
8
4
2
1
x (1101)1101
~x0010

The flipped bits represent -14 in Python’s signed integer system.

python
~x

Left Shift (<<)

The left shift (<<) operator moves bits to the left and always fills with zeros on the right.

Each left shift multiplies the number by 2.

Left Shift Example
Operation
Binary
Decimal
x110113
x << 11101026
python
# Implies x = 1101 in binary
x = bin(13)
# Bits shifts to left by 1 and fills with 0 to right: 11010
x << 1
# Decimal value of 11010 is the result

Right Shift (>>)

The right shift (>>) operator moves bits to the right and rightmost bit is dropped.

Each right shift divides the number by 2 and discards the remainder.

Right Shift Example
Operation
Binary
Decimal
y110113
y >> 101106
python
# Implies y = 1101 in binary
y = bin(13)
# Bit shifts to right by 1, the rightmost bit (1) is dropped: 0110
y >> 1
# Decimal value of 0110 is the result

Assignment Operators

Assignment operators are used to perform assignment operation on a variable while performing arithmetic operations.

Example: To compute an operation given as x = x + 1 we can simply write: x += 1

python
x = 2
x += 1
print(x)
python
x = 2
x -= 1
print(x)
python
x = 2
x *= 10
print(x)
python
x = 2
x /= 10
print(x)
python
x = 2
x %= 10
print(x)
python
x = 2
x //= 2
print(x)

Test your knowledge

🧠 Knowledge Check
1 / 15

What is an operator in Python?