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:
+: Addition (e.g.,5 + 3→ 8).-: Subtraction (e.g.,5 - 3→ 2).*: Multiplication (e.g.,5 * 3→ 15)./: Division (returns float, e.g.,5 / 2→ 2.5).//: Floor division (returns int, e.g.,5 // 2→ 2).%: Modulus (remainder, e.g.,5 % 2→ 1).**: Exponentiation (e.g.,2 ** 3→ 8).
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:
- Division (
/) always returns afloat; floor division (//) returns anint. - Modulus (
%) is useful for checking divisibility.
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:
==: Equal to (e.g.,5 == 5→True).!=: Not equal to (e.g.,5 != 3→True).>: Greater than (e.g.,5 > 3→True).<: Less than (e.g.,5 < 3→False).>=: Greater than or equal to (e.g.,5 >= 5→True).<=: Less than or equal to (e.g.,3 <= 5→True).
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:
- Comparison operators work with numeric types, strings (lexicographical order), and other comparable types.
- Useful in conditional statements (
if,while).
5. What are logical operators in Python?
Q: What are logical operators?
Logical operators combine boolean expressions to evaluate complex conditions.
Operators:
and:Trueif both operands areTrue(e.g.,True and False→False).or:Trueif at least one operand isTrue(e.g.,True or False→True).not: Negates the operand (e.g.,not True→False).
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:
andevaluates toTrueonly if both conditions areTrue.orshort-circuits if the first condition isTrue.notinverts the boolean value.
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:
=: Assign value (e.g.,x = 5).+=: Add and assign (e.g.,x += 3→x = x + 3).-=: Subtract and assign (e.g.,x -= 3).*=,/=,//=,%=,**=: Multiply, divide, floor divide, modulus, exponentiate and assign.&=,|=,^=,<<=,>>=: Bitwise and assign (covered in bitwise 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:
- Compound operators (
+=, etc.) are concise and efficient. - Division (
/=) returnsfloat; floor division (//=) may returnintorfloat.
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.