Argument Labels and Defaults
Lesson 23 of 48 in Coddy's Python for Swift Developers course.
Swift distinguishes external argument labels from internal parameter names. Python keyword calls normally use parameter names directly, unless the signature declares positional-only parameters. Positional and keyword values cannot bind the same parameter twice. Python defaults are evaluated when the function is defined, not on every omitted-argument call as in Swift. Mutable defaults therefore persist across calls; use None plus a fresh allocation inside when independent state is needed.
Swift example
func cost(_ price: Int, quantity: Int = 1) -> Int { price * quantity }
print(cost(4))
print(cost(4, quantity: 3))Python
def cost(price, quantity=1):
return price * quantity
print(cost(4))
print(cost(quantity=3, price=4))Porting trap
Keyword argument reordering is supported in ordinary Python calls, but an explicit Swift-like external label mapping is not part of a normal def signature.
Implement the specified API in the editable Python files. The locked driver.py runs the program and supplies input/output. Keep its imports and names unchanged.
Challenge
BeginnerImplement line_cost(price, quantity=1, discount=0). Return price * quantity - discount as an integer. Inputs are nonnegative integers and the discount never exceeds the product. Do not print or read input. The locked driver reads price, quantity, and discount on three lines. It calls your function with just price, with two positional arguments, and with all arguments named in a different order. It prints the three results on separate lines.
Your function definitions go in functions.py. The locked driver.py imports them and handles the supplied input/output and checks. You do not need to edit the driver.
Try it yourself
from functions import line_cost
price = int(input())
quantity = int(input())
discount = int(input())
print(line_cost(price))
print(line_cost(price, quantity))
print(line_cost(discount=discount, price=price, quantity=quantity))
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Python for Swift Developers
1From Swift to Python
Run Your First Python ProgramPrinting and Stringslet, var, and RebindingInt Range and DivisionIndentation and Block ScopeRecap: Rewrite a Receipt4Iteration and Transformations
for and Half-Open Rangesenumerated to enumeratewhile and Loop Controlmap, filter, ComprehensionsRecap: Build a Ranking2Values and Decisions
Optionals and NoneEquatable and IdentityBool Conditions and FallbacksBranches and TernariesRecap: Eligibility Rules5Functions and Closures
Functions and Explicit ReturnArgument Labels and DefaultsVariadics and Call ExpansionCapture Lists and DefaultsRecap: Refactor a Pricing Tool3Value Types to Shared Objects
Array to listTuples and UnpackingDictionary Lookup and OrderValue Copies and AliasesRecap: Clean an Inventory6Errors and Modules
Input and String Formattingthrows, defer, and withModules and Main GuardsHints and Runtime TypesRecap: Validate a Batch9Project: Order Processing
Create the Order ModelAdd Pricing PoliciesPractice on your own: Online Python compiler