Operators are symbols or keywords used to perform operations on values and variables. They tell Python what action to perform, such as adding numbers, comparing values, or checking conditions. Without operators, a program would not be able to calculate, decide, or manipulate data.
Every Python program uses operators to transform input into meaningful output.
Types of Operators in Python
Python provides several types of operators, each serving a specific purpose.
Arithmetic Operators
These operators are used to perform mathematical calculations.
| Operator | Meaning |
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulus (remainder) |
** | Power |
// | Floor division |
Example:
a = 10
b = 3
print(a + b) # Output:3
print(a % b) # Output:1Comparison (Relational) Operators
These operators compare two values and return either True or False.
| Operator | Meaning |
== | Equal to |
!= | Not equal |
> | Greater than |
< | Less than |
>= | Greater or equal |
<= | Less or equal |
Example:
x = 10
y = 20
print(x < y) # Output:TrueLogical Operators
These operators are used to combine conditions.
| Operator | Meaning |
and | True if both are true |
or | True if at least one is true |
not | Reverses the result |
Example:
age = 20
print(age > 18 and age < 30) # Output:TrueAssignment Operators
These operators assign values to variables.
| Operator | Meaning |
= | Assign |
+= | Add and assign |
-= | Subtract and assign |
*= | Multiply and assign |
Example:
x = 10
x += 5Membership Operators
Used to check whether a value exists in a sequence.
| Operator | Meaning |
in | Found in sequence |
not in | Not found |
Example:
fruits = ["apple", "banana"]
print("apple" in fruits) # Output:TrueIdentity Operators
Used to compare memory location of objects.
| Operator | Meaning |
is | Same object |
is not | Different object |
Example:
a = [1,2]
b = a
print(a is b) # Output:TrueGot it. Then let’s do it properly, clean, exact, and interview-safe.
Bitwise Operators
Bitwise operators work on binary representations (bits) of numbers.
They operate bit by bit, not on whole values like logical operators.
| Operator | Meaning |
| & | AND – Sets bit to 1 if both bits are 1 |
| | | OR – Set bit to 1 if one or both bits are 1 |
| ^ | XOR – Sets bit to 1 if bits are different |
| ~ | NOT – Inverts all bits |
| << | Left shift – Shifts bits to the left |
| >> | Right shift – Shifts bits to the right |
Example
a = 10
b = 3
print(a & b) # Output:2Explanation:
Binary representation:
10 → 1010
3 → 0011
------------
& → 00100010 in decimal is 2.
So, the output will be:
2Key insight (this is where most people fail)
- Logical operators (
and,or) work on truth values - Bitwise operators (
&,|) work on bits
Confusing them means you don’t understand how data actually lives in memory.
Conclusion
Operators are the tools that make Python useful. They allow programs to calculate, compare, and make decisions. Without operators, Python would only store data—it would never do anything meaningful with it.
