Bird
Raised Fist0
Pythonprogramming~10 mins

Argument order rules in Python - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to call the function with the correct positional argument.

Python
def greet(name):
    return f"Hello, {name}!"

message = greet([1])
print(message)
Drag options to blanks, or click blank then click option'
A"Alice"
Bname
Cgreet
DHello
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Passing the function name instead of a string.
Passing a variable that is not defined.
2fill in blank
medium

Complete the function call using a keyword argument.

Python
def describe_pet(pet_name, animal_type='dog'):
    return f"I have a {animal_type} named {pet_name}."

info = describe_pet([1])
print(info)
Drag options to blanks, or click blank then click option'
Aanimal_type='Buddy'
B'Buddy'
Canimal_type='cat'
Dpet_name='Buddy'
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Passing only a string without specifying the parameter name.
Using the wrong parameter name.
3fill in blank
hard

Fix the error in the function call by placing arguments in the correct order.

Python
def make_shirt(size, message):
    return f"Shirt size: {size}, message: {message}"

result = make_shirt([1])
print(result)
Drag options to blanks, or click blank then click option'
A'Large', 'Hello World!'
B'Hello World!', 'Large'
Csize='Large', message='Hello World!'
Dmessage='Hello World!', size='Large'
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Swapping the order of positional arguments.
Mixing positional and keyword arguments incorrectly.
4fill in blank
hard

Fill both blanks to create a function call with one positional and one keyword argument.

Python
def order_drink(size, flavor):
    return f"Order: {size} {flavor} drink"

order = order_drink([1], [2])
print(order)
Drag options to blanks, or click blank then click option'
A'Medium'
B'Large'
Cflavor='Mango'
Dflavor='Peach'
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using keyword argument before positional argument.
Not specifying the keyword argument name.
5fill in blank
hard

Fill all three blanks to call the function with one positional and two keyword arguments in correct order.

Python
def book_flight(destination, seat_class='Economy', meal='Standard'):
    return f"Flight to {destination}, Class: {seat_class}, Meal: {meal}"

confirmation = book_flight([1], [2], [3])
print(confirmation)
Drag options to blanks, or click blank then click option'
A'Paris'
Bseat_class='Business'
Cmeal='Vegetarian'
Ddestination='Paris'
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Placing keyword arguments before positional arguments.
Not using parameter names for keyword arguments.

Practice

(1/5)
1.

Which of the following correctly describes the order of arguments in a Python function definition?

easy
A. Positional-only, positional-or-keyword, keyword-only
B. Keyword-only, positional-only, positional-or-keyword
C. Positional-or-keyword, keyword-only, positional-only
D. Positional-or-keyword, positional-only, keyword-only

Solution

  1. Step 1: Recall argument order rules

    Python functions must define arguments in the order: positional-only, positional-or-keyword, then keyword-only.
  2. Step 2: Match the correct order

    Positional-only, positional-or-keyword, keyword-only matches this exact order, others do not.
  3. Final Answer:

    Positional-only, positional-or-keyword, keyword-only -> Option A
  4. Quick Check:

    Argument order = Positional-only, positional-or-keyword, keyword-only [OK]
Hint: Remember: positional-only first, then normal, then keyword-only [OK]
Common Mistakes:
  • Confusing keyword-only and positional-only order
  • Thinking positional-or-keyword comes first
  • Ignoring positional-only arguments
2.

Which of the following function definitions is syntactically correct in Python?

def example(a, /, b, *, c):
    pass
easy
A. def example(a, /, b, *, c): pass
B. def example(a, b, *, /, c): pass
C. def example(a, *, b, /, c): pass
D. def example(/, a, b, *, c): pass

Solution

  1. Step 1: Understand syntax for positional-only and keyword-only

    The slash (/) marks positional-only arguments before it, and the asterisk (*) marks keyword-only arguments after it.
  2. Step 2: Check each option's order

    def example(a, /, b, *, c): pass correctly places a before / (positional-only), b as positional-or-keyword, and c after * (keyword-only). Others have invalid order or misplaced / and *.
  3. Final Answer:

    def example(a, /, b, *, c): pass -> Option A
  4. Quick Check:

    Syntax with / and * correct = def example(a, /, b, *, c): pass [OK]
Hint: Slash (/) before positional-only, star (*) before keyword-only [OK]
Common Mistakes:
  • Placing / after positional-or-keyword arguments
  • Putting * before /
  • Using / as a standalone argument
3.

What is the output of this code?

def func(a, /, b, *, c):
    print(a, b, c)

func(1, 2, c=3)
medium
A. TypeError: missing required keyword-only argument 'c'
B. 1 2 3
C. TypeError: positional argument after keyword argument
D. SyntaxError

Solution

  1. Step 1: Understand argument passing

    a is positional-only, so must be passed by position (1). b is positional-or-keyword, passed by position (2). c is keyword-only, passed as c=3.
  2. Step 2: Check function call validity and output

    All arguments are passed correctly. The print outputs the values: 1 2 3.
  3. Final Answer:

    1 2 3 -> Option B
  4. Quick Check:

    func(1, 2, c=3) prints 1 2 3 [OK]
Hint: Positional-only args by position, keyword-only by name [OK]
Common Mistakes:
  • Passing positional-only argument by keyword
  • Passing keyword-only argument positionally
  • Confusing argument order in call
4.

Identify the error in this function call:

def f(x, /, y, *, z):
    return x + y + z

f(x=1, y=2, z=3)
medium
A. No error, returns 6
B. TypeError: missing required positional argument y
C. SyntaxError: invalid function call
D. TypeError: x is positional-only and cannot be passed as keyword

Solution

  1. Step 1: Check argument types in definition

    x is positional-only (before /), y is positional-or-keyword, z is keyword-only (after *).
  2. Step 2: Analyze function call

    Calling f(x=1, y=2, z=3) tries to pass x by keyword, which is not allowed for positional-only arguments, causing a TypeError.
  3. Final Answer:

    TypeError: x is positional-only and cannot be passed as keyword -> Option D
  4. Quick Check:

    Positional-only args cannot be keyword = TypeError [OK]
Hint: Positional-only args can't be named in calls [OK]
Common Mistakes:
  • Passing positional-only argument by keyword
  • Thinking all arguments can be named
  • Ignoring the / marker meaning
5.

You want to define a function that takes exactly two positional-only arguments, one positional-or-keyword argument, and two keyword-only arguments. Which of these definitions is correct?

1. def func(a, b, /, c, *, d, e): pass
2. def func(a, /, b, c, *, d, e): pass
3. def func(a, b, c, /, *, d, e): pass
4. def func(a, b, /, *, c, d, e): pass
hard
A. Definition 3
B. Definition 2
C. Definition 1
D. Definition 4

Solution

  1. Step 1: Identify argument categories

    Two positional-only args: before / (a, b). One positional-or-keyword: after / and before * (c). Two keyword-only: after * (d, e).
  2. Step 2: Check each definition

    Definition 1 matches this order exactly: a, b before /, c after /, d, e after *. Others have wrong placement of / or * or argument counts.
  3. Final Answer:

    Definition 1 -> Option C
  4. Quick Check:

    Two positional-only, one normal, two keyword-only = Definition 1 [OK]
Hint: Two positional-only before /, one normal after, two keyword-only after * [OK]
Common Mistakes:
  • Placing too many args before or after / or *
  • Miscounting positional-only arguments
  • Confusing keyword-only with positional-or-keyword