Operators are symbols or keywords in Python used to perform operations on variables and values. Python provides a wide range of operators that are grouped into different categories based on their functionality.
Types of Python Operators
- Arithmetic Operators
- Comparison (Relational) Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Membership Operators
- Identity Operators
1. Arithmetic Operators
Arithmetic operators are used to perform mathematical operations like addition, subtraction, etc.
Operator | Name | Description | Example |
---|---|---|---|
+ | Addition | Adds two values | 5 + 3 → 8 |
- | Subtraction | Subtracts the second value from the first | 5 - 3 → 2 |
* | Multiplication | Multiplies two values | 5 * 3 → 15 |
/ | Division | Divides the first value by the second (float result) | 5 / 2 → 2.5 |
// | Floor Division | Divides and truncates the decimal part | 5 // 2 → 2 |
% | Modulus | Returns the remainder | 5 % 2 → 1 |
** | Exponentiation | Raises the first value to the power of the second | 2 ** 3 → 8 |
2. Comparison (Relational) Operators
Comparison operators compare two values and return a Boolean result (True
or False
).
Operator | Name | Description | Example |
---|---|---|---|
== | Equal to | Checks if two values are equal | 5 == 5 → True |
!= | Not equal to | Checks if two values are not equal | 5 != 3 → True |
> | Greater than | Checks if the first value is greater | 5 > 3 → True |
< | Less than | Checks if the first value is smaller | 5 < 3 → False |
>= | Greater than or equal to | Checks if the first value is greater or equal | 5 >= 5 → True |
<= | Less than or equal to | Checks if the first value is smaller or equal | 5 <= 3 → False |
3. Logical Operators
Logical operators are used to combine conditional statements.
Operator | Name | Description | Example |
---|---|---|---|
and | Logical AND | Returns True if both conditions are True | (5 > 3 and 3 > 1) → True |
or | Logical OR | Returns True if at least one condition is True | (5 > 3 or 3 < 1) → True |
not | Logical NOT | Reverses the Boolean value | not(5 > 3) → False |
4. Bitwise Operators
Bitwise operators work at the binary level.
Operator | Name | Description | Example (Binary) |
---|---|---|---|
& | AND | Performs binary AND | 5 & 3 → 1 (0101 & 0011 → 0001) |
` | ` | OR | Performs binary OR |
^ | XOR | Performs binary XOR | 5 ^ 3 → 6 (0101 ^ 0011 → 0110) |
~ | NOT | Performs binary NOT (1’s complement) | ~5 → -6 |
<< | Left Shift | Shifts bits to the left | 5 << 1 → 10 |
>> | Right Shift | Shifts bits to the right | 5 >> 1 → 2 |
5. Assignment Operators
Assignment operators are used to assign values to variables.
Operator | Name | Example | Explanation |
---|---|---|---|
= | Assign | x = 5 | Assigns 5 to x . |
+= | Add and Assign | x += 3 | Equivalent to x = x + 3 . |
-= | Subtract and Assign | x -= 2 | Equivalent to x = x - 2 . |
*= | Multiply and Assign | x *= 4 | Equivalent to x = x * 4 . |
/= | Divide and Assign | x /= 2 | Equivalent to x = x / 2 . |
//= | Floor Divide and Assign | x //= 3 | Equivalent to x = x // 3 . |
%= | Modulus and Assign | x %= 2 | Equivalent to x = x % 2 . |
**= | Exponent and Assign | x **= 3 | Equivalent to x = x ** 3 . |
&= | Bitwise AND and Assign | x &= 2 | Equivalent to x = x & 2 . |
` | =` | Bitwise OR and Assign | `x |
^= | Bitwise XOR and Assign | x ^= 3 | Equivalent to x = x ^ 3 . |
>>= | Right Shift and Assign | x >>= 1 | Equivalent to x = x >> 1 . |
<<= | Left Shift and Assign | x <<= 1 | Equivalent to x = x << 1 . |
6. Membership Operators
Membership operators test for the presence of a value in a sequence (e.g., list, string).
Operator | Name | Description | Example |
---|---|---|---|
in | In | Returns True if a value exists in a sequence | 'P' in "Python" → True |
not in | Not In | Returns True if a value does not exist in a sequence | 'X' not in "Python" → True |
7. Identity Operators
Identity operators check whether two variables refer to the same object in memory.
Operator | Name | Description | Example |
---|---|---|---|
is | Is | Returns True if both variables point to the same object | x is y |
is not | Is Not | Returns True if variables point to different objects | x is not y |
Precedence of Operators
Operator precedence determines the order of operations in an expression. Higher precedence operators are evaluated first.
Precedence Level | Operators |
---|---|
1 (Highest) | ** |
2 | ~ , + , - (Unary operators) |
3 | * , / , // , % |
4 | + , - |
5 | >> , << |
6 | & |
7 | ^ |
8 | ` |
9 | == , != , > , < , >= , <= |
10 | not |
11 | and |
12 (Lowest) | or |
Examples
Example 1: Arithmetic Operations
a = 10
b = 3
print(a + b) # Output: 13
print(a % b) # Output: 1
print(a ** b) # Output: 1000
Example 2: Logical Operators
x = 5
y = 10
print(x > 2 and y < 20) # Output: True
print(not(x == 5)) # Output: False
Example 3: Membership Operators
text = "Python"
print('P' in text) # Output: True
print('X' not in text) # Output: True
Example 4: Identity Operators
x = [1, 2, 3]
y = x
z = [1, 2, 3]
print(x is y) # Output: True
print(x is z) # Output: False
This explanation
provides a structured overview of Python operators with practical examples for a beginner-friendly Python tutorial. Let me know if you’d like more detailed examples!