Python supports various types of operators, including arithmetic, comparison, logical, assignment, and more. Here are some examples of different types of operators in Python:
1. Arithmetic Operators:
```python
a = 10
b = 3
addition = a + b # 13
subtraction = a - b # 7
multiplication = a * b # 30
division = a / b # 3.3333 (floating-point division)
modulus = a % b # 1 (remainder)
exponentiation = a ** b # 1000
```
2. Comparison Operators:
```python
x = 5
y = 10
is_equal = x == y # False
not_equal = x != y # True
greater_than = x > y # False
less_than = x < y # True
```
3. Logical Operators:
```python
a = True
b = False
logical_and = a and b # False
logical_or = a or b # True
logical_not = not a # False
```
4. Assignment Operators:
```python
x = 5
x += 3 # equivalent to x = x + 3
```
5. Membership Operators:
```python
numbers = [1, 2, 3, 4, 5]
is_in_list = 3 in numbers # True
is_not_in_list = 6 not in numbers # True
```
6. Identity Operators:
```python
a = [1, 2, 3]
b = [1, 2, 3]
is_same_object = a is b # False (checks if objects are the same)
is_not_same_object = a is not b # True
```
These examples cover various types of operators in Python. Operators are essential for performing different types of operations on variables and values.
0 Comments