← Back to Chapters

Python Operators Exercises

? Python Operators Exercises

⚡ Quick Overview

Python operators are symbols that let you perform actions on values and variables. In this topic, you will practice using four major groups of operators: arithmetic, comparison, logical, and assignment. Mastering these will help you write clear conditions, perform calculations, and update values efficiently.

? Key Concepts

  • Arithmetic – Perform basic math like addition, subtraction, multiplication, and division.
  • Comparison – Compare two values and get a Boolean result (True or False).
  • Logical – Combine or invert Boolean values using and, or, and not.
  • Assignment – Assign and update values stored in variables in a compact way.

? Syntax and Theory

? Arithmetic Operators

Used for mathematical operations on numbers:

  • + – addition
  • - – subtraction
  • * – multiplication
  • / – division (always returns a float)
  • % – modulus (remainder of a division)
  • ** – exponentiation
  • // – floor division (quotient without decimal part)

? Comparison Operators

Used to compare two values; the result is either True or False:

  • == – equal to
  • != – not equal to
  • < – less than
  • > – greater than
  • <= – less than or equal to
  • >= – greater than or equal to

? Logical Operators

Used to combine Boolean expressions:

  • andTrue only if both conditions are True.
  • orTrue if at least one condition is True.
  • not – inverts the Boolean value.

? Assignment Operators

Used to assign and update values stored in variables:

  • = – simple assignment
  • +=, -=, *=, /= – update and assign in a single step
  • %=, //=, **= – apply modulus, floor division, exponentiation and reassign

? Code Examples

? Arithmetic Operators Example

Basic mathematical operations on integers:

? View Code Example
a = 10
b = 3

print(a + b)  # 13
print(a - b)  # 7
print(a * b)  # 30
print(a / b)  # 3.3333...
print(a % b)  # 1
print(a ** b) # 1000
print(a // b) # 3

? Comparison Operators Example

Comparing two numbers using different comparison operators:

? View Code Example
x = 5
y = 10

print(x == y)  # False
print(x != y)  # True
print(x < y)  # True
print(x > y)  # False
print(x <= y) # True
print(x >= y) # False

? Logical Operators Example

Combining Boolean values with logical operators:

? View Code Example
a = True
b = False

print(a and b)  # False
print(a or b)   # True
print(not a)    # False

? Assignment Operators Example

Updating the same variable using different assignment operators:

? View Code Example
num = 5
num += 3  # num = 8
num -= 2  # num = 6
num *= 2  # num = 12
num /= 4  # num = 3.0
num %= 2  # num = 1.0
num **= 3 # num = 1.0
num //= 1 # num = 1.0

? Output and Explanation

? Understanding the Results

  • Arithmetic: a + b, a - b, a * b and a / b behave like normal math, but division gives a float. a // b drops the decimal part.
  • Comparison: Each comparison prints True or False. For example, 5 < 10 is True and 5 > 10 is False.
  • Logical: a and b is only True when both are True, a or b is True if at least one is True, and not a flips the value of a.
  • Assignment: The variable num is changed step-by-step. Each operator both performs the operation and updates num in place.

? Use Cases

  • Building calculators or billing systems using arithmetic and assignment operators.
  • Writing conditions in if-statements and loops using comparison operators.
  • Creating complex validation rules (for input, authentication, etc.) using logical operators.
  • Updating counters and scores in games or simulations using assignment operators.

✅ Tips and Best Practices

  • Use arithmetic operators for clear and simple mathematical calculations.
  • Prefer comparison operators in if-conditions instead of manually checking values using multiple statements.
  • Use logical operators to combine multiple comparisons instead of nesting many if-statements.
  • Use compound assignment operators like += for counters (e.g., i += 1 in loops).
  • Add short comments next to tricky expressions to explain what the expression is checking.

? Try It Yourself

  • Write a program that uses arithmetic operators to calculate the area and perimeter of a rectangle.
  • Ask the user for two numbers, compare them using comparison operators, and print which one is greater or if they are equal.
  • Use logical operators to check if a number entered by the user is both positive and even.
  • Start with a variable (e.g., score = 0) and update it in multiple steps using different assignment operators.
  • Create a small “exam pass checker” that uses comparison and logical operators to decide if a student passed based on marks in three subjects.