Courses/Python Mastery/Module 3: Variables & Data Types
Module 3 ยท Lesson 725 minBeginner๐Ÿงช Practice Session

๐Ÿงช Practice: Variables & Types

Lesson goal
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

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

code
item = "Notebook"
price = 149.5
qty = 3

Expected output:

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

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

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

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

code
Bill: 850
Tip %: 10
Tip: โ‚น85.00
Total: โ‚น935.00

Exercise 7 โ€” Type X-ray

For each value, write type(value)'s output:

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

code
[name] went to [place] and bought [number] samosas.
The shopkeeper said: "That will be [number ร— 15] rupees!"

Solutions

Exercise 1:

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

code
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 string
  • print("Total: " + str(100)) or better: print(f"Total: {100}")
  • price = float("12.55") โ€” "12.5.5" isn't a valid number
  • Exercise 4:

    code
    total = int(input("Seconds: "))
    hours = total // 3600
    minutes = (total % 3600) // 60
    seconds = total % 60
    print(f"{hours} hours, {minutes} minutes, {seconds} seconds")

    Exercise 5:

    code
    False    # "10" (string) != 10 (number) โ€” different types
    True     # after conversion, values match
    TypeError!  # you can't + a string and an int

    Exercise 6:

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

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