Operators are special symbols in Python that perform specific operations on variables and values. They are used to manipulate data and carry out computations. Python provides a variety of operators for arithmetic, comparison, logical operations, and more.
we’ll cover:
- What operators are.
- Types of Python operators.
- Examples to demonstrate their use.
What are Operators?
Operators are symbols or keywords that perform operations on one or more operands (values or variables). For example, the +
operator adds two numbers, while ==
checks if two values are equal.
Types of Python Operators
Python supports the following types of operators:
- Arithmetic Operators
- Comparison (Relational) Operators
- Logical Operators
- Assignment Operators
- Bitwise Operators
- Membership Operators
- Identity Operators
1. Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations.
Operator | Description | Example |
---|---|---|
+ |
Addition | 3 + 2 → 5 |
- |
Subtraction | 5 - 3 → 2 |
* |
Multiplication | 4 * 2 → 8 |
/ |
Division | 10 / 2 → 5.0 |
% |
Modulus (remainder) | 7 % 3 → 1 |
** |
Exponentiation (power) | 2 ** 3 → 8 |
// |
Floor division (quotient) | 7 // 2 → 3 |
Example:
a = 10 b = 3 print(a + b) # Output: 13 print(a - b) # Output: 7 print(a * b) # Output: 30 print(a / b) # Output: 3.3333333333333335 print(a % b) # Output: 1 print(a ** b) # Output: 1000 print(a // b) # Output: 3
2. Comparison (Relational) Operators
Comparison operators are used to compare two values. They 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 | 3 < 5 → True |
>= |
Greater than or equal to | 5 >= 5 → True |
<= |
Less than or equal to | 3 <= 5 → True |
Example:
x = 10 y = 5 print(x == y) # Output: False print(x > y) # Output: True print(x <= y) # Output: False
3. Logical Operators
Logical operators are used to combine multiple Boolean expressions.
Operator | Description | Example |
---|---|---|
and |
Returns True if both are True |
True and False → False |
or |
Returns True if one is True |
True or False → True |
not |
Reverses the Boolean value | not True → False |
Example:
x = 10 y = 5 print(x > y and x > 0) # Output: True print(x > y or x < 0) # Output: True print(not (x > y)) # Output: False
4. Assignment Operators
Assignment operators are used to assign values to variables, often with arithmetic operations.
Operator | Description | Example |
---|---|---|
= |
Assign | x = 10 |
+= |
Add and assign | x += 5 → x = x + 5 |
-= |
Subtract and assign | x -= 5 → x = x - 5 |
*= |
Multiply and assign | x *= 2 → x = x * 2 |
/= |
Divide and assign | x /= 2 → x = x / 2 |
%= |
Modulus and assign | x %= 3 → x = x % 3 |
//= |
Floor division and assign | x //= 2 → x = x // 2 |
**= |
Exponentiation and assign | x **= 3 → x = x ** 3 |
Example:
x = 10 x += 5 # Equivalent to x = x + 5 print(x) # Output: 15
5. Bitwise Operators
Bitwise operators perform operations on binary representations of integers.
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 |
Example:
x = 5 # Binary: 0101 y = 3 # Binary: 0011 print(x & y) # Output: 1 (Binary: 0001) print(x | y) # Output: 7 (Binary: 0111) print(x ^ y) # Output: 6 (Binary: 0110)
6. Membership Operators
Membership operators are used to check if a value is part of a sequence (like a list, tuple, or string).
Operator | Description | Example |
---|---|---|
in |
Returns True if the value is found |
"a" in "apple" → True |
not in |
Returns True if the value is not found |
"x" not in "apple" → True |
Example:
fruits = ["apple", "banana", "cherry"] print("apple" 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 they are the same object |
x is y |
is not |
Returns True if they are not the same object |
x is not y |
Example:
x = [1, 2, 3] y = x z = [1, 2, 3] print(x is y) # Output: True (same object) print(x is z) # Output: False (different objects) print(x == z) # Output: True (same content)