Operators in Python are special symbols or keywords that perform operations on values or variables. These are essential building blocks in Python to manipulate data and perform computations.
Types of Python Operators
Python provides the following types of operators:
- Arithmetic Operators
- Comparison (Relational) Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Membership Operators
- Identity Operators
1. Arithmetic Operators
These operators are used for mathematical calculations.
Operator | Description | Example |
---|---|---|
+ |
Addition | 5 + 3 = 8 |
- |
Subtraction | 5 - 3 = 2 |
* |
Multiplication | 5 * 3 = 15 |
/ |
Division (float) | 5 / 2 = 2.5 |
// |
Floor Division | 5 // 2 = 2 |
% |
Modulus (remainder) | 5 % 2 = 1 |
** |
Exponentiation | 2 ** 3 = 8 |
2. Comparison (Relational) Operators
These operators compare values and return a Boolean result (True
or False
).
Operator | Description | Example |
---|---|---|
== |
Equal to | 5 == 5 → True |
!= |
Not equal to | 5 != 3 → True |
> |
Greater than | 5 > 3 → True |
< |
Less than | 5 < 3 → False |
>= |
Greater than or equal to | 5 >= 5 → True |
<= |
Less than or equal to | 5 <= 3 → False |
3. Logical Operators
These operators combine conditional statements.
Operator | Description | Example |
---|---|---|
and |
Returns True if both conditions are True | (5 > 3 and 2 < 4) → True |
or |
Returns True if at least one condition is True | (5 > 3 or 2 > 4) → True |
not |
Reverses the Boolean result | not(5 > 3) → False |
4. Bitwise Operators
These operators perform bit-level operations.
Operator | Description | Example |
---|---|---|
& |
AND | 5 & 3 = 1 |
` | ` | OR |
^ |
XOR | 5 ^ 3 = 6 |
~ |
NOT | ~5 = -6 |
<< |
Left Shift | 5 << 1 = 10 |
>> |
Right Shift | 5 >> 1 = 2 |
5. Assignment Operators
These operators assign values to variables.
Operator | Description | Example |
---|---|---|
= |
Assign | x = 5 |
+= |
Add and assign | x += 3 (x = x + 3) |
-= |
Subtract and assign | x -= 3 (x = x – 3) |
*= |
Multiply and assign | x *= 3 (x = x * 3) |
/= |
Divide and assign | x /= 3 (x = x / 3) |
//= |
Floor divide and assign | x //= 3 |
%= |
Modulus and assign | x %= 3 |
**= |
Exponent and assign | x **= 3 |
&= |
Bitwise AND and assign | x &= 3 |
` | =` | Bitwise OR and assign |
^= |
Bitwise XOR and assign | x ^= 3 |
<<= |
Left shift and assign | x <<= 1 |
>>= |
Right shift and assign | x >>= 1 |
6. Membership Operators
These operators test membership in a sequence (e.g., string, list).
Operator | Description | Example |
---|---|---|
in |
Returns True if value is in sequence | 'a' in 'apple' → True |
not in |
Returns True if value is not in sequence | 'x' not in 'apple' → True |
7. Identity Operators
These operators test whether two variables point to the same object in memory.
Operator | Description | Example |
---|---|---|
is |
Returns True if objects are identical | x is y |
is not |
Returns True if objects are not identical | x is not y |
Operator Precedence
When multiple operators are used in a single expression, operator precedence determines the order of execution.
Example:
result = 5 + 3 * 2 # Multiplication (*) has higher precedence than addition (+)
print(result) # Output: 11
Precedence (Highest to Lowest):
- Parentheses
()
- Exponentiation
**
- Unary operators
+
,-
,~
- Multiplication, Division, Modulus
*
,/
,//
,%
- Addition, Subtraction
+
,-
- Bitwise shifts
<<
,>>
- Bitwise AND
&
- Bitwise XOR
^
- Bitwise OR
|
- Comparison operators
<
,<=
,>
,>=
,==
,!=
- Logical NOT
not
- Logical AND
and
- Logical OR
or
- Assignment
=
Python’s diverse set of operators makes it a versatile language for solving complex problems efficiently. Understanding these operators is essential for mastering Python programming!