Bird
Raised Fist0
Pythonprogramming~10 mins

Positional arguments 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 two positional arguments.

Python
def greet(name, age):
    print(f"Hello {name}, you are {age} years old.")

greet([1])
Drag options to blanks, or click blank then click option'
A"Alice", 30
B"Bob", 25
C"Charlie", 20
D"Diana", 22
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Passing only one argument.
Forgetting the comma between arguments.
Using keyword arguments instead of positional.
2fill in blank
medium

Complete the function call to print the sum of two numbers using positional arguments.

Python
def add(a, b):
    return a + b

result = add([1])
print(result)
Drag options to blanks, or click blank then click option'
A5, 7
B5
Ca, b
D5 + 7
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Passing only one argument.
Passing an expression instead of two separate arguments.
Using variable names not defined.
3fill in blank
hard

Fix the error in the function call by completing the positional arguments.

Python
def multiply(x, y):
    return x * y

result = multiply([1])
print(result)
Drag options to blanks, or click blank then click option'
A4, 5
B4
Cx, y
D4 * 5
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Passing only one argument causes a TypeError.
Passing an expression instead of two arguments.
Using undefined variable names.
4fill in blank
hard

Fill both blanks to create a function call with three positional arguments.

Python
def describe_pet(name, animal_type, age):
    print(f"{name} is a {age} year old {animal_type}.")

describe_pet([1], [2], 5)
Drag options to blanks, or click blank then click option'
A"Buddy"
B"cat"
C"dog"
D"Max"
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Swapping the order of name and animal type.
Using numbers instead of strings for name or animal type.
Missing quotes around string arguments.
5fill in blank
hard

Fill all three blanks to call the function with correct positional arguments.

Python
def book_info(title, author, year):
    print(f"{title} by {author}, published in {year}.")

book_info([1], [2], [3])
Drag options to blanks, or click blank then click option'
A"1984"
B"George Orwell"
C1949
D"Animal Farm"
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Mixing up the order of arguments.
Using quotes around the year (should be a number).
Passing only two arguments instead of three.

Practice

(1/5)
1.

What are positional arguments in Python functions?

easy
A. Arguments passed in the exact order the function expects
B. Arguments passed with their names explicitly stated
C. Arguments that have default values
D. Arguments passed as a list or tuple

Solution

  1. Step 1: Understand argument passing

    Positional arguments are passed to a function in the order the function defines its parameters.
  2. Step 2: Differentiate from other argument types

    Unlike keyword arguments, positional arguments do not specify parameter names explicitly.
  3. Final Answer:

    Arguments passed in the exact order the function expects -> Option A
  4. Quick Check:

    Positional arguments = ordered values [OK]
Hint: Remember: order matters in positional arguments [OK]
Common Mistakes:
  • Confusing positional with keyword arguments
  • Thinking default values are positional arguments
  • Assuming order doesn't matter
2.

Which of the following function calls uses positional arguments correctly?

def greet(name, age):
    return f"Hello {name}, you are {age} years old."
easy
A. greet(age=30, name='Alice')
B. greet(30, name='Alice')
C. greet('Alice', name=30)
D. greet('Alice', 30)

Solution

  1. Step 1: Identify positional argument usage

    Positional arguments are passed without naming, in the order parameters are defined.
  2. Step 2: Check each option

    greet('Alice', 30) passes 'Alice' then 30 matching (name, age) order correctly.
  3. Final Answer:

    greet('Alice', 30) -> Option D
  4. Quick Check:

    Positional call = values in order [OK]
Hint: Positional means no names, just order [OK]
Common Mistakes:
  • Mixing keyword and positional arguments incorrectly
  • Passing arguments in wrong order
  • Using parameter names with positional arguments
3.

What is the output of this code?

def multiply(a, b):
    return a * b

result = multiply(3, 4)
print(result)
medium
A. 7
B. 34
C. 12
D. Error

Solution

  1. Step 1: Understand function call with positional arguments

    Arguments 3 and 4 are passed as a and b respectively.
  2. Step 2: Calculate the return value

    3 multiplied by 4 equals 12, so the function returns 12.
  3. Final Answer:

    12 -> Option C
  4. Quick Check:

    3 * 4 = 12 [OK]
Hint: Multiply arguments in order given [OK]
Common Mistakes:
  • Adding instead of multiplying
  • Concatenating numbers as strings
  • Confusing argument order
4.

Find the error in this function call:

def divide(x, y):
    return x / y

print(divide(10))
medium
A. Missing one positional argument in the call
B. Division by zero error
C. Syntax error in function definition
D. No error, prints 10

Solution

  1. Step 1: Check function parameters and call

    Function divide expects two positional arguments: x and y.
  2. Step 2: Analyze the call with one argument

    Calling divide(10) provides only one argument, missing the second required positional argument y.
  3. Final Answer:

    Missing one positional argument in the call -> Option A
  4. Quick Check:

    Function needs 2 args, got 1 [OK]
Hint: Count arguments to match parameters [OK]
Common Mistakes:
  • Assuming missing arguments default to zero
  • Thinking one argument is enough
  • Ignoring error messages
5.

Given this function:

def create_greeting(greeting, name, punctuation):
    return f"{greeting}, {name}{punctuation}"

Which call correctly uses positional arguments to produce Hello, Bob!?

hard
A. create_greeting('Bob', 'Hello', '!')
B. create_greeting('Hello', 'Bob', '!')
C. create_greeting(name='Hello', punctuation='!', 'Bob')
D. create_greeting(name='Bob', greeting='Hello', punctuation='!')

Solution

  1. Step 1: Identify parameter order

    The function parameters are greeting, name, punctuation in that order.
  2. Step 2: Match arguments to parameters positionally

    create_greeting('Hello', 'Bob', '!') passes 'Hello' to greeting, 'Bob' to name, and '!' to punctuation, producing the desired output.
  3. Final Answer:

    create_greeting('Hello', 'Bob', '!') -> Option B
  4. Quick Check:

    Order matches parameters for correct output [OK]
Hint: Match argument order to parameter order exactly [OK]
Common Mistakes:
  • Swapping argument order
  • Mixing positional and keyword incorrectly
  • Assuming keyword arguments are positional