Python provides a variety of operators that allow you to perform different operations on variables and values. Here are some of the commonly used operators in Python:
1. Arithmetic Operators:
- Addition (+): Adds two operands.
- Subtraction (-): Subtracts the right operand from the left operand.
- Multiplication (*): Multiplies two operands.
- Division (/): Divides the left operand by the right operand.
- Modulo (%): Returns the remainder of the division of the left operand by the right operand.
- Exponentiation (**): Raises the left operand to the power of the right operand.
- Floor Division (//): Performs integer division, discarding the decimal part.
Example:
```python
x = 10
y = 3
print(x + y) # Output: 13
print(x - y) # Output: 7
print(x * y) # Output: 30
print(x / y) # Output: 3.3333333333333335
print(x % y) # Output: 1
print(x ** y) # Output: 1000
print(x // y) # Output: 3
```
2. Assignment Operators:
- Assignment (=): Assigns a value to a variable.
- Addition Assignment (+=): Adds the right operand to the left operand and assigns the result to the left operand.
- Subtraction Assignment (-=): Subtracts the right operand from the left operand and assigns the result to the left operand.
- Multiplication Assignment (*=): Multiplies the left operand by the right operand and assigns the result to the left operand.
- Division Assignment (/=): Divides the left operand by the right operand and assigns the result to the left operand.
- Modulo Assignment (%=): Performs modulo division and assigns the remainder to the left operand.
- Exponentiation Assignment (**=): Raises the left operand to the power of the right operand and assigns the result to the left operand.
- Floor Division Assignment (//=): Performs integer division and assigns the quotient to the left operand.
Example:
```python
x = 10
y = 3
x += y # Equivalent to x = x + y
print(x) # Output: 13
x -= y # Equivalent to x = x - y
print(x) # Output: 10
x *= y # Equivalent to x = x * y
print(x) # Output: 30
x /= y # Equivalent to x = x / y
print(x) # Output: 10.0
x %= y # Equivalent to x = x % y
print(x) # Output: 1.0
x **= y # Equivalent to x = x ** y
print(x) # Output: 1.0
x //= y # Equivalent to x = x // y
print(x) # Output: 0.0
```
3. Comparison Operators:
- Equal to (==): Checks if two operands are equal.
- Not equal to (!=): Checks if two operands are not equal.
- Greater than (>): Checks if the left operand is greater than the right operand.
- Less than (<): Checks if the left operand is less than the right operand.
- Greater than or equal to (>=): Checks if the left operand is greater than or equal to the right operand.
- Less than or equal to (<=): Checks if the left operand is less than or equal to the right operand.
Example:
```python
x = 10
y = 5
print(x == y) # Output: False
print(x != y) # Output: True
print(x > y) # Output: True
print(x < y) # Output: False
print(x >= y) # Output: True
print(x <= y) # Output: False
```
4. Logical Operators:
- Logical AND (and): Returns True if both operands are True, otherwise returns False.
- Logical OR (or): Returns True if either of the operands is True, otherwise returns False.
- Logical NOT (not): Returns the opposite of the operand's logical value.
Example:
```python
x = True
y = False
print(x and y) # Output: False
print(x or y) # Output: True
print(not x) # Output: False
Comments
Post a Comment