Definition
Operators are special symbols or keywords used to perform operations on variables and values. They are used for arithmetic calculations, comparisons, logical decisions, bitwise operations, assignments, and checking membership or identity.
Example:
a = 10
b = 5
print(a + b)
Output:
15
Types of Operators in Python
Python provides the following types of operators:
- Arithmetic Operators
- Comparison (Relational) Operators
- Assignment Operators
- Logical Operators
- Bitwise Operators
- Membership Operators
- Identity Operators
1. Arithmetic Operators
These operators perform mathematical calculations.
| Operator | Description | Example |
|---|---|---|
+ | Addition | 10 + 5 = 15 |
- | Subtraction | 10 - 5 = 5 |
* | Multiplication | 10 * 5 = 50 |
/ | Division | 10 / 5 = 2.0 |
// | Floor Division | 10 // 3 = 3 |
% | Modulus (Remainder) | 10 % 3 = 1 |
** | Exponent (Power) | 2 ** 3 = 8 |
Example
a = 10
b = 3
print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
print("Division:", a / b)
print("Floor Division:", a // b)
print("Modulus:", a % b)
print("Power:", a ** b)
Output
Addition: 13
Subtraction: 7
Multiplication: 30
Division: 3.3333333333333335
Floor Division: 3
Modulus: 1
Power: 1000
2. Comparison (Relational) Operators
These operators compare two values and return True or False.
| Operator | Description |
|---|---|
== | Equal to |
!= | Not equal to |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
Example
x = 10
y = 20
print(x == y)
print(x != y)
print(x > y)
print(x < y)
print(x >= y)
print(x <= y)
Output
False
True
False
True
False
True
3. Assignment Operators
Assignment operators assign values to variables.
| Operator | Example | Equivalent To |
|---|---|---|
= | x = 5 | Assign value |
+= | x += 2 | x = x + 2 |
-= | x -= 2 | x = x - 2 |
*= | x *= 2 | x = x * 2 |
/= | x /= 2 | x = x / 2 |
%= | x %= 2 | x = x % 2 |
//= | x //= 2 | x = x // 2 |
**= | x **= 2 | x = x ** 2 |
Example
x = 10
x += 5
print(x)
Output
15
4. Logical Operators
Logical operators combine or negate conditions.
| Operator | Description |
|---|---|
and | True if both conditions are true |
or | True if at least one condition is true |
not | Reverses the result |
Example
a = True
b = False
print(a and b)
print(a or b)
print(not a)
Output
False
True
False
5. Bitwise Operators
Bitwise operators work on the binary representation of integers.
| Operator | Description |
|---|---|
& | Bitwise AND |
| ` | ` |
^ | Bitwise XOR |
~ | Bitwise NOT |
<< | Left Shift |
>> | Right Shift |
Example
a = 5
b = 3
print(a & b)
print(a | b)
print(a ^ b)
Output
1
7
6
6. Membership Operators
Membership operators check whether a value exists in a sequence.
| Operator | Description |
|---|---|
in | Returns True if present |
not in | Returns True if not present |
Example
numbers = [10, 20, 30]
print(20 in numbers)
print(40 not in numbers)
Output
True
True
7. Identity Operators
Identity operators compare whether two variables refer to the same object.
| Operator | Description |
|---|---|
is | True if both variables refer to the same object |
is not | True if they refer to different objects |
Example
a = [1, 2]
b = a
c = [1, 2]
print(a is b)
print(a is c)
print(a is not c)
Output
True
False
True
Operator Precedence (Highest to Lowest)
()(Parentheses)**(Exponent)*,/,//,%+,-- Comparison operators (
==,!=,<,>,<=,>=) notandor
Advantages of Operators
- Simplify calculations and expressions.
- Reduce the amount of code.
- Improve readability.
- Support decision-making and logical operations.
- Enable efficient data manipulation.
