Tech With Galvan
Courses/Python Mastery/Variables & Data Types

πŸ§ͺ Practice: Variables & Types

25 min β€’ Beginner β€’ Module 3

8 exercises: swap variables, build a mini bio card, convert types, and fix type errors.

πŸ§ͺ 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 5

Exercise 2 β€” The receipt

Given these values, print a formatted receipt exactly like the sample using f-strings:

item = "Notebook"
price = 149.5
qty = 3

Expected 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.00

Exercise 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:

  1. age = int(input("Age: ")) β€” input returns a string
  2. print("Total: " + str(100)) or better: print(f"Total: {100}")
  3. price = float("12.55") β€” β€œ12.5.5” isn’t a valid number

Exercise 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 int

Exercise 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.