Python Operators
In Python, operators are special symbols that perform operations on variables and values. There are several types of operators in Python, each serving a different purpose. Here’s a detailed breakdown of the different types of operators in Python:
1. Arithmetic Operators
These operators are used to perform basic mathematical operations such as addition, subtraction, multiplication, division, etc.
| Operator | Description | Example | Output |
|---|---|---|---|
+ | Addition | 5 + 3 | 8 |
- | Subtraction | 5 - 3 | 2 |
* | Multiplication | 5 * 3 | 15 |
/ | Division | 5 / 3 | 1.666... |
// | Floor Division | 5 // 3 | 1 |
% | Modulus (Remainder) | 5 % 3 | 2 |
** | Exponentiation | 5 ** 3 | 125 |
Example:
x = 5
y = 3
# Addition
print(x + y) # Output: 8
# Subtraction
print(x - y) # Output: 2
# Multiplication
print(x * y) # Output: 15
# Division
print(x / y) # Output: 1.666...
# Floor Division
print(x // y) # Output: 1
# Modulus
print(x % y) # Output: 2
# Exponentiation
print(x ** y) # Output: 125
2. Comparison (Relational) Operators
These operators compare two values and return a boolean result (True or False).
| Operator | Description | Example | Output |
|---|---|---|---|
== | Equal to | x == y | False |
!= | Not equal to | x != y | True |
> | Greater than | x > y | True |
< | Less than | x < y | False |
>= | Greater than or equal to | x >= y | True |
<= | Less than or equal to | x <= y | False |
Example:
x = 5
y = 3
# Comparison
print(x == y) # Output: False
print(x != y) # Output: True
print(x > y) # Output: True
print(x < y) # Output: False
print(x >= y) # Output: True
print(x <= y) # Output: False
3. Assignment Operators
These operators are used to assign values to variables.
| Operator | Description | Example | Output |
|---|---|---|---|
= | Assign value | x = 5 | x = 5 |
+= | Add and assign | x += 3 | x = 8 |
-= | Subtract and assign | x -= 3 | x = 5 |
*= | Multiply and assign | x *= 3 | x = 15 |
/= | Divide and assign | x /= 3 | x = 1.666... |
//= | Floor division and assign | x //= 3 | x = 1 |
%= | Modulus and assign | x %= 3 | x = 2 |
**= | Exponentiation and assign | x **= 3 | x = 125 |
Example:
x = 5
# Using assignment operators
x += 3 # x = x + 3
print(x) # Output: 8
x *= 2 # x = x * 2
print(x) # Output: 16
4. Logical Operators
Logical operators are used to combine conditional statements. They return a boolean result.
| Operator | Description | Example | Output |
|---|---|---|---|
and | Returns True if both statements are true | x > 3 and x < 10 | True |
or | Returns True if at least one statement is true | x < 3 or x > 10 | False |
not | Reverses the result (True to False, False to True) | not(x > 3) | False |
Example:
x = 5
# Logical Operators
print(x > 3 and x < 10) # Output: True
print(x < 3 or x > 10) # Output: False
print(not(x > 3)) # Output: False
5. Identity Operators
Identity operators are used to compare the memory location of two objects.
| Operator | Description | Example | Output |
|---|---|---|---|
is | Returns True if both variables point to the same object | x is y | True/False |
is not | Returns True if both variables point to different objects | x is not y | True/False |
Example:
x = [1, 2, 3]
y = [1, 2, 3]
print(x is y) # Output: False (they are different objects in memory)
print(x == y) # Output: True (they have the same content)
print(x is not y) # Output: True (they are not the same object)
6. Membership Operators
Membership operators are used to test if a value is present in a sequence (such as a list, tuple, or string).
| Operator | Description | Example | Output |
|---|---|---|---|
in | Returns True if value is found in the sequence | x in y | True/False |
not in | Returns True if value is not found in the sequence | x not in y | True/False |
Example:
x = 5
y = [1, 2, 3, 4, 5]
print(x in y) # Output: True (5 is in the list)
print(6 not in y) # Output: True (6 is not in the list)
7. Bitwise Operators
Bitwise operators are used to perform operations on binary numbers at the bit level.
| Operator | Description | Example | Output |
|---|---|---|---|
& | Bitwise AND | 5 & 3 | 1 |
| ` | ` | Bitwise OR | `5 |
^ | Bitwise XOR | 5 ^ 3 | 6 |
~ | Bitwise NOT | ~5 | -6 |
<< | Left Shift | 5 << 1 | 10 |
>> | Right Shift | 5 >> 1 | 2 |
Example:
x = 5 # Binary: 0101
y = 3 # Binary: 0011
# Bitwise AND
print(x & y) # Output: 1 (Binary: 0001)
# Bitwise OR
print(x | y) # Output: 7 (Binary: 0111)
# Left Shift
print(x << 1) # Output: 10 (Binary: 1010)
8. Precedence of Operators
Python evaluates expressions using the precedence of operators. For example, arithmetic operators (*, /) have higher precedence than logical operators (and, or), and operators like not have higher precedence than and and or.
Summary of Operator Categories:
- Arithmetic Operators: Perform mathematical operations like addition, subtraction, etc.
- Comparison Operators: Compare values and return
TrueorFalse. - Assignment Operators: Assign values to variables.
- Logical Operators: Combine boolean expressions.
- Identity Operators: Compare memory locations of objects.
- Membership Operators: Check if a value exists in a sequence.
- Bitwise Operators: Perform bit-level operations.