What Are Operators in Python?
Operators are special symbols or keywords used to perform operations on values and variables.
For example, when you add two numbers using the + symbol, you are using an arithmetic operator.
a = 10
b = 20
result = a + b
print(result)Output:
30
Python supports several types of operators. Let's understand them one by one.
1. Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations such as addition, subtraction, multiplication, and division.
| Operator | Meaning | Example |
|---|---|---|
| + | Addition | 10 + 5 = 15 |
| - | Subtraction | 10 - 5 = 5 |
| * | Multiplication | 10 * 5 = 50 |
| / | Division | 10 / 5 = 2.0 |
| % | Modulus | 10 % 3 = 1 |
| // | Floor Division | 10 // 3 = 3 |
| ** | Exponentiation | 2 ** 3 = 8 |
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)
2. Comparison Operators
Comparison operators are used to compare two values. They return either True or False depending on the comparison result.
| Operator | Meaning |
|---|---|
| == | Equal to |
| != | Not equal to |
| > | Greater than |
| < | Less than |
| >= | Greater than or equal to |
| <= | Less than or equal to |
Example:
a = 10 b = 20 print(a == b) print(a != b) print(a < b) print(a > b)
Output:
False
True
True
False
3. Logical Operators
Logical operators are used to combine multiple conditions and evaluate Boolean expressions.
Python has three logical operators:
- and: Returns True if both conditions are True.
- or: Returns True if at least one condition is True.
- not: Reverses the Boolean result.
Example:
age = 25 print(age > 18 and age < 60) print(age < 18 or age > 60) print(not(age > 18))
4. Assignment Operators
Assignment operators are used to assign values to variables and update their existing values.
| Operator | Example | Equivalent |
|---|---|---|
| = | x = 10 | x = 10 |
| += | x += 5 | x = x + 5 |
| -= | x -= 5 | x = x - 5 |
| *= | x *= 2 | x = x * 2 |
| /= | x /= 2 | x = x / 2 |
| %= | x %= 3 | x = x % 3 |
Example:
x = 10
x += 5
print(x)Output:
15
5. Bitwise Operators
Bitwise operators perform operations on the binary representations of integers. They are commonly used in low-level programming, bit manipulation, and certain optimization tasks.
| Operator | Meaning |
|---|---|
| & | AND |
| | | OR |
| ^ | XOR |
| ~ | NOT |
| << | Left Shift |
| >> | Right Shift |
Example:
a = 5 b = 3 print(a & b) print(a | b) print(a ^ b)
6. Membership Operators
Membership operators check whether a particular value exists in a sequence, such as a string, list, or tuple.
Python provides two membership operators:
- in: Returns True if the value exists in a sequence.
- not in: Returns True if the value does not exist in a sequence.
Example:
fruits = ["apple", "banana", "mango"] print("apple" in fruits) print("orange" not in fruits)
Output:
True
True
7. Identity Operators
Identity operators check whether two variables refer to the same object in memory. They are different from comparison operators, which check whether two values are equal.
Python provides two identity operators:
- is: Returns True if both variables refer to the same object.
- is not: Returns True if both variables refer to different objects.
Example:
a = [1, 2, 3] b = a c = [1, 2, 3] print(a is b) print(a is c)
Output:
True
False
Although a and c contain the same values, they are different list objects in memory. However, b refers to the same object as a.
Conclusion
Operators are essential in Python programming because they allow developers to perform calculations, compare values, combine conditions, assign values, and manipulate data.
Python provides seven main categories of operators: arithmetic, comparison, logical, assignment, bitwise, membership, and identity operators. Understanding these operators will help you write better programs, understand Python code more easily, and solve programming problems efficiently.
