Python Operators — Complete Guide (With Examples)

Preview

Introduction to Python Operators

Python operators (Digiedutech) are symbols or keywords that let Python perform actions on values and variables. They may look small, but they play a big role in every Python program. Python operators help perform calculations, compare data, make decisions, and control logic in a clean and readable way.

Moreover, python operators make code shorter and easier to read. For example, assignment operators allow you to update values without repeating long expressions. Therefore, learning them early helps you write clean and efficient code.

Additionally, python operators are important when you work with conditions, loops, math operations, and data processing. Consequently, understanding them is a key step toward mastering Python.

Why Python Operators Matter

Python operators are essential because they allow you to:

  • Perform calculations smoothly
  • Compare values in conditional statements
  • Control program logic using logical operators
  • Update variables quickly using assignment operators
  • Work with binary data, when needed, using bitwise operators
  • Check membership and identity, which is useful for collection

Types of Python Operators (Overview)

Python provides several types of operators, each designed for specific kinds of operations. Here’s an overview:

1. Python Arithmetic Operators

Arithmetic operators help you perform basic math in Python. They handle addition, subtraction, multiplication, and division. They also support floor division, modulus, and exponent calculations.

Sl. No.OperatorDescriptionExample
1+Addition5 + 3 = 8
2Subtraction5 – 3 = 2
3*Multiplication5 * 3 = 15
4/Division5 / 2 = 2.5
5//Floor Division5 // 2 = 2
6%Modulus5 % 2 = 1
7**Exponent5 ** 2 = 25

2. Python Assignment Operators

Assignment operators help you update the value of a variable. They make your code shorter and cleaner. Each operator changes the value on the left using the value on the right.

Sl. No.OperatorMeaning / DescriptionEquivalent Long FormExample 
1=Simple assignmentx = 5Assigns value to variable 
2+=Add and assignx = x + 5x += 5 
3-=Subtract and assignx = x – 3x -= 3 
4*=Multiply and assignx = x * 4x *= 4 
5/=Divide and assign (float result)x = x / 2x /= 2 
6//=Floor-divide and assignx = x // 2x //= 2 
7%=Modulus and assignx = x % 3x %= 3 
8**=Exponent (power) and assignx = x ** 2x **= 2 
9&=Bitwise AND and assignx = x & 3x &= 3 
10^=Bitwise XOR and assignx = x ^ 2x ^= 2 
11<<=Left shift and assignx = x << 1x <<= 1 
12>>=Right shift and assignx = x >> 1x >>= 1 

3. Python Comparison Operators

Comparison operators help you compare two values, and they return a boolean value (True or False).

Sl. No.OperatorDescriptionExample
1+Addition5 + 3 = 8
2Subtraction5 – 3 = 2
3*Multiplication5 * 3 = 15
4/Division5 / 2 = 2.5
5//Floor Division5 // 2 = 2
6%Modulus5 % 2 = 1
7**Exponent5 ** 2 = 25

4. Python Logical Operators

Logical operators help you combine conditions in Python. They return True or False based on the result.

Sl. No.OperatorMeaningDescriptionExampleResult
1andLogical ANDTrue only if both conditions are True5 > 2 and 3 < 4True
2orLogical ORTrue if at least one condition is True5 > 10 or 3 < 4True
3notLogical NOTReverses the result of a conditionnot(5 > 2)False

5. Python Bitwise Operators

Bitwise operators work on binary representations of numbers.

Sl. No.OperatorDescription
1&AND
2``
3^XOR
4~NOT
5<< Left Shift
6>> Right Shift

6. Python Membership Operators

Membership operators check whether a value exists inside a sequence. They return True or False.

Sl. No.OperatorMeaningDescriptionExampleResult
1inValue presentTrue if the value is found in the sequence“a” in “apple”True
2not inValue not presentTrue if the value is not found in the sequence3 not in [1, 2, 4]True

7. Python Identity Operators

Identity operators check whether two variables refer to the same object in memory.
They are different from comparison operators.

Sl. Nos.OperatorMeaningDescriptionExampleResult
1isSame objectTrue if both variables point to the same objectx is yTrue / False
2is notNot same objectTrue if both variables point to different objectsx is not yTrue / False

Scroll to Top