Skip to main content

Phase 2 🧱 — Foundations

Topics: Variables, data types, operators, string manipulation, type casting

Build on the basics by working with different data types, performing operations, and transforming data from one type to another.


🔄 Exercise Flow


📚 Prerequisites

Before starting these exercises, make sure you've read:


🔰 Starter: Shopping Cart Calculator

Time: 12 minutes

Write a program that calculates the total cost of items in a shopping cart, applying a discount and displaying a receipt.

Learning Objectives

  • Declare variables of different types (int, float, str)
  • Use arithmetic operators (+, *, /)
  • Use f-strings for formatted output
  • Cast types using str() and float()

Starter Code

# Starter: Shopping Cart Calculator

# Item prices (floats)
item1_price = 12.99
item2_price = 5.49
item3_price = 8.00

# Quantities (ints)
item1_qty = 2
item2_qty = 1
item3_qty = 3

# TODO: Calculate subtotal for each item (price * quantity)
# TODO: Calculate the total before discount
# TODO: Apply a 10% discount to get the final total
# TODO: Print a receipt with all values formatted to 2 decimal places

# Your code here 👇

Expected Output

=== SHOPPING RECEIPT ===
Item 1: 2 x $12.99 = $25.98
Item 2: 1 x $5.49 = $5.49
Item 3: 3 x $8.00 = $24.00
------------------------
Subtotal: $55.47
Discount (10%): -$5.55
Total: $49.92

⭐ Medium: Data Type Detective

Time: 20 minutes

Write a program that analyses user input, identifies each value's data type, and performs appropriate conversions.

Learning Objectives

  • Get user input with input()
  • Detect types with type() and isinstance()
  • Convert between types (int(), float(), str(), bool())
  • Handle edge cases in type conversion

Starter Code

# Medium: Data Type Detective

# TODO: Take 4 different inputs from the user (a number, a decimal, a word, and anything else)
# TODO: For each input, print:
# - The original value
# - Its type using type()
# - Whether it can be converted to int, float, or bool
# TODO: Attempt each conversion and print the result (or "Cannot convert" if it fails)

# Starter — get inputs
user_inputs = []

print("Enter 4 different values (press Enter after each):")
for i in range(4):
value = input(f"{i+1}. ")
user_inputs.append(value)

# Your code here 👇

Expected Output

--- Analysis ---
Input: "42"
Original type: <class 'str'>
→ int(42): 42
→ float(42): 42.0
→ bool(42): True

Input: "hello"
Original type: <class 'str'>
→ int(hello): Cannot convert
→ float(hello): Cannot convert
→ bool(hello): True

🏆 Hard: Personal Finance Tracker

Time: 30 minutes

Build a personal finance tracker that manages income, expenses, and savings using variables, calculations, string formatting, and type conversions.

Learning Objectives

  • Design a complete mini-program from a specification
  • Use all major data types (int, float, str, bool)
  • Perform complex string formatting and manipulation
  • Write clean, well-commented code

Starter Code

# Hard: Personal Finance Tracker

# --- User Profile ---
name = "Jamie"
age = 28
employed = True
monthly_salary = 4800.00

# --- Monthly Expenses ---
rent = 1400.00
groceries = 450.00
utilities = 185.50
transport = 120.00
entertainment = 200.00
misc = 150.00

# TODO 1: Calculate total expenses, savings (salary - expenses)
# TODO 2: Calculate savings rate as a percentage (2 decimal places)
# TODO 3: Calculate yearly projection (savings * 12) and 5-year projection
# TODO 4: Check if savings rate >= 20% → print "Great job!" else print suggestion
# TODO 5: Create a summary string with all numbers formatted nicely
# TODO 6: Print a full financial report with borders and alignment

# Your code here 👇

Expected Output

═══════════════════════════════════════
PERSONAL FINANCE REPORT
Name: Jamie (Age: 28)
═══════════════════════════════════════
Income
Monthly Salary: $4,800.00
───────────────────────────────────────
Expenses
Rent: $1,400.00
Groceries: $450.00
Utilities: $185.50
Transport: $120.00
Entertainment: $200.00
Misc: $150.00
───────────────────────────────────────
Total Expenses: $2,505.50
───────────────────────────────────────
Savings
Monthly Savings: $2,294.50
Savings Rate: 47.80%
───────────────────────────────────────
Projections
1 Year: $27,534.00
5 Years: $137,670.00
───────────────────────────────────────
Status: ✅ Great job! You save 47.8% of your income.
═══════════════════════════════════════

💡 Tips for Success

  • Use type() freely to check what type a value actually is — it will save you from many bugs.
  • String multiplication "=" * 40 is great for creating borders.
  • When converting user input, always consider that it might fail — errors are learning opportunities.
  • f-strings can evaluate expressions: f"{price * qty:.2f}" formats the result inline.

← Back to Exercises