Python operators are essential for performing a variety of operations on variables and values. Understanding them helps in writing efficient and clear Python code. Below is an explanation of various types of operators in Python:

### 1. Arithmetic Operators
Arithmetic operators are used to perform mathematical operations. These include:

– `+` (Addition): Adds two operands.
“`python
result = 3 + 5 # 8
“`

– `-` (Subtraction): Subtracts the right operand from the left operand.
“`python
result = 10 – 4 # 6
“`

– `*` (Multiplication): Multiplies two operands.
“`python
result = 6 * 7 # 42
“`

– `/` (Division): Divides the left operand by the right operand, returning a float.
“`python
result = 15 / 3 # 5.0
“`

– `//` (Floor Division): Divides and returns the largest whole number.
“`python
result = 15 // 2 # 7
“`

– `%` (Modulus): Returns the remainder of the division.
“`python
result = 14 % 3 # 2
“`

– `**` (Exponentiation): Raises the left operand to the power of the right operand.
“`python
result = 2 ** 3 # 8
“`

### 2. Comparison Operators
Comparison operators compare two values and return `True` or `False`. These include:

– `==` (Equal): Checks if two values are equal.
“`python
result = (5 == 5) # True
“`

– `!=` (Not equal): Checks if two values are not equal.
“`python
result = (5 != 3) # True
“`

– `>` (Greater than): Checks if the left value is greater than the right.
“`python
result = (9 > 6) # True
“`

– `<` (Less than): Checks if the left value is less than the right. ```python result = (3 < 8) # True ``` - `>=` (Greater than or equal to): Checks if the left value is greater than or equal to the right.
“`python
result = (9 >= 9) # True
“`

– `<=` (Less than or equal to): Checks if the left value is less than or equal to the right. ```python result = (7 <= 8) # True ``` ### 3. Logical Operators Logical operators are used to combine conditional statements: - `and`: Returns `True` if both statements are true. ```python result = (5 > 3) and (8 > 6) # True
“`

– `or`: Returns `True` if one of the statements is true.
“`python
result = (5 < 3) or (8 > 6) # True
“`

– `not`: Reverses the result, returns `False` if the result is true.
“`python
result = not(5 > 3) # False
“`

### 4. Assignment Operators
Assignment operators are used to assign values to variables. These include:

– `=`: Assigns the right value to the left variable.
“`python
x = 5
“`

– `+=`: Adds the right operand to the left operand and assigns the result to the left operand.
“`python
x += 3 # x = x + 3; result is 8
“`

– `-=`: Subtracts the right operand from the left operand and assigns the result to the left operand.
“`python
x -= 2 # x = x – 2; result is 3
“`

– `*=`: Multiplies the right operand by the left operand and assigns the result to the left operand.
“`python
x *= 5 # x = x * 5; result is 25
“`

– `/=`: Divides the left operand by the right operand and assigns the result to the left operand.
“`python
x /= 5 # x = x / 5; result is 1.0
“`

– `//=`: Performs floor division and assigns to left operand.
“`python
x //= 2 # x = x // 2
“`

– `%=`: Computes modulus and assigns to left operand.
“`python
x %= 2 # x = x % 2
“`

– `**=`: Raises to power and assigns to the left operand.
“`python
x **= 3 # x = x ** 3
“`

### 5. Identity Operators
Identity operators check if two variables reference the same object:

– `is`: Returns `True` if both variables point to the same object.
“`python
x = [1, 2, 3]
y = x
result = (x is y) # True
“`

– `is not`: Returns `True` if the variables do not point to the same object.
“`python
y = [1, 2, 3]
result = (x is not y) # True
“`

### 6. Membership Operators
Membership operators check for membership within a sequence.

– `in`: Returns `True` if the specified element is present.
“`python
result = ‘a’ in ‘banana’ # True
“`

– `not in`: Returns `True` if the specified element is not present.
“`python
result = ‘d’ not in ‘banana’ # True
“`

### Operator Precedence
Operator precedence determines the order in which operations are performed. Higher precedence operators are evaluated before lower precedence ones. For instance:

“`python
result = 2 + 3 * 4 # 14, multiplication * has higher precedence than addition +
“`

### Short-Circuiting
Logical operators `and` and `or` employ short-circuiting. For `and`, if the first operand is `False`, the second operand is not evaluated. For `or`, if the first operand is `True`, the second operand is not evaluated:

“`python
def check():
print(“Check called”)
return True

result = False and check() # “Check called” is not printed
result = True or check() # “Check called” is not printed
“`

### Real-World Applications

1. **Filtering Data:**
Logical operators can be used to filter data, such as filtering a list of numbers for even ones:
“`python
numbers = [1, 2, 3, 4, 5, 6]
evens = [n for n in numbers if n % 2 == 0]
“`

2. **Comparisons in Loops:**
Use comparison operators to iterate over a collection:
“`python
for num in numbers:
if num > 3:
print(num) # Prints numbers greater than 3
“`

3. **Simple Calculations:**
Use arithmetic operators to perform calculations in applications like a basic calculator:
“`python
def add(a, b):
return a + b

total = add(10, 20) # 30
“`

By mastering these operators and understanding their precedence and behavior, you’ll be equipped to write more efficient and effective Python code.

Scroll to Top