Python operators: Solutions to the exercises
Hi, team! This document shows you exactly how to solve those Python operator exercises you
received. Work through each one step-by-step - don't just copy the code!
1. Arithmetic Operators (+, -, *, /, //, %, **)
First exercise: You need to take two numbers and show their sum, difference, product, and floor
division.
Step 1: What do these words mean?
• Sum = addition (+)
• Difference = subtraction (-)
• Product = multiplication (*)
• Floor division = divide and round DOWN (//). Remember: 7 // 2 = 3, not 3.5!
Step 2: What do you need?
• Two numbers from the user
• Four calculations
• Four print statements
Here's how you do it:
# Get two numbers from user
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
# Do the math (you should remember a + b gives sum!)
sum_result = a + b
diff_result = a - b
prod_result = a * b
floor_result = a // b
# Show the answers
print("Sum:", sum_result)
print("Difference:", diff_result)
1
print("Product:", prod_result)
print("Floor division:", floor_result)
Quick check: If you enter 10 and 3, you should get:
Sum: 13, Difference: 7, Product: 30, Floor division: 3
Exercise 2: Square and cube using:
Square = n ** 2 and Cube = n ** 3
n = int(input("Enter a number: "))
print("Square:", n ** 2)
print("Cube:", n ** 3)
Exercise 3: Minutes to hours and minutes
Hours = total // 60, Remaining = total % 60
total_minutes = int(input("Enter total minutes: "))
hours = total_minutes // 60
minutes = total_minutes % 60
print(hours, "hours and", minutes, "minutes")
2. Comparison Operators (==, !=, >, <, >=, <=)
These give you True or False and are super useful for decisions!
Exercise 1: Check if two numbers are equal
To do this, we use == (that's two equals signs!).
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
if a == b:
print("Numbers are equal!")
else:
print("Numbers are different!")
2
Exercise 2: Voting age (18+)
You should remember: ">= 18" means "18 or older".
age = int(input("What's your age? "))
if age >= 18:
print("You can vote!")
else:
print("Too young to vote!")
Exercise 3: Which string is longer?
Use len() to get string length, then compare.
s1 = input("Enter first string: ")
s2 = input("Enter second string: ")
if len(s1) > len(s2):
print("First string is longer!")
elif len(s2) > len(s1):
print("Second string is longer!")
else:
print("Same length!")
3. Logical operators (and, or, not)
Exercise 1: Number between 10-50 inclusive
"Between 10 and 50" means >= 10 AND <= 50.
n = int(input("Enter a number: "))
if n >= 10 and n <= 50:
print("In range!")
else:
print("Out of range!")
Exercise 2: Age 18+ AND logged in
Two conditions must BOTH be true.
age = int(input("Your age? "))
logged_in = input("Logged in? (yes/no): ").lower() == "yes"
3
if age >= 18 and logged_in:
print("Access granted!")
else:
print("Access denied!")
Importantly, [.lower()] makes "Yes" → "yes"
Exercise 3: Toggle with not
not True becomes False.
light_on = True
print("Light was:", light_on)
light_on = not light_on
print("Light is now:", light_on)
4. Assignment operators (=, +=, -=, *=, /=, etc.)
x += 5 means x = x + 5.
Exercise 1: Start with x = 5, add 10
x = 5
x += 10 # Now x = 15
print(x)
Exercise 2: Double until over 100
n = 1
while n <= 100:
print(n)
n *= 2 # Double it!
Exercise 3: Seconds to minutes
seconds = int(input("Total seconds: "))
minutes = seconds // 60
4
seconds %= 60
print(minutes, "minutes,", seconds, "seconds")
5. Identity & membership (is, in, not in, etc.)
Exercise 1: Use is
a = [1, 2, 3]
b = a # Same list object
c = [1, 2, 3] # Different object, same contents
print(a is b) # True
print(a is c) # False
Exercise 2: Character in string
text = input("Enter text: ")
char = input("Enter character: ")
if char in text:
print("Found it!")
else:
print("Not found!")
Exercise 3: Number in list
numbers = [2, 4, 6, 8]
n = int(input("Enter number: "))
if n in numbers:
print("Number exists!")
else:
print("Number not found!")
5
6. Bitwise Operators (&, |, ^, ~, <<, >>)
First, the binary trick: Convert to binary, then operate bit-by-bit.
Exercise 1: a=5 (101), b=3 (011)
text
5: 101
3: 011
AND: 001 = 1 (&)
OR: 111 = 7 (|)
XOR: 110 = 6 (^)
python
a, b = 5, 3
print("a & b =", a & b) # 1
print("a | b =", a | b) # 7
print("a ^ b =", a ^ b) # 6
Exercise 2: ~a flips all bits
a = 4
print("a =", a, bin(a))
print("~a =", ~a, bin(~a))
Exercise 3: Shifts
n = 10
print("<<1:", n << 1) # 20
print("<<2:", n << 2) # 40
print(">>1:", n >> 1) # 5
print(">>2:", n >> 2) # 2
Exercise 4: Check 3rd bit (index 2)
Mask = 1 << 2 = binary 100
num = int(input("Enter number: "))
mask = 1 << 2 # 3rd bit from right
6
if num & mask:
print("3rd bit is SET!")
else:
print("3rd bit is OFF!")
Happy coding! Feel free to ask for assistance in the WhatsApp
group. We’re always willing to help.