….
Python Operators – The Power Behind Expressions
In Python, operators are special symbols that perform operations on variables and values. They are the building blocks of expressions, helping you do everything from simple math to complex logical comparisons.
👉 For example:
print(10 + 5) # Output: 15
Here, +
is the addition operator.
Types of Operators in Python
Python groups operators into the following categories:
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Identity Operators
- Membership Operators
- Bitwise Operators
1. Arithmetic Operators
Arithmetic operators are used for basic mathematical calculations.
Operator | Name | Example | Result |
---|---|---|---|
+ | Addition | 10 + 5 | 15 |
- | Subtraction | 10 - 5 | 5 |
* | Multiplication | 10 * 5 | 50 |
/ | Division | 10 / 5 | 2.0 |
% | Modulus | 10 % 3 | 1 |
** | Exponentiation | 2 ** 3 | 8 |
// | Floor Division | 10 // 3 | 3 |
2. Assignment Operators
Assignment operators are used to assign values to variables, often combining assignment with another operation.
x = 10 # Simple assignment
x += 5 # x = x + 5 → 15
x *= 2 # x = x * 2 → 30
Common assignment operators: =
, +=
, -=
, *=
, /=
, %=
, //=
, **=
, &=
, |=
, ^=
, >>=
, <<=
, :=
(walrus operator).
3. Comparison Operators
Comparison operators compare two values and return a Boolean result (True
or False
).
print(10 == 10) # True
print(10 != 5) # True
print(7 > 3) # True
print(7 <= 3) # False
Operator | Name | Example |
---|---|---|
== | Equal to | x == y |
!= | Not equal to | x != y |
> | Greater than | x > y |
< | Less than | x < y |
>= | Greater than or equal to | x >= y |
<= | Less than or equal to | x <= y |
4. Logical Operators
Logical operators are used to combine conditional statements.
x = 5
print(x > 3 and x < 10) # True
print(x > 10 or x == 5) # True
print(not(x > 3 and x < 10)) # False
Operator | Description | Example |
---|---|---|
and | True if both conditions are True | x < 5 and x < 10 |
or | True if at least one condition is True | x < 5 or x < 4 |
not | Reverses the result | not(x < 5 and x < 10) |
5. Identity Operators
Identity operators check if two variables refer to the same object in memory.
a = [1, 2, 3]
b = a
c = [1, 2, 3]
print(a is b) # True (same object)
print(a is c) # False (same values, different objects)
print(a is not c) # True
is
→ True if objects are the same.is not
→ True if objects are different.
6. Membership Operators
Membership operators test if a value exists inside a sequence (like a string, list, or tuple).
fruits = ["apple", "banana", "cherry"]
print("apple" in fruits) # True
print("grape" not in fruits) # True
in
→ True if value exists in sequence.not in
→ True if value does not exist in sequence.
7. Bitwise Operators
Bitwise operators work on the binary representation of numbers.
x = 6 # 110 in binary
y = 3 # 011 in binary
print(x & y) # 2 (010)
print(x | y) # 7 (111)
print(x ^ y) # 5 (101)
print(~x) # -7 (inverts bits)
print(x << 2) # 24 (11000)
print(x >> 1) # 3 (011)
Operator | Name | Description |
---|---|---|
& | AND | Sets bit if both are 1 |
` | ` | OR |
^ | XOR | Sets bit if only one is 1 |
~ | NOT | Inverts bits |
<< | Left shift | Shifts bits left |
>> | Right shift | Shifts bits right |
Operator Precedence
When multiple operators are used in one expression, Python follows precedence rules (order of execution).
print(100 + 5 * 3) # 115 (multiplication before addition)
print((100 + 5) * 3) # 315 (parentheses first)
Precedence Order (Highest → Lowest)
()
Parentheses**
Exponentiation+x, -x, ~x
Unary operators* / // %
Multiplication & division+ -
Addition & subtraction<< >>
Shifts&
Bitwise AND^
Bitwise XOR|
Bitwise OR- Comparison, identity, membership
not
and
or
Real-World Applications of Operators 🚀
- Arithmetic → Finance calculators, scientific computation
- Comparison & Logical → Decision-making in AI models
- Assignment → Loop counters, updating variables
- Identity & Membership → Checking database records or cache
- Bitwise → Image processing, cryptography, compression
Quick Recap
✔️ Operators are used for calculations, comparisons, logic, and more.
✔️ Python has 7 groups of operators.
✔️ Precedence defines the execution order in complex expressions.
✔️ Mastering operators helps you write faster, smarter, and cleaner code.
👉 At GoNimbus, we help learners go beyond just syntax — we show real-world use cases of Python operators so you can apply them confidently in coding projects.