๐งช Practice: Variables & Types
Module 3 in your fingers, not just your head. Eight exercises, solutions at the bottom โ attempt honestly first.
Exercise 1 โ Swap without a third variable
a = 5
b = 10
# swap a and b (any method you like)
print(a, b) # should print: 10 5Exercise 2 โ The receipt
Given these values, print a formatted receipt exactly like the sample using f-strings:
item = "Notebook"
price = 149.5
qty = 3Expected output:
Item: Notebook
Total: โน448.50*(Hint: price * qty, then format to 2 decimals with :.2f)*
Exercise 3 โ Fix the crashes
Each line has one problem. Fix all three:
1. age = input("Age: ")
print(age + 1)
2. print("Total: " + 100)
3. price = float("12.5.5")Exercise 4 โ Seconds converter
Ask the user for a number of seconds, then convert it to hours, minutes, and seconds.
Input: 3671
Output: 1 hours, 1 minutes, 11 seconds*(Hints: // for hours and minutes, % for leftovers. 3671 // 3600 = 1 hour.)*
Exercise 5 โ Truth detector
Predict the output before running:
x = "10"
y = 10
print(x == y)
print(int(x) == y)
print(x + y)Exercise 6 โ The tip calculator
Ask for a bill amount and a tip percentage. Print the tip and the total, formatted to 2 decimals:
Bill: 850
Tip %: 10
Tip: โน85.00
Total: โน935.00Exercise 7 โ Type X-ray
For each value, write type(value)'s output:
5 โ ?
5.0 โ ?
"5" โ ?
5 == 5 โ ?
int("5") โ ?Exercise 8 โ Mad Libs (the fun one)
Ask the user for: a name, a place, and a number. Print this story with their words:
[name] went to [place] and bought [number] samosas.
The shopkeeper said: "That will be [number ร 15] rupees!"Solutions
Exercise 1:
a = 5
b = 10
a, b = b, a # Python's simultaneous swap โ the elegant way
print(a, b) # 10 5(The classic three-line method with a temp variable works too; the tuple swap is the Pythonic one.)
Exercise 2:
item = "Notebook"
price = 149.5
qty = 3
total = price * qty
print(f"Item: {item}")
print(f"Total: โน{total:.2f}")Exercise 3:
age = int(input("Age: ")) โ input returns a stringprint("Total: " + str(100)) or better: print(f"Total: {100}")price = float("12.55") โ "12.5.5" isn't a valid numberExercise 4:
total = int(input("Seconds: "))
hours = total // 3600
minutes = (total % 3600) // 60
seconds = total % 60
print(f"{hours} hours, {minutes} minutes, {seconds} seconds")Exercise 5:
False # "10" (string) != 10 (number) โ different types
True # after conversion, values match
TypeError! # you can't + a string and an intExercise 6:
bill = float(input("Bill: "))
tip_pct = float(input("Tip %: "))
tip = bill * tip_pct / 100
print(f"Tip: โน{tip:.2f}")
print(f"Total: โน{bill + tip:.2f}")Exercise 7: <class 'int'>, <class 'float'>, <class 'str'>, <class 'bool'>, <class 'int'>.
Exercise 8:
name = input("Name: ")
place = input("Place: ")
count = int(input("Number of samosas: "))
print(f"{name} went to {place} and bought {count} samosas.")
print(f'The shopkeeper said: "That will be {count * 15} rupees!"')โ Module 3 Checkpoint
Eight done? Module 3 complete. You now hold the raw materials of every program: variables, numbers, strings, booleans, conversions, and conversation with the user.
Next module: Operators โ every symbol that does work, in one focused sweep.