Python practice: Advanced Functions, Scope & Arguments
Chapter 14 · 8 questions · included in full access
Question 1 · easy
Function with default discount argument
Solve it in your browserDefine
discount_price(price, discount=0.05)returninground(price * (1 - discount), 2). Printdiscount_price(1000)on line 1, anddiscount_price(1000, 0.15)on line 2:Question 2 · easy
Explicit keyword arguments
Solve it in your browserDefine
payout(base, comm, bonus=0)returningbase + comm + bonus. Call it withbase=50000, comm=12000, bonus=3000and print:Question 3 · easy
Sum arbitrary numbers with *args
Solve it in your browserDefine
total_sales(*figures)that returnssum(figures). Call with1200, 3400, 2100and print the total:Question 4 · medium
Pass configuration dictionary with **kwargs
Solve it in your browserDefine
format_event(event, **metadata)returningf"{event}: {len(metadata)} details". Call withformat_event("LOGIN", user="Asha", role="Admin")and print:Question 5 · easy
Local vs global variable scope
Solve it in your browserCreate global
tax_code = "GST-18". Defineget_tag(id)that creates localtag = f"{tax_code}-{id}"and returnstag. Printget_tag(101):Question 6 · easy
One-line lambda calculation
Solve it in your browserCreate a lambda function
gst_tax = lambda x: round(x * 0.18, 2). Call it with2500and print the result:Question 7 · medium
Sort list of tuples by price with lambda
Solve it in your browserGiven
items = [("Desk", 4500), ("Chair", 2200), ("Lamp", 850)], sort by price usingitems.sort(key=lambda x: x[1]). Printitems:Question 8 · easy
Configurable multiplier with default
Solve it in your browserDefine
scale(val, factor=2)returningval * factor. Printscale(10)on line 1, andscale(10, factor=5)on line 2:
These questions are part of full access.
The first chapters of every track are free. Full access is a one-time payment.
See pricing