Python Operators
Operators are special symbols in Python that perform specific operations on values (operands). They are fundamental to performing calculations, comparisons, and logical operations. Here's a breakdown of the different types of operators in Python:
1. Arithmetic Operators:
These operators perform mathematical operations.
| Operator | Description | Example | Result |
|---|---|---|---|
+ |
Addition | x + y |
Sum of x and y |
- |
Subtraction | x - y |
Difference between x and y |
* |
Multiplication | x * y |
Product of x and y |
/ |
Division | x / y |
Quotient of x and y (always returns a float) |
// |
Floor Division | x // y |
Quotient of x and y, rounded down to the nearest integer |
% |
Modulus | x % y |
Remainder of x divided by y |
** |
Exponentiation | x ** y |
x raised to the power of y |
x = 10
y = 3
print(x + y) # Output: 13
print(x - y) # Output: 7
print(x * y) # Output: 30
print(x / y) # Output: 3.3333333333333335
print(x // y) # Output: 3
print(x % y) # Output: 1
print(x ** y) # Output: 1000
2. Assignment Operators:
These operators are used to assign values to variables.
| Operator | Description | Example | Equivalent to |
|---|---|---|---|
= |
Assign | x = 5 |
x = 5 |
+= |
Add and Assign | x += 3 |
x = x + 3 |
-= |
Subtract and Assign | x -= 2 |
x = x - 2 |
*= |
Multiply and Assign | x *= 4 |
x = x * 4 |
/= |
Divide and Assign | x /= 2 |
x = x / 2 |
//= |
Floor Divide and Assign | x //= 3 |
x = x // 3 |
%= |
Modulus and Assign | x %= 5 |
x = x % 5 |
**= |
Exponentiate and Assign | x **= 2 |
x = x ** 2 |
x = 5
x += 3 # x = x + 3 => x = 8
print(x) # Output: 8
x *= 2 # x = x * 2 => x = 16
print(x) # Output: 16
3. Comparison Operators:
These operators compare two values and return a boolean (True or False).
| Operator | Description | Example | Result |
|---|---|---|---|
== |
Equal to | x == y |
True if x and y are equal, False otherwise |
!= |
Not equal to | x != y |
True if x and y are not equal, False otherwise |
> |
Greater than | x > y |
True if x is greater than y, False otherwise |
< |
Less than | x < y |
True if x is less than y, False otherwise |
>= |
Greater than or equal to | x >= y |
True if x is greater than or equal to y, False otherwise |
<= |
Less than or equal to | x <= y |
True if x is less than or equal to y, False otherwise |
x = 5
y = 10
print(x == y) # Output: False
print(x != y) # Output: True
print(x > y) # Output: False
print(x < y) # Output: True
print(x >= 5) # Output: True
print(y <= 10) # Output: True
4. Logical Operators:
These operators combine conditional statements.
| Operator | Description | Example | Result |
|---|---|---|---|
and |
Logical AND | x and y |
True if both x and y are True, False otherwise |
or |
Logical OR | x or y |
True if either x or y is True, False if both are False |
not |
Logical NOT | not x |
True if x is False, False if x is True |
x = True
y = False
print(x and y) # Output: False
print(x or y) # Output: True
print(not x) # Output: False
print(not y) # Output: True
5. Identity Operators:
These operators check if two variables refer to the same object in memory.
| Operator | Description | Example |
|---|---|---|
is |
Returns True if both variables point to the same object |
x is y |
is not |
Returns True if both variables do not point to the same object |
x is not y |
x = [1, 2, 3]
y = x # y refers to the same list object as x
z = [1, 2, 3] # z is a new list object with the same content
print(x is y) # Output: True
print(x is z) # Output: False
print(x is not y) # Output: False
print(x is not z) # Output: True
6. Membership Operators:
These operators check if a value is present in a sequence (e.g., string, list, tuple).
| Operator | Description | Example |
|---|---|---|
in |
Returns True if the value is found in the sequence |
x in y |
not in |
Returns True if the value is not found in the sequence |
x not in y |
my_list = [1, 2, 3, 4, 5]
print(3 in my_list) # Output: True
print(6 in my_list) # Output: False
print(3 not in my_list) # Output: False
print(6 not in my_list) # Output: True
my_string = "Hello World"
print("World" in my_string) # Output: True
Operator Precedence:
Python follows a specific order of operations (precedence) when evaluating expressions. Here's a simplified order (from highest to lowest):
**(Exponentiation)*,/,//,%(Multiplication, Division, Floor Division, Modulus)+,-(Addition, Subtraction)==,!=,>,<,>=,<=(Comparison)not(Logical NOT)and(Logical AND)or(Logical OR)=(Assignment)
You can use parentheses () to override the default precedence and control the order of evaluation.
result = 2 + 3 * 4 # Multiplication happens before addition: 2 + 12 = 14
print(result) # Output: 14
result = (2 + 3) * 4 # Addition happens first due to parentheses: 5 * 4 = 20
print(result) # Output: 20
Understanding operators is crucial for writing effective and correct Python code. Practice using them in different scenarios to solidify your understanding.