What is Operator?
- Operators in Python are special symbols or keywords used to perform operations on variables.
Types of Operators:
- Arithmetic Operator
- Assignment Operator
- Comparison Operator
- Logical Operator
- Identity Operator
- Membership Operator
- Bitwise Operator
Arithmetic Operator:
- Used to perform mathematical operations.
| Operator | Name | Example | Result |
|---|---|---|---|
+ |
Addition | 10 + 5 |
15 |
- |
Subtraction | 10 - 5 |
5 |
* |
Multiplication | 10 * 5 |
50 |
/ |
Division | 10 / 5 |
2.0 |
// |
Floor Division | 10 // 3 |
3 |
% |
Modulus | 10 % 3 |
1 |
** |
Exponentiation | 2 ** 3 |
8 |
Example:
a = 10
b = 3
print(a + b)
print(a-b)
print(a*b)
print(a // b)
print(a % b)
print(a ** b)
13
7
30
3
1
1000
2. Assignment Operators
Used to assign values to variables or update their values.
| Operator | Example | Equivalent |
|---|---|---|
= |
a = 10 |
Assign 10 |
+= |
a += 5 |
a = a + 5 |
-= |
a -= 5 |
a = a - 5 |
*= |
a *= 5 |
a = a * 5 |
/= |
a /= 5 |
a = a / 5 |
//= |
a //= 5 |
a = a // 5 |
%= |
a %= 5 |
a = a % 5 |
**= |
a **= 2 |
a = a ** 2 |
Example:
a=10
a += 5;
print(a)
a -= 5;
print(a)
a *= 5
print(a)
a /= 5
print(a)
15
10
50
10.0
3. Comparison Operators
Used to compare two values. They return either True or False.
| Operator | Meaning | Example | Result |
|---|---|---|---|
== |
Equal to | 10 == 10 |
True |
!= |
Not equal to | 10 != 5 |
True |
> |
Greater than | 10 > 5 |
True |
< |
Less than | 10 < 5 |
False |
>= |
Greater than or equal to | 10 >= 10 |
True |
<= |
Less than or equal to | 5 <= 10 |
True |
Example:
a = 10
b = 5
print(a == b)
print(a > b)
print(a != b)
False
True
True
4. Logical Operators
Used to combine multiple conditions.
| Operator | Meaning | Example | Result |
|---|---|---|---|
and |
True if both conditions are True | True and False |
False |
or |
True if at least one condition is True | True or False |
True |
not |
Reverses the result | not True |
False |
Example:
age = 24
print(age > 18 and age < 30)
print(age > 30 or age == 24)
print(not age > 18)
True
True
False
5. Identity Operators
Used to check whether two variables refer to the same object in memory.
| Operator | Meaning |
|---|---|
is |
Returns True if both variables refer to the same object. |
is not |
Returns True if both variables refer to different objects. |
Example:
a = [10, 20]
b = a
c = [10, 20]
print(a is b)
print(a is c)
print(a == c)
True
False
True
6. Membership Operators
Used to check whether a value exists in a sequence such as a string, list or tuple.
| Operator | Meaning |
|---|---|
in |
Checks whether a value exists in a sequence. |
not in |
Checks whether a value does not exist in a sequence. |
Example:
fruits = ["apple", "banana", "orange"]
print("apple" in fruits)
print("grapes" not in fruits)
True
True
7. Bitwise Operators
Used to perform operations on the binary (bit-level) representation of integers.
| Operator | Name |
|---|---|
& |
AND |
^ |
XOR |
~ |
NOT |
<< |
Left Shift |
>> |
Right Shift |
Example:
a = 5 # 0101
b = 3 # 0011
print(a & b)
print(a | b)
print(a ^ b)
print(a << 1)
print(a >> 1)
1
7
6
10
2
Top comments (0)