Operators In Python

7th February 2026 by Tanishq Chavan
Python C C++ Javascript React JS
Python Programming Language

Python Operators: The Complete Guide

Operators are used to perform operations on variables and values. Python divides the operators into several groups to handle different types of logic and calculations.


1. Arithmetic Operators

Used with numeric values to perform common mathematical operations.

| Operator | Name | Example | Try it | | :--- | :--- | :--- | :--- | | + | Addition | x + y | 5 + 3 = 8 | | - | Subtraction | x - y | 10 - 2 = 8 | | * | Multiplication | x * y | 4 * 3 = 12 | | / | Division | x / y | 10 / 4 = 2.5 | | % | Modulus | x % y | 10 % 3 = 1 | | ** | Exponentiation | x ** y | 2 ** 3 = 8 | | // | Floor division | x // y | 10 // 3 = 3 |

Example Usage:

a = 15
b = 4

print("Addition:", a + b)        # 19
print("Floor Division:", a // b) # 3 (removes decimal)
print("Modulus:", a % b)        # 3 (remainder)

2. Comparison Operators

Comparison operators are used to compare two values and return a Boolean (True or False).

| Operator | Name | Example | | :--- | :--- | :--- | | == | Equal | x == y | | != | Not equal | x != y | | > | Greater than | x > y | | < | Less than | x < y | | >= | Greater than or equal to | x >= y | | <= | Less than or equal to | x <= y |

Example Usage:

x = 10
y = 12

print(x == y) # False
print(x < y)  # True
print(x != y) # True

3. Logical Operators

Logical operators are used to combine conditional statements.

| Operator | Description | Example | | :--- | :--- | :--- | | and | Returns True if both statements are true | x < 5 and x < 10 | | or | Returns True if one of the statements is true | x < 5 or x < 4 | | not | Reverse the result, returns False if true | not(x < 5 and x < 10) |

Example Usage:

age = 25
has_license = True

if age >= 18 and has_license:
    print("You can drive!")

4. Assignment Operators

Assignment operators are used to assign values to variables.

| Operator | Example | Same As | | :--- | :--- | :--- | | = | x = 5 | x = 5 | | += | x += 3 | x = x + 3 | | -= | x -= 3 | x = x - 3 | | *= | x *= 3 | x = x * 3 | | /= | x /= 3 | x = x / 3 | | %= | x %= 3 | x = x % 3 | | //= | x //= 3 | x = x // 3 |


5. Bitwise Operators

Bitwise operators are used to compare (binary) numbers.

| Operator | Name | Description | | :--- | :--- | :--- | | & | AND | Sets each bit to 1 if both bits are 1 | | \| | OR | Sets each bit to 1 if one of two bits is 1 | | ^ | XOR | Sets each bit to 1 if only one of two bits is 1 | | ~ | NOT | Inverts all the bits | | << | Shift Left | Zero fill left shift | | >> | Shift Right | Signed right shift |


6. Membership Operators

Used to test if a sequence is presented in an object.

| Operator | Description | Example | | :--- | :--- | :--- | | in | Returns True if a sequence is in the object | x in y | | not in | Returns True if a sequence is not in the object | x not in y |

Example Usage:

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

print("banana" in fruits)     # True
print("orange" not in fruits) # True

7. Identity Operators

Used to compare the objects, not if they are equal, but if they are actually the same object, with the same memory location.

| Operator | Description | Example | | :--- | :--- | :--- | | is | Returns True if both variables are the same object | x is y | | is not | Returns True if both variables are not the same object | x is not y |

Example Usage:

x = ["apple", "banana"]
y = ["apple", "banana"]
z = x

print(x is z)     # True (same object)
print(x is y)     # False (different objects with same content)
print(x == y)    # True (equal content)

How did you feel about this post?

๐Ÿ˜ ๐Ÿ™‚ ๐Ÿ˜ ๐Ÿ˜• ๐Ÿ˜ก

Was this helpful?

๐Ÿ‘ ๐Ÿ‘Ž