Python Operators

Python Operators

May 6th, 2026
263
20:00 Minutes

Have you ever wanted to learn more about what actually happens when an operator is used in Python to perform calculations, comparisons or logical decisions? Operators are the primary tools through which all of these actions can be accomplished within Python programming languages.

I have been doing hands-on real-world coding with Python programming for over three years. During that time I have become very knowledgeable about how to use operators efficiently and cleanly in code. I have used logical operators in authentication systems where multiple conditions must be verified together. A common mistake beginners make is confusing == with is, which can lead to unexpected bugs

In this article, I will provide you with practical examples to assist you with gaining confidence in your ability to use Python operators in your own programming. Let’s start!

What are Python Operators?

Python operators are special symbols or keywords that are used for performing operations on variables and values. They are the building blocks of any Python program that can help you manipulate data easily.

For example:

a = 10
b = 5

print(a + b)  # Output: 15

python operators

Read Also: Python Tutorial for Beginners

Types of Operators in Python

Before writing effective Python code, it is important to understand the different types of operators available. Each type serves a specific purpose, which makes your code more efficient and readable. In this section, I will explain you the main types of operators in Python.

types of operators in python

1. Arithmetic Operators

Arithmetic operators are used when you have to perform basic mathematical operations like addition, subtraction, multiplication and division. You use them with numeric data types like integers and floats.

Operator Name Example Result
+ Addition 10 + 5 15
- Subtraction 10 - 5 5
* Multiplication 10 * 5 50
/ Division 10 / 3 3.33
% Modulus 10 % 3 1
** Exponentiation 10 ** 2 100
// Floor Division 10 // 3 3

For example:

# Arithmetic Operators with Large Numbers

a = 9
b = 12

print("Value of a:", a)
print("Value of b:", b)

print("\nAddition:", a + b)          
print("Subtraction:", a - b)       
print("Multiplication:", a * b)    
print("Division:", a / b)          
print("Modulus:", a % b)           
print("Exponentiation:", a ** 2)   # square of a
print("Floor Division:", a // b) 

arithmetic operators in python

2. Comparison Operators/ Relational Operators

Comparison or Relational operators compare values. It either returns True or False, which you need for conditional logic in if statements and while loops.

Operator Name Example Result
== Equal to 10 == 5 False
!= Not equal to 10 != 5 True
> Greater than 10 > 5 True
< Less than 10 < 5 False

For example:

a = 10
b = 5

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

comparison or relational operators in python

3. Logical Operators

Logical operators combine conditional statements and return True or False. You can use them to create complex decision making logic. The precedence of Logical Operators in Python is as follows:

  • Logical not
  • logical and
  • logical or

For example:

# Logical Operators Example

a = 10
b = 5
c = 0

# Logical NOT (highest precedence)
print("not(a > b):", not(a > b))   # False

# Logical AND (medium precedence)
print("(a > b) and (b > c):", (a > b) and (b > c))  # True

# Logical OR (lowest precedence)
print("(a < b) or (b > c):", (a < b) or (b > c))    # True

# Combined Example (showing precedence)
result = not(a < b) and (b > c) or (c > a)
print("Combined Result:", result)

logical operators in python

4. Bitwise Operators

Bitwise operators are used to perform operations on numbers at the binary (bit) level. Instead of working with whole numbers. They work on individual bits (0s and 1s) of a number.

For example:

# Bitwise Operators Example

a = 10  # 1010 in binary
b = 4   # 0100 in binary

print("a & b:", a & b)   # AND
print("a | b:", a | b)   # OR
print("a ^ b:", a ^ b)   # XOR
print("~a:", ~a)         # NOT
print("a << 1:", a << 1) # Left Shift
print("a >> 1:", a >> 1) # Right Shift

bitwise operators in python

5. Assignment Operators

Assignment operators are used to assign values to variables. They can also perform operations and assign the result in one step.

For example:

# Assignment Operators Example

x = 10
print("Initial value:", x)

x += 5   # x = x + 5
print("After += :", x)

x -= 3   # x = x - 3
print("After -= :", x)

x *= 2   # x = x * 2
print("After *= :", x)

x /= 4   # x = x / 4
print("After /= :", x)

x %= 3   # x = x % 3
print("After %= :", x)

x **= 2  # x = x ** 2
print("After **= :", x)

x //= 2  # x = x // 2
print("After //= :", x)

assignment operators in python

Read Also: Python Interview Questions and Answers

Operator Precedence and Associativity in Python

When you are writing a Python expression with multiple operators, Python needs to decide which operation to perform first. That’s where precedence and associativity come in.

What is Operator Precedence?

Operator precedence is the rule that determines operator priority in an expression. It decides which operation is performed first.

Example:

result = 10 + 5 * 2
print(result)  # Output: 20

operator precedence in python

Multiplication has higher precedence than addition, so it runs first.

What is Associativity?

Operator associativity defines the order in which operators of the same precedence are evaluated (left to right or right to left).

Example:

result = 10 - 5 - 2
print(result)  # Output: 3

operator associativity in python

Read Also: Final Keyword in Java

Special Python Operators

When you are learning Python, not every operator is about doing math. Some operators help you understand relationships between values, like checking if two things are actually the same object or if something exists inside a collection. These are called the special operators.

Here are the two special operators:

1. Identity Operators

Identity operators are used to check whether two variables point to the same object in memory and not just if their values are equal.

Operators:

  • is: Returns True if both variables refer to the same object
  • is not: Returns True if they do not refer to the same object

For example:

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

print(a is b)      # True (same object)
print(a is c)      # False (different objects)
print(a == c)      # True (values are same)

identity python operators

2. Membership Operators

Membership operators are used to check if a value exists inside a sequence like a list, string, tuple or dictionary.

Operators:

  • in: returns True if value exists
  • not in: Returns True if value does not exist

For example:

fruits = ["apple", "banana", "Kiwi"]

print("apple" in fruits)      # True
print("grapes" in fruits)     # False
print("banana" not in fruits) # False

membership python operators

Read Also: Static Keyword in Java

Practical Examples of Python Operators in Real Code

Operators in Python are not limited to theory, but instead serve as an important part of practical programming. Every program you develop to address a particular task may utilize one or more of the operators. Understanding their practical use cases to produce efficient, easy-to-read code to solve real-world issues is essential.

Here are some of them:

1. Shopping Cart

Arithmetic operators are used to perform mathematical calculations like totals and discounts, which helps in managing pricing, billing and financial computations efficiently in real-world applications.

For example:

price = 499
quantity = 3

total_cost = price * quantity
discount = 100

final_price = total_cost - discount

print("Final Price:", final_price)

use of arithmetic operators in shopping cart

2. Login System

Comparison operators verify user credentials by checking equality between entered and stored values, which ensures correct authentication and prevents unauthorized access.

For example:

entered_password = "admin123"
actual_password = "admin123"

if entered_password == actual_password:
    print("Login successful")
else:
    print("Wrong password")

comparison operators in login system

3. Eligibility Check

Logical and comparison operators combine multiple conditions like age and identity verification to decide eligibility. It is commonly used in validation and access control systems.

For example:

age = 20
has_id = True

if age >= 18 and has_id:
    print("Allowed to enter")
else:
    print("Not allowed")

use of logical and comparison operators in eligibility check

4. Score Update

Assignment operators update values efficiently, such as adding bonuses or subtracting penalties, which makes them useful in games, scoring systems and real time tracking.

For example:

score = 50

score += 10   # bonus points
score -= 5    # penalty

print("Final Score:", score)

assignment operators in score update

5. Search Feature

Membership operators check whether an item exists in a list or database, enabling fast search, filtering and validation in applications like e-commerce and data systems.

For example:

products = ["laptop", "mobile", "tablet"]

search_item = "mobile"

if search_item in products:
    print("Product available")
else:
    print("Not found")

use of membership operators in search feature

6. Object Comparison

Identity operators compare memory locations instead of values, which helps in determining if two variables refer to the same object, useful in optimization and debugging.

For example:

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

print(a is b)   # True (same object)
print(a is c)   # False (different objects)

identity operators in object comparison

7. Permissions System

Bitwise operators are used to perform operations on binary values, commonly for managing permissions or flags efficiently in system-level and performance-critical applications.

For example:

READ = 1   # 001
WRITE = 2  # 010
EXECUTE = 4 # 100

permission = READ | WRITE  # 011

if permission & WRITE:
    print("Write access granted")

use of bitwise operators in permissions system

Wrapping Up

Operators used in Python are fundamental tools for performing mathematical calculations, creating computer-based logic for making decisions, and manipulating and managing data. By better understanding the various categories of operators and how they work along with one another, programmers will produce better programming code that is more concise and clean.

Learning about the operators is fundamental to learning about programming and solving problems in a real-time fashion with increased accuracy and assurance.

FAQs on Python Operators

1. Why are Python operators important in programming?

These are essentials as they allow you to perform specific mathematical, logical and relational actions on variables and values

2. How do logical operators help in decision making?

Logical operators will help you combine multiple conditions and decide whether a block of code should run. They are mainly used in if statements to check complex conditions easily.

3. Are and == operators in Python?

Both are operators and they are used differently. and is a logical operator used to combine conditions, while == is a comparison operator used to check if two values are equal.

About the Author
Sanjay Prajapat
About the Author

Sanjay Prajapat is a Data Engineer and technology writer with expertise in Python, SQL, data visualization, and machine learning. He simplifies complex concepts into engaging content, helping beginners and professionals learn effectively while exploring emerging fields like AI, ML, and cybersecurity in today’s evolving tech landscape.

Drop Us a Query
Fields marked * are mandatory

Programming Certification Courses

×

Your Shopping Cart


Your shopping cart is empty.