Python Operators: Arithmetic, Comparison, Logical, Assignment & More

1. What are arithmetic operators in Python?

Q: What are arithmetic operators?

Arithmetic operators perform mathematical operations on numeric values (int, float).

Operators:

Use Case: Calculating totals, discounts, or exponential growth.

2. Can you give an example of arithmetic operators?

# Arithmetic operators example
a = 10
b = 3

# Perform operations
addition = a + b
subtraction = a - b
multiplication = a * b
division = a / b
floor_division = a // b
modulus = a % b
exponentiation = a ** 2

# Display results
print(f"Addition: {a} + {b} = {addition}")
print(f"Subtraction: {a} - {b} = {subtraction}")
print(f"Multiplication: {a} * {b} = {multiplication}")
print(f"Division: {a} / {b} = {division}")
print(f"Floor Division: {a} // {b} = {floor_division}")
print(f"Modulus: {a} % {b} = {modulus}")
print(f"Exponentiation: {a} ** 2 = {exponentiation}")
  

Output:

Addition: 10 + 3 = 13
Subtraction: 10 - 3 = 7
Multiplication: 10 * 3 = 30
Division: 10 / 3 = 3.3333333333333335
Floor Division: 10 // 3 = 3
Modulus: 10 % 3 = 1
Exponentiation: 10 ** 2 = 100
  

Note:

3. What are comparison operators in Python?

Q: What are comparison operators?

Comparison operators compare two values and return a boolean (True or False).

Operators:

Use Case: Validating conditions, filtering data, or controlling program flow.

4. Can you give an example of comparison operators?

# Comparison operators example
x = 15
y = 10

# Perform comparisons
equal = x == y
not_equal = x != y
greater = x > y
less = x < y
greater_equal = x >= y
less_equal = x <= y

# Display results
print(f"{x} == {y}: {equal}")
print(f"{x} != {y}: {not_equal}")
print(f"{x} > {y}: {greater}")
print(f"{x} < {y}: {less}")
print(f"{x} >= {y}: {greater_equal}")
print(f"{x} <= {y}: {less_equal}")
  

Output:

15 == 10: False
15 != 10: True
15 > 10: True
15 < 10: False
15 >= 10: True
15 <= 10: False
  

Note:

5. What are logical operators in Python?

Q: What are logical operators?

Logical operators combine boolean expressions to evaluate complex conditions.

Operators:

Use Case: Combining conditions in decision-making or filtering.

6. Can you give an example of logical operators?

# Logical operators example
age = 25
salary = 60000

# Combine conditions
can_promote = age > 21 and salary >= 50000
can_transfer = age < 30 or salary > 70000
not_eligible = not (salary >= 50000)

# Display results
print(f"Can promote (age > 21 and salary >= 50000): {can_promote}")
print(f"Can transfer (age < 30 or salary > 70000): {can_transfer}")
print(f"Not eligible (not salary >= 50000): {not_eligible}")
  

Output:

Can promote (age > 21 and salary >= 50000): True
Can transfer (age < 30 or salary > 70000): True
Not eligible (not salary >= 50000): False
  

Note:

7. What are assignment operators in Python?

Q: What are assignment operators?

Assignment operators assign values to variables, often combining arithmetic or bitwise operations with assignment.

Operators:

Use Case: Updating variables efficiently in loops or calculations.

8. Can you give an example of assignment operators?

# Assignment operators example
x = 10  # Basic assignment

# Compound assignments
x += 5  # x = x + 5
print(f"After +=: {x}")

x -= 3  # x = x - 3
print(f"After -=: {x}")

x *= 2  # x = x * 2
print(f"After *=: {x}")

x /= 4  # x = x / 4
print(f"After /=: {x}")

x //= 2  # x = x // 2
print(f"After //=: {x}")

x %= 3  # x = x % 3
print(f"After %=: {x}")

x **= 2  # x = x ** 2
print(f"After **=: {x}")
  

Output:

After +=: 15
After -=: 12
After *=: 24
After /=: 6.0
After //=: 3.0
After %=: 0.0
After **=: 0.0
  

Note:

9. What are membership and identity operators?

Q: What are membership operators?

in and not in test if a value exists in a sequence (list, tuple, string, etc.).

Q: What are identity operators?

is and is not check if two variables refer to the same object in memory (object identity).

Q: What is operator precedence?

Python evaluates operators in a specific order (e.g., parentheses > ** > * / // % > + - > comparisons > not > and > or). Use parentheses for clarity.