Sitemap

Operators in Python

8 min readOct 6, 2026

--

Press enter or click to view image in full size

Operators are an important part of Python because they allow us to perform different operations on values.

Python provides operators for performing calculations, assigning values, comparing values, combining conditions, checking membership, comparing object identity, and working with binary representations.

An operator is a symbol or keyword that tells Python to perform a particular operation on one or more values.

For example:

a = 10
b = 5

print(a + b)

Output:

15

Here, + is an operator that performs addition.

Python provides several types of operators, including arithmetic, assignment, comparison, logical, membership, identity, and bitwise operators.

Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations.

The main arithmetic operators in Python are:

  • + — Addition
  • - — Subtraction
  • * — Multiplication
  • / — Division
  • % — Modulus
  • // — Floor Division
  • ** — Exponentiation

Addition

The + operator adds two values.

a = 8
b = 6

print(a + b)

Output:

14

Subtraction

The - operator subtracts one value from another.

a = 15
b = 7

print(a - b)

Output:

8

Multiplication

The * operator multiplies two values.

a = 4
b = 7

print(a * b)

Output:

28

Division

The / operator performs division.

a = 21
b = 4

print(a / b)

Output:

5.25

Division using / produces a floating-point result.

Modulus

The % operator returns the remainder after division.

a = 21
b = 4

print(a % b)

Output:

1

Because 21 divided by 4 leaves a remainder of 1.

Floor Division

The // operator performs floor division.

a = 21
b = 4

print(a // b)

Output:

5

Floor division returns the floor of the division result.

Exponentiation

The ** operator is used to raise a number to a power.

a = 2
b = 5

print(a ** b)

Output:

32

This is equivalent to:

2 × 2 × 2 × 2 × 2 = 32

Assignment Operators

Assignment operators are used to assign values to variables.

The most common assignment operator is:

=

For example:

number = 10

Here, the value 10 is assigned to number.

Python also provides operators that combine an arithmetic operation with assignment.

+= : The += operator adds a value to the existing value of a variable and assigns the result back to that variable.

number = 10
number += 5

print(number)

Output:

15

This is equivalent to:

number = number + 5

-= : The -= operator subtracts a value from the existing value and assigns the result back to the variable.

number = 10
number -= 3

print(number)

Output:

7

This is equivalent to:

number = number - 3

These operators allow a value to be updated without writing the variable name again in the longer form.

Comparison Operators

Comparison operators are used to compare two values.

The result of a comparison is a Boolean value: True or False.

Python provides the following comparison operators:

  • == — Equal to
  • != — Not equal to
  • > — Greater than
  • < — Less than
  • >= — Greater than or equal to
  • <= — Less than or equal to

For example:

a = 10
b = 5

print(a == b)
print(a != b)
print(a > b)
print(a < b)
print(a >= b)
print(a <= b)

Output:

False
True
True
False
True
False

Comparison operators are useful when a program needs to determine whether a particular condition is true or false.

Logical Operators

Logical operators are used to combine or modify conditions.

Python provides three logical operators:

  • and
  • or
  • not

and

The and operator returns True when both conditions are true.

age = 25
has_id = True

print(age > 18 and has_id)

Output:

True

The basic behaviour of and is:

True and True   → True
True and False → False
False and True → False
False and False → False

or

The or operator returns True when at least one condition is true.

age = 16
has_permission = True

print(age >= 18 or has_permission)

Output:

True

Its basic behaviour is:

True or True   → True
True or False → True
False or True → True
False or False → False

not

The not operator reverses a Boolean value.

print(not True)
print(not False)

Output:

False
True

These operators are useful when working with multiple conditions in a program.

Membership Operators

Membership operators are used to check whether a value exists within a sequence or collection.

Python provides two membership operators:

  • in
  • not in

For example, membership can be checked within a string:

text = "python"

print("p" in text)
print("z" in text)

Output:

True
False

The in operator checks whether the specified value is present.

The not in operator checks whether the specified value is not present.

text = "python"

print("z" not in text)

Output:

True

Membership operators can also be used with collections such as lists.

For example:

numbers = [10, 20, 30, 40]

print(20 in numbers)
print(50 not in numbers)

Output:

True
True

Identity Operators

Identity operators are used to check whether two variables refer to the same object.

Python provides:

  • is
  • is not

For example:

a = [1, 2, 3]
b = a
c = [1, 2, 3]

print(a is b)
print(a is c)

Output:

True
False

Here, b refers to the same object as a, so:

a is b

returns True.

Get Mrunal Shrikumar’s stories in your inbox

Join Medium for free to get updates from this writer.

Although c contains the same values as a, it is a different object. Therefore:

a is c

returns False.

Identity vs Equality

Identity is different from equality.

The == operator checks whether two values are equal:

a == c

This returns:

True

because both contain the same values.

The is operator, on the other hand, checks whether two variables refer to the same object:

a is c

This returns:

False

because a and c refer to different objects.

Bitwise Operators

Bitwise operators work with the binary representation of numbers.

Python provides several bitwise operators:

  • & — Bitwise AND
  • | — Bitwise OR
  • ^ — Bitwise XOR
  • ~ — Bitwise NOT
  • << — Left shift
  • >> — Right shift

To understand bitwise operators, it is useful to first understand how numbers can be represented in binary.

Binary Representation

Binary is a number system that uses only two digits:

0 and 1

For example, the decimal number 5 can be represented in binary as:

101

The decimal number 3 can be represented as:

11

Python provides the bin() function to display the binary representation of an integer.

number = 10

print(bin(number))

Output:

0b1010

The prefix 0b indicates that the value is represented in binary.

Binary representation is particularly useful when understanding bitwise operations because these operators work on individual bits.

Bitwise AND (&)

The & operator performs AND between corresponding binary bits.

The basic behaviour of AND is:

0 & 0 → 0
0 & 1 → 0
1 & 0 → 0
1 & 1 → 1

For example:

a = 18
b = 3

print(a & b)

Output:

2

The operation is performed on the binary representation of the two numbers.

Bitwise OR ( | )

The | operator performs a bitwise OR operation between corresponding bits.

The basic behaviour of OR is:

0 | 0 → 0
0 | 1 → 1
1 | 0 → 1
1 | 1 → 1

For example:

a = 5
b = 3

print(a | b)

Output:

7

Bitwise XOR (^)

The ^ operator performs a bitwise XOR operation.

XOR produces 1 when the two corresponding bits are different and 0 when they are the same.

Its basic behaviour is:

0 ^ 0 → 0
0 ^ 1 → 1
1 ^ 0 → 1
1 ^ 1 → 0

For example:

a = 5
b = 3

print(a ^ b)

Output:

6

Bitwise NOT (~)

The ~ operator performs a bitwise NOT operation.

For example:

print(~3)

Output:

-4

Similarly:

print(~6)

Output:

-7

The result may initially seem surprising because bitwise NOT operation works on the binary representation of an integer.

Left Shift (<<)

The left-shift operator moves the bits of a number to the left.

For example:

a = 5

print(a << 1)

Output:

10

Another example:

a = 35

print(a << 3)

Output:

280

Right Shift (>>)

The right-shift operator moves the bits of a number to the right.

For example:

a = 10

print(a >> 1)

Output:

5

Another example:

a = 280

print(a >> 3)

Output:

35

Left and right shift operations are performed on the binary representation of integers.

Operator Precedence

When an expression contains more than one operator, Python needs to determine which operation should be performed first.

This is known as operator precedence.

For example:

result = 2 + 3 * 4
print(result)

Output:

14

The multiplication is performed before the addition.

Parentheses can be used when a specific operation needs to be performed first:

result = (2 + 3) * 4
print(result)

Output:

20

Here, the expression inside the parentheses is evaluated first.

This is similar to the mathematical order of operations often remembered as BODMAS.

Using parentheses also makes expressions easier to understand and reduces ambiguity.

For example:

result = (10 + 5) + (8 / 2)

print(result)

The expressions inside the parentheses are evaluated first.

The + Operator with Strings

The behaviour of an operator can depend on the data type of the values being used.

The + operator can also be used to combine strings.

first_name = "Riya"
last_name = "Sharma"

print(first_name + last_name)

Output:

RiyaSharma

This operation is called string concatenation.

However, a string and an integer cannot be directly added together.

name = "Riya"
age = 25
print(name + age)

This produces a TypeError because Python does not automatically convert the integer into a string for this operation.

The value can instead be converted explicitly:

print(name + str(age))

Output:

Riya25

This is one example of why understanding data types and type conversion is important when working with operators.

A Practical Example

Several operators can be combined in a single program.

marks = 78
passing_marks = 40

print(marks >= passing_marks)
print(marks + 2)
print(marks % 10)
print(marks > 50 and marks < 100)

Output:

True
80
8
True

Here:

  • >= compares the marks with the passing marks.
  • + performs addition.
  • % finds the remainder.
  • and combines two conditions.

This shows how different operators can be used together to work with values and conditions.

Key Takeaways

  • Arithmetic operators perform mathematical calculations.
  • Assignment operators assign and update values.
  • Comparison operators compare values and return Boolean results.
  • Logical operators combine or modify conditions.
  • Membership operators check whether a value exists in a sequence or collection.
  • Identity operators check whether two variables refer to the same object.
  • Bitwise operators work with the binary representation of integers.
  • bin() can be used to view the binary representation of an integer.
  • Shift operators move bits to the left or right.
  • Operator precedence determines the order in which operations are evaluated.
  • The behaviour of an operator can depend on the data types involved.
  • Strings can be combined using the + operator, while values of incompatible types need to be converted before they can be combined.

--

--