Python practice: Advanced Functions, Scope & Arguments

Chapter 14 · 8 questions · included in full access

  1. Question 1 · easy

    Function with default discount argument

    Define discount_price(price, discount=0.05) returning round(price * (1 - discount), 2). Print discount_price(1000) on line 1, and discount_price(1000, 0.15) on line 2:

    Solve it in your browser
  2. Question 2 · easy

    Explicit keyword arguments

    Define payout(base, comm, bonus=0) returning base + comm + bonus. Call it with base=50000, comm=12000, bonus=3000 and print:

    Solve it in your browser
  3. Question 3 · easy

    Sum arbitrary numbers with *args

    Define total_sales(*figures) that returns sum(figures). Call with 1200, 3400, 2100 and print the total:

    Solve it in your browser
  4. Question 4 · medium

    Pass configuration dictionary with **kwargs

    Define format_event(event, **metadata) returning f"{event}: {len(metadata)} details". Call with format_event("LOGIN", user="Asha", role="Admin") and print:

    Solve it in your browser
  5. Question 5 · easy

    Local vs global variable scope

    Create global tax_code = "GST-18". Define get_tag(id) that creates local tag = f"{tax_code}-{id}" and returns tag. Print get_tag(101):

    Solve it in your browser
  6. Question 6 · easy

    One-line lambda calculation

    Create a lambda function gst_tax = lambda x: round(x * 0.18, 2). Call it with 2500 and print the result:

    Solve it in your browser
  7. Question 7 · medium

    Sort list of tuples by price with lambda

    Given items = [("Desk", 4500), ("Chair", 2200), ("Lamp", 850)], sort by price using items.sort(key=lambda x: x[1]). Print items:

    Solve it in your browser
  8. Question 8 · easy

    Configurable multiplier with default

    Define scale(val, factor=2) returning val * factor. Print scale(10) on line 1, and scale(10, factor=5) on line 2:

    Solve it in your browser

These questions are part of full access.

The first chapters of every track are free. Full access is a one-time payment.

See pricing