Python Operators
Python Operators
Operators in Python are special symbols or keywords used to perform operations on variables and values. Python has several types of operators, categorized based on the operation they perform.
1. Arithmetic Operators
Arithmetic operators are used for mathematical operations.
Operator | Description | Example |
---|---|---|
+ |
Addition | 5 + 3 = 8 |
- |
Subtraction | 5 - 3 = 2 |
* |
Multiplication | 5 * 3 = 15 |
/ |
Division | 5 / 2 = 2.5 |
// |
Floor Division | 5 // 2 = 2 |
% |
Modulus (remainder) | 5 % 2 = 1 |
** |
Exponentiation | 5 ** 2 = 25 |
Example:
a = 10 b = 3 print(a + b) # Output: 13 print(a ** b) # Output: 1000
2. Comparison (Relational) Operators
These operators compare two values and return a Boolean (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 >= 3 = True |
<= |
Less than or equal to | 5 <= 3 = False |
Example:
x = 7 y = 10 print(x > y) # Output: False print(x != y) # Output: True
3. Logical Operators
Logical operators are used to combine conditional statements.
Operator | Description | Example |
---|---|---|
and |
Returns True if both conditions are True |
True and False = False |
or |
Returns True if at least one condition is True |
True or False = True |
not |
Reverses the logical state | not True = False |
Example:
a = 5 b = 10 print(a > 3 and b < 20) # Output: True print(not (a > b)) # Output: True
4. Assignment Operators
Assignment operators are used to assign values to variables.
Operator | Description | Example |
---|---|---|
= |
Assigns value | x = 5 |
+= |
Adds and assigns | x += 3 (x = x + 3) |
-= |
Subtracts and assigns | x -= 3 (x = x - 3) |
*= |
Multiplies and assigns | x *= 3 |
/= |
Divides and assigns | x /= 3 |
//= |
Floor divides and assigns | x //= 3 |
%= |
Modulus and assigns | x %= 3 |
**= |
Exponentiation and assigns | x **= 3 |
Example:
x = 10 x += 5 print(x) # Output: 15
5. Bitwise Operators
Bitwise operators operate on binary numbers.
Operator | Description | Example |
---|---|---|
& |
AND | 5 & 3 = 1 |
| |
OR | 5 | 3 = 7 |
^ |
XOR | 5 ^ 3 = 6 |
~ |
Complement | ~5 = -6 |
<< |
Left Shift | 5 << 1 = 10 |
>> |
Right Shift | 5 >> 1 = 2 |
Example:
a = 5 # 101 in binary b = 3 # 011 in binary print(a & b) # Output: 1 print(a | b) # Output: 7
6. Membership Operators
These operators check for membership in a sequence (like strings, lists, tuples).
Operator | Description | Example |
---|---|---|
in |
Returns True if value exists in sequence |
'a' in 'apple' = True |
not in |
Returns True if value does not exist in sequence |
'b' not in 'apple' = True |
Example:
fruits = ["apple", "banana", "cherry"] print("banana" in fruits) # Output: True print("grape" not in fruits) # Output: True
7. Identity Operators
Identity operators compare the memory locations of two objects.
Operator | Description | Example |
---|---|---|
is |
Returns True if objects are identical (same memory location) |
a is b |
is not |
Returns True if objects are not identical |
a is not b |
Example:
x = [1, 2, 3] y = x z = [1, 2, 3] print(x is y) # Output: True print(x is z) # Output: False
8. Special Operators
Ternary Operator (Conditional Expression)
Python allows conditional expressions using a shorthand form.
Example:
a, b = 5, 10 result = "A is greater" if a > b else "B is greater" print(result) # Output: B is greater
Summary:
Python operators allow you to perform various tasks, from arithmetic to logical comparisons. Understanding these operators is essential for writing efficient and effective Python code.