10 min read

Learn Python Operators with Example | List of Operators in Python L

Python is popular because it is easy to understand and use. A key reason for this is its wide range of Python operators, special symbols, or keywords that simplify common programming tasks. Whether basic math, value comparisons, or data manipulation, these operators make coding more intuitive and accessible.
 

In short, all operators in Python allow you to perform a variety of operations quickly and efficiently. They help you write clean, concise code, making Python even more powerful. With these operators, you can easily tackle complex problems, enhancing your overall coding experience.

 

What is the Python Operator?

 

Python operators serve as instruments that facilitate data manipulation, including numerical values and variables. The Python programming language offers a diverse array of operators, each tailored for distinct tasks.
 

Understanding these operators' use is essential for writing clean and efficient code. They allow you to perform everything from basic math to comparing values and solving complex problems. By mastering these operators, you can write effective code. In addition, it is simple and easy to read. This way, you can tackle any coding challenge with ease.

 

Different Types of Python Operators

 

In this tutorial, we’ll look at all the different types of operators. Here’s a quick breakdown:
 

  • Arithmetic Operators – These are used for basic math, like addition and subtraction.
  • Comparison Operators – These let you compare values to check if they’re equal or different.
  • Assignment Operators – These assign values to your variables.
  • Logical Operators – These help you make decisions based on conditions.
  • Bitwise Operators – These work with data at the bit level for advanced tasks.
  • Special Operators – These are unique to Python and perform specific actions.

Let’s get started and explore each one in detail. 
 

1. Arithmetic Operators in Python
 

In Python, operators are key for performing math in your code. Arithmetic operators are symbols that help you do basic calculations with numbers. Here are the most common ones:
 

  • Addition (+)
  • Subtraction (-)
  • Multiplication (*)
  • Division (/)
  • Modulus (%)

These Python operators make math easy, helping you get the results you need quickly. From the below table, you’ll able to understand the concept more quickly. 

 

Operator 

Description 

Syntax

+ (Addition)

Adds two numbers.

For example, x + y

– (Subtraction)

Subtracts one number from another. 

x – y

* (Multiplication)

Multiplies two numbers. 

For example, x * y

/ (Division – float)

Divides one number by another and gives a decimal result.

x / y

// (Floor Division)

These Python Operators divide one number by another and round down to the nearest whole number. 

For example, x // y

% (Modulus)

Gives the remainder when one number is divided by another. 

For example, x % y

** (Power)

Raises the first number to the power of the second. 

x ** y

These operators are simple yet powerful, making math in Python quick and easy. With just a few symbols, you can handle all sorts of calculations.

Example – 
 

# Variables

a = 15

b = 4

# Addition

print("Addition:", a + b)

# Subtraction

print("Subtraction:", a – b) 

# Multiplication

print("Multiplication:", a * b)

# Division

print("Division:", a / b) 

# Floor Division

print("Floor Division:", a // b)

# Modulus

print("Modulus:", a % b) 

# Exponentiation

print("Exponentiation:", a ** b)

 

2. Comparison Operators

 

Comparison operators in Python help you compare values and return either True or False. They are essential for making decisions in your code. Let’s go through the most common ones:
 

  1. Equal to (==)
     

The equal to operator checks if two values are the same. If they are, it returns True; otherwise, it returns False. For example:

a = 5

b = 5

result = a == b

print(result) # Output: True
 

  1. Not Equal to (!=)
     

The not equal to operator checks if two values are different. If they are, it returns True; if not, it returns False. For example:

a = 5

b = 3

result = a != b

print(result) # Output: True
 

  1. Greater than (>)
     

The greater than operator checks if the left value is larger than the right. If it is, it returns True; otherwise, it returns False. For example:

a = 5

b = 3

result = a > b

print(result) # Output: True
 

  1. Less than (<)
     

The less-than Python operator checks if the left value is smaller than the right. If it is, it returns True; if not, it returns False. For instance:

a = 5

b = 3

result = a < b

print(result) # Output: False
 

  1. Greater than or Equal to (>=)
     

The greater than or equal to operator checks if the left value is greater than or equal to the right. If true, it returns True; if not, it returns False. For example:

a = 5

b = 5

result = a >= b

print(result) # Output: True
 

  1. Less than or Equal to (<=)
     

Finally, the less than or equal to operator checks if the left value is smaller or equal to the right value. If true, it returns True; otherwise, it returns False. For example:

a = 5

b = 3

result = a <= b

print(result) # Output: False

These comparison operators are essential for comparing the values of your program. 
 

3. Assignment Operators
 

In Python, assignment operators help you assign or update values in variables. Let’s explore the most commonly used ones:
 

  1. Simple Assignment (=)
     

This operator assigns a value to a variable.
 

For example: a = 5
 

  1. Addition Assignment (+=)
     

With this operator, you add a number to a variable and update it.

For example:

a = 5

a += 3

print(a) # Output: 8
 

  1. Subtraction Assignment (-=)

Similarly, this operator subtracts a number from a variable and updates the value.

For example:

a = 5

a -= 3

print(a) # Output: 2
 

  1. Multiplication Assignment (*=)

This operator multiplies the variable by a number and updates it.

For example:

a = 5

a *= 3

print(a) # Output: 15
 

  1. Division Assignment (/=)
     

Next, this operator divides the variable by a number and updates it.

For example:
 

a = 10

a /= 2

print(a) # Output: 5.0
 

  1. Modulus Assignment (%=)
     

This operator calculates the remainder when dividing the variable by a number and updates it.

For example:
 

a = 10

a %= 3

print(a) # Output: 1
 

  1. Exponentiation Assignment (=)**
     

The exponentiation assignment operator raises the variable to the power of a number and updates the value.

For example:
 

a = 2

a **= 3

print(a) # Output: 8
 

  1. Floor Division Assignment (//=)
     

Finally, this operator divides the variable by a number and updates it with the largest whole number.

For example:
 

a = 10

a //= 3

print(a) # Output: 3

These operators make it easier to update variable values in Python. Moreover, they help you write concise, efficient code while keeping things clear and simple.

 

4. Logical Operators in Python

 

Python Logical Operators allow you to combine multiple conditions and make complex decisions in your code. Python and operators include AND, OR, and NOT, which help evaluate several conditions together.

Here’s an overview of each logical operator:
 

  • Logical AND: This operator checks if both conditions are true. It returns True only if both are true; otherwise, it returns False. In other words, “Do both conditions need to be true to proceed?”

For example:
 

a = True

b = True

result = a and b

print(result) # Output: True
 

  • Logical OR: The OR operator checks if at least one of the conditions is true. If either condition is true, it returns True; if both are false, it returns False. It’s like asking, “Is one condition enough to proceed?”

For example:
 

a = False

b = True

result = a or b

print(result) # Output: True
 

  • Logical NOT: The NOT operator reverses the result of a condition. If the condition is True, it turns it to False; if it’s False, it turns it to `True.” It simply flips the value.

For example:
 

a = True

result = not a

print(result) # Output: False

 

5. Bitwise Operators in Python

 

Python Bitwise Operators let you work directly with binary numbers. They perform operations on bits, which are the most basic units of data. These operators are useful when you need to optimize performance or interact with hardware.

Here’s an overview of the main bitwise operators in Python:
 

  • Bitwise NOT: This operator flips every bit in a number. If the bit is 1, it changes to 0. If it’s 0, it turns into 1. It’s like flipping a switch for each bit.
     

For example:
 

a = 5 # 0101 in binary

result = ~a # Flips all bits

print(result) # Output: -6
 

  • Bitwise Shift: This is one of the Python operators that shifts the bits to the left or right. Shifting left multiplies the number by 2, while shifting right divides it by 2. In essence, it moves the bits over, changing the number’s value.

For example:
 

a = 5 # 0101 in binary

result = a << 1 # Shift left

print(result) # Output: 10
 

  • Bitwise AND: The AND operator compares two numbers bit by bit. It returns 1 only if both bits are 1. Otherwise, it returns 0. So, both bits must be 1 for the result to be true. Here’s an example:

 

a = 5 # 0101 in binary

b = 3 # 0011 in binary

result = a & b # Bitwise AND

print(result) # Output: 1
 

  • Bitwise XOR: The XOR operator compares bits one by one. It returns 1 when the bits are different. If both are the same, it returns 0. It’s like saying, “True only if the bits don’t match.”

For example:
 

a = 5 # 0101 in binary

b = 3 # 0011 in binary

result = a ^ b # Bitwise XOR

print(result) # Output: 6
 

  • Bitwise OR: The OR operator compares two numbers bit by bit. It returns 1 if at least one of the bits is 1.
     

Python operators are essential for performing calculations, comparisons, and logical operations. If you're looking to master Python from the ground up, a Python Certification Course covers everything from basic syntax to advanced programming concepts, helping you build real-world applications with ease.

 

Concluding Words

 

In summary, Python operators are powerful tools that make working with data simpler. They handle tasks like basic calculations, comparisons, and even bit-level operations. Once you understand operators like arithmetic, comparison, logic, assignment, and bitwise, you'll write more efficient code. As you master them, solving problems and optimizing your code will become quicker and easier, making your programming process smoother.

 

Frequently Asked Questions

 
Q1. What is the +- in Python?

Ans. In Python, there’s no operator like +-. However, you’ll often see += and -=, which are shortcuts. These Python operators let you quickly add or subtract from a variable and update its value in one step.

Q2.What is the [:] operator in Python?

Ans. In Python, the [:] operator is a useful tool for slicing sequences like lists, strings, and tuples. It allows you to extract a part of the sequence while leaving the original intact.