Operators are symbols or keywords that tell Python to perform an operation on values.
You've already used some operators without probably realizing it:
total = 10 + 5Here, + is an operator used for addition.
Python provides different types of operators for calculations, comparisons, logical operations, and more.
Types of Operators in Python
The main types are:
Type | Purpose |
|---|---|
Arithmetic | Perform mathematical calculations |
Assignment | Assign values to variables |
Comparison | Compare values |
Logical | Combine conditions |
Identity | Check whether two objects are the same |
Membership | Check whether a value exists in a collection |
Bitwise | Perform operations on binary values |
Let's look at each one.
1. Arithmetic Operators
Arithmetic operators are used for mathematical calculations.
Operator | Meaning | Example |
|---|---|---|
| Addition |
|
| Subtraction |
|
| Multiplication |
|
| Division |
|
| Floor Division |
|
| Modulus |
|
| Exponentiation |
|
Example:
a = 10
b = 3
print(a + b)
print(a - b)
print(a * b)
print(a / b)
print(a // b)
print(a % b)
print(a ** b)Output:
13
7
30
3.3333333333333335
3
1
10002. Assignment Operators
Assignment operators are used to assign or update values in variables.
The basic assignment operator is:
x = 10Python also provides shortcuts:
Operator | Example | Equivalent To |
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
For example:
score = 10
score += 5
print(score)Output:
153. Comparison Operators
Comparison operators are used to compare two values. The result is always True or False.
Operator | Meaning |
|---|---|
| Equal to |
| Not equal to |
| Greater than |
| Less than |
| Greater than or equal to |
| Less than or equal to |
Example:
age = 20
print(age == 20)
print(age > 18)
print(age < 18)Output:
True
True
FalseDon't confuse = with ==.
x = 10 # Assignment
x == 10 # Comparison4. Logical Operators
Logical operators are used to combine multiple conditions.
Python has three logical operators:
Operator | Meaning |
|---|---|
| Both conditions must be true |
| At least one condition must be true |
| Reverses the result |
and
age = 20
print(age >= 18 and age <= 30)Both conditions are true, so the result is:
Trueor
age = 20
print(age < 18 or age > 18)At least one condition is true, so the result is:
Truenot
is_logged_in = True
print(not is_logged_in)Output:
False5. Identity Operators
Identity operators check whether two variables refer to the same object.
There are two:
is
is notExample:
a = [1, 2, 3]
b = a
print(a is b)Output:
Trueb refers to the same list object as a.
For everyday value comparisons, use ==. The is operator is mainly used for object identity, especially with values such as None.
6. Membership Operators
Membership operators check whether a value exists inside a sequence or collection.
There are two:
in
not inExample:
fruits = ["Apple", "Banana", "Mango"]
print("Apple" in fruits)
print("Orange" not in fruits)Output:
True
TrueThis is very useful when checking whether a particular item exists in a list, string, or other collection.
7. Bitwise Operators
Bitwise operators work with numbers at the binary level.
Some common bitwise operators are:
Operator | Meaning |
|---|---|
| AND |
` | ` |
| XOR |
| NOT |
| Left shift |
| Right shift |
For example:
a = 5
b = 3
print(a & b)Output:
1You won't need bitwise operators often when starting with Python, but they are useful in areas such as low-level programming, networking, and performance-oriented code.
Operator Precedence
When an expression contains multiple operators, Python follows a certain order.
For example:
result = 10 + 5 * 2
print(result)Output:
20Multiplication happens before addition:
5 * 2 = 10
10 + 10 = 20You can use parentheses when you want to make the order explicit:
result = (10 + 5) * 2
print(result)Output:
30A good habit is to use parentheses when they make your intention clearer.
A Practical Example
Let's use operators to check whether a student passed:
marks = 75
passed = marks >= 40
print("Passed:", passed)Output:
Passed: TrueWe can make the condition a little more interesting:
marks = 75
attendance = 85
if marks >= 40 and attendance >= 75:
print("John passed the exam")Here, the comparison operators and the and logical operator work together to make the decision.
Operators may look like small symbols, but they are fundamental building blocks of Python programs. You'll use them in calculations, conditions, loops, and almost every program you write.