About These Assignments
This page contains 50+ practice assignments covering all Python operator types. Each problem is designed to:
- Reinforce your understanding of Python operators
- Apply concepts to real-world scenarios
- Build problem-solving skills step by step
- Prepare you for coding interviews
π‘ Tip: Try solving each problem on your own first. Click the "Show Solution" button only after you've attempted the problem. This approach will help you learn more effectively.
1. Arithmetic Operators 10 Questions
item2 = 350
item3 = 450
total = item1 + item2 + item3
print(f"Total cost: βΉ{total}") # Output: βΉ1050
We use the + operator to add all three item prices together. The result is stored in the total variable and displayed using an f-string. This is a common pattern in e-commerce applications.
width = 8
area = length * width
print(f"Area: {area} square meters") # Output: 96
The multiplication operator * multiplies length and width to calculate the area. This formula is used in architecture, interior design, and real estate applications.
friends = 5
each = apples // friends # Floor division for equal share
leftover = apples % friends # Modulus for remainder
print(f"Each friend gets: {each} apples")
print(f"Apples left over: {leftover}")
# Output: Each friend gets: 5 apples
# Apples left over: 2
The floor division operator // gives the number each person gets (quotient), while the modulus operator % gives the remainder. This pattern is used in inventory management and resource allocation.
BMI = weight / heightΒ².
height = 1.75
bmi = weight / (height ** 2)
print(f"BMI: {bmi:.2f}") # Output: 22.86
We use the exponentiation operator ** to square the height, then divide the weight by that value. The :.2f formats the output to 2 decimal places. This is used in health, fitness, and medical applications.
SI = P Γ R Γ T / 100.
rate = 8
time = 3
simple_interest = (principal * rate * time) / 100
print(f"Simple Interest: βΉ{simple_interest:.2f}") # Output: βΉ2400.00
We multiply principal, rate, and time using *, then divide by 100 using /. This demonstrates how multiple arithmetic operators work together in a single expression, commonly used in banking and finance applications.
F = (C Γ 9/5) + 32.
fahrenheit = (celsius * 9/5) + 32
print(f"{celsius}Β°C = {fahrenheit}Β°F") # Output: 25Β°C = 77.0Β°F
We use * and / to multiply by 9/5, then + to add 32. This demonstrates operator precedence β multiplication and division happen before addition. This is used in weather apps, scientific applications, and international travel.
CI = P Γ (1 + R/100)^T - P.
r = 8
t = 3
amount = p * (1 + r/100) ** t
compound_interest = amount - p
print(f"Compound Interest: βΉ{compound_interest:.2f}") # Output: βΉ2597.12
We use ** for exponentiation, which has higher precedence than * and /. The expression (1 + r/100) ** t calculates the growth factor. This demonstrates the power of combining multiple operators for complex financial calculations.
d = β((xβ - xβ)Β² + (yβ - yβ)Β²).
x1, y1 = 3, 4
x2, y2 = 7, 1
distance = math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
print(f"Distance: {distance:.2f}") # Output: 5.00
We use - for difference, ** for squaring, + for addition, and math.sqrt() for square root. This demonstrates combining arithmetic operators with built-in functions, used in GPS systems, gaming, and graphics applications.
2. Assignment Operators 6 Questions
print(f"User's age: {age}") # Output: 25
The = operator assigns the value 25 to the variable age. This is the most basic and commonly used assignment operation in Python.
+= operator.
score += 15 # Equivalent to score = score + 15
print(f"New score: {score}") # Output: 65
The += operator adds 15 to the current value of score. This is a shorthand for score = score + 15 and is commonly used in gaming and counter applications.
print(f"Health: {health}, Mana: {mana}, Stamina: {stamina}")
# Output: Health: 100, Mana: 100, Stamina: 100
Chain assignment allows you to assign the same value to multiple variables in a single line. This is efficient and readable, commonly used for initializing game stats or configuration values.
3. Relational Operators 7 Questions
==.
min_age = 18
is_equal = age == min_age
print(f"Is age equal to minimum? {is_equal}") # Output: True
The == operator compares two values for equality. It returns True if they are equal, otherwise False. This is used in authentication, validation, and conditional logic.
max_score = score1
if score2 > max_score:
max_score = score2
if score3 > max_score:
max_score = score3
print(f"Highest score: {max_score}") # Output: 92
The > operator compares values to find the largest. We use an if statement to update the maximum when a larger value is found. This is commonly used in data analysis and ranking applications.
4. Logical Operators 8 Questions
and.
is_eligible = amount >= 500 and amount <= 1000
print(f"Eligible for discount: {is_eligible}") # Output: True
The and operator returns True only if both conditions are True. This is used in eligibility checks, validation, and filtering applications.
or.
is_winning = num % 3 == 0 or num % 5 == 0
print(f"Is {num} a winning number? {is_winning}") # Output: True
The or operator returns True if at least one condition is True. This is used in game logic, filtering, and decision-making systems.
5. Bitwise Operators 6 Questions
&.
WRITE = 2 # binary: 0010
EXECUTE = 4 # binary: 0100
user_permission = READ | WRITE # 3
can_read = user_permission & READ
print(f"Can read? {bool(can_read)}") # Output: True
The bitwise AND operator & checks if a specific bit is set. This is commonly used in permission systems, flag checking, and low-level programming.
is_power = n > 0 and (n & (n - 1)) == 0
print(f"Is {n} a power of 2? {is_power}") # Output: True
A power of 2 has exactly one bit set. n & (n - 1) clears the lowest set bit. If the result is 0, the number is a power of 2. This demonstrates the power of bitwise operations in performance-critical applications.
6. Special Operators 6 Questions
is.
is_none = result is None
print(f"Is result None? {is_none}") # Output: True
The is operator checks identity, not equality. It's the preferred way to check for None because None is a singleton object. This is used in database queries, API responses, and error handling.
in.
is_in_cart = "apple" in cart
print(f"Is 'apple' in cart? {is_in_cart}") # Output: True
The in operator checks if a value exists in a sequence (list, tuple, string, etc.). This is commonly used in shopping carts, user permissions, and data validation.
7. Mixed Practice 7 Questions
- First 100 units: βΉ5 per unit
- Next 100 units: βΉ7 per unit
- Above 200 units: βΉ10 per unit
if units <= 100:
bill = units * 5
elif units <= 200:
bill = 100 * 5 + (units - 100) * 7
else:
bill = 100 * 5 + 100 * 7 + (units - 200) * 10
print(f"Total bill: βΉ{bill}") # Output: βΉ1700
This problem combines relational operators (<=) for comparison, arithmetic operators (*, +, -) for calculations, and logical flow (if, elif, else) for decision-making. It's a common pattern in utility billing, tax calculation, and pricing systems.
- It is divisible by 400, OR
- It is divisible by 4 but NOT by 100
is_leap = (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0)
print(f"Is {year} a leap year? {is_leap}") # Output: True
This problem combines the modulus operator % for divisibility checks, logical operators (or, and) for combining conditions, and relational operators (==, !=) for comparison. It's a classic interview question that tests understanding of operator combinations.
Try It Yourself!
Use the interactive editor below to test your solutions or write your own code.
OPERATORS PRACTICE
========================================
7 is odd
Maximum: 25
Is 75 in range? True
β Write your solutions here!
π Related Tutorials
π’ Arithmetic Operators
Review arithmetic operators with examples
π Relational Operators
Review comparison operators with examples
π§ Logical Operators
Review logical operators with examples