Python Operators

Calculate, compare, assign, and combine values with operators.

Operators are one of the most important parts of programming. They perform calculations, compare values, support decisions, and manipulate data. Whether you are adding numbers, checking voting eligibility, or combining conditions, operators make these tasks possible.

Python operators are straightforward to learn and use. Once you understand them, you can write more useful and interactive programs.

In this lesson, you will learn about the different types of operators in Python, how they work, and where they are commonly used.

What is an Operator?

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

python
a = 10
b = 5

print(a + b)

Output

text
15

Here, + tells Python to add the values assigned to a and b.

Types of Operators in Python

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Logical operators
  • Identity operators
  • Membership operators
  • Bitwise operators

Arithmetic Operators

Arithmetic operators perform mathematical calculations.

OperatorDescriptionExample
+Addition5 + 3
-Subtraction5 - 3
*Multiplication5 * 3
/Division10 / 2
//Floor division10 // 3
%Modulus or remainder10 % 3
**Exponent or power2 ** 3
python
a = 10
b = 3

print(a + b)
print(a - b)
print(a * b)
print(a / b)
print(a // b)
print(a % b)
print(a ** b)

Output

text
13
7
30
3.3333333333333335
3
1
1000

Assignment Operators

Assignment operators store values in variables. The equals sign performs basic assignment.

python
x = 10

print(x)

Augmented assignment operators update a variable using its current value, making code shorter and often easier to read.

python
x = 10

x += 5
print(x)

x -= 3
print(x)

x *= 2
print(x)

Output

text
15
12
24

Comparison Operators

Comparison operators compare two values and return True or False. They are commonly used in conditional statements.

OperatorMeaning
==Equal to
!=Not equal to
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to
python
age = 18

print(age == 18)
print(age > 20)
print(age <= 18)

Output

text
True
False
True

Logical Operators

Logical operators combine or reverse conditions. Python provides and, or, and not.

python
age = 22
has_license = True

print(age >= 18 and has_license)
print(age < 18 or has_license)
print(not has_license)

Output

text
True
True
False

Use and when every condition must be true, or when at least one condition must be true, and not to reverse a truth value.

Identity Operators

Identity operators check whether two references point to the same object in memory. Python provides is and is not. Identity is different from value equality.

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

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

Output

text
True
False

a and c contain equal values, but they are distinct list objects. Use == when comparing values and is primarily for identity checks such as value is None.

Membership Operators

Membership operators check whether a value exists in a sequence or collection. Python provides in and not in.

python
fruits = ["Apple", "Banana", "Orange"]

print("Apple" in fruits)
print("Mango" in fruits)

Output

text
True
False

Membership operators are useful when searching strings, lists, tuples, sets, dictionaries, and other containers.

Bitwise Operators

Bitwise operators work with the binary representations of integers. They are mainly used in low-level programming, flags, permissions, protocols, and other advanced applications.

OperatorOperation
&Bitwise AND
|Bitwise OR
^Bitwise XOR
~Bitwise NOT
<<Left shift
>>Right shift
python
a = 5
b = 3

print(a & b)
print(a | b)

Bitwise operators are generally not required in beginner programs, but it is useful to recognize them.

Operator Precedence

When an expression contains multiple operators, Python follows an order of evaluation. Multiplication has higher precedence than addition.

python
result = 5 + 3 * 2

print(result)

Output

text
11

Parentheses can change or clarify the evaluation order.

python
result = (5 + 3) * 2

print(result)

Output

text
16

Use parentheses when they make a calculation's intention clearer.

Key Takeaways

  • Operators perform actions on values and variables.
  • Arithmetic operators perform mathematical calculations.
  • Assignment operators store and update variable values.
  • Comparison operators return True or False after comparing values.
  • Logical operators combine or reverse conditions.
  • Identity operators check whether references point to the same object.
  • Membership operators check whether a value exists in a collection.
  • Bitwise operators work with binary integer representations and are mostly used in advanced programming.
  • Python follows operator precedence, but parentheses can change or clarify evaluation order.
  • Operators are essential because they appear in almost every Python program.
Let's learn with DevBrainBox AI