Bird
Raised Fist0
Pythonprogramming~20 mins

Parameters and arguments in Python - Practice Problems & Coding Challenges

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
Challenge - 5 Problems
๐ŸŽ–๏ธ
Parameter Pro
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output of function with default and keyword arguments
What is the output of this Python code?
Python
def greet(name, greeting="Hello"):
    return f"{greeting}, {name}!"

print(greet("Alice"))
print(greet("Bob", greeting="Hi"))
ATypeError: greet() missing 1 required positional argument: 'greeting'
BHello, Alice!\nHello, Bob!
CHi, Alice!\nHi, Bob!
DHello, Alice!\nHi, Bob!
Attempts:
2 left
๐Ÿ’ก Hint
Remember the default value is used if no argument is given for that parameter.
โ“ Predict Output
intermediate
2:00remaining
Output with *args and **kwargs parameters
What will this code print?
Python
def show_args(*args, **kwargs):
    print(f"Positional: {args}")
    print(f"Keyword: {kwargs}")

show_args(1, 2, a=3, b=4)
APositional: [1, 2]\nKeyword: {'a': 3, 'b': 4}
BPositional: (1, 2)\nKeyword: {'a': 3, 'b': 4}
CPositional: (1, 2)\nKeyword: [('a', 3), ('b', 4)]
DTypeError: show_args() got multiple values for argument 'a'
Attempts:
2 left
๐Ÿ’ก Hint
*args collects positional arguments as a tuple, **kwargs collects keyword arguments as a dictionary.
โ“ Predict Output
advanced
2:00remaining
Output of function with mutable default argument
What is the output of this code?
Python
def add_item(item, item_list=[]):
    item_list.append(item)
    return item_list

print(add_item('apple'))
print(add_item('banana'))
A['apple']\n['banana']
B['apple']\n['apple']
C['apple']\n['apple', 'banana']
DTypeError: unhashable type: 'list'
Attempts:
2 left
๐Ÿ’ก Hint
Default arguments are evaluated once when the function is defined, not each time it is called.
โ“ Predict Output
advanced
2:00remaining
Output of function with positional-only and keyword-only parameters
What will this code print?
Python
def func(a, /, b, *, c):
    return a + b + c

print(func(1, 2, c=3))
A6
BTypeError: func() missing 1 required keyword-only argument: 'c'
CTypeError: func() got some positional-only arguments passed as keyword arguments
DTypeError: func() missing 1 required positional argument: 'b'
Attempts:
2 left
๐Ÿ’ก Hint
Parameters before / are positional-only, parameters after * are keyword-only.
๐Ÿง  Conceptual
expert
2:00remaining
Identify the error in function call with argument unpacking
Given the function definition below, which option will cause a TypeError when called?
Python
def combine(a, b, c):
    return a + b + c
A
kwargs = {'a': 1, 'b': 2, 'c': 3}
combine(**kwargs, c=4)
B
args = (1, 2, 3)
combine(*args)
C
kwargs = {'a': 1, 'b': 2}
combine(**kwargs, c=3)
D
args = (1, 2)
combine(*args, 3)
Attempts:
2 left
๐Ÿ’ก Hint
Check if any argument is passed twice due to unpacking and explicit argument.

Practice

(1/5)
1. What is the role of parameters in a Python function?
easy
A. They are variables defined outside the function.
B. They are placeholders to receive values when the function is called.
C. They are the actual values passed to the function.
D. They are the output values returned by the function.

Solution

  1. Step 1: Understand what parameters are

    Parameters are names used in the function definition to hold values passed in.
  2. Step 2: Differentiate parameters from arguments

    Arguments are the actual values given when calling the function, parameters receive them.
  3. Final Answer:

    They are placeholders to receive values when the function is called. -> Option B
  4. Quick Check:

    Parameters = placeholders [OK]
Hint: Parameters are names in function definition, not actual values. [OK]
Common Mistakes:
  • Confusing parameters with arguments
  • Thinking parameters are outputs
  • Mixing parameters with global variables
2. Which of the following is the correct way to define a function with two parameters a and b in Python?
easy
A. function my_func(a, b):
B. def my_func[a, b]:
C. def my_func(a, b):
D. def my_func(a b):

Solution

  1. Step 1: Recall Python function syntax

    Functions are defined using def keyword, parameters inside parentheses separated by commas.
  2. Step 2: Check each option

    def my_func(a, b): uses correct syntax: def my_func(a, b):. Others have syntax errors.
  3. Final Answer:

    def my_func(a, b): -> Option C
  4. Quick Check:

    def + (params separated by commas) = correct [OK]
Hint: Use def and parentheses with commas for parameters. [OK]
Common Mistakes:
  • Using square brackets instead of parentheses
  • Omitting commas between parameters
  • Using wrong keywords like 'function'
3. What will be the output of the following code?
def greet(name):
    return f"Hello, {name}!"

print(greet("Alice"))
medium
A. Hello, Alice!
B. Hello, name!
C. greet(Alice)
D. Error: name not defined

Solution

  1. Step 1: Understand the function call

    The function greet takes one parameter name and returns a greeting string with that name.
  2. Step 2: Substitute the argument value

    Calling greet("Alice") passes "Alice" as the argument, so the returned string is "Hello, Alice!".
  3. Final Answer:

    Hello, Alice! -> Option A
  4. Quick Check:

    Argument replaces parameter in output [OK]
Hint: Arguments fill parameters; output shows argument value. [OK]
Common Mistakes:
  • Printing parameter name instead of argument value
  • Confusing function name with output
  • Expecting error due to missing quotes
4. Identify the error in this function call:
def add(x, y):
    return x + y

result = add(5)
medium
A. Missing one argument in the function call.
B. Function name is incorrect.
C. Parameters should be strings.
D. No error, code runs fine.

Solution

  1. Step 1: Check function definition

    The function add requires two parameters: x and y.
  2. Step 2: Check function call arguments

    The call add(5) provides only one argument, missing the second one.
  3. Final Answer:

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

    Parameters count must match arguments count [OK]
Hint: Count parameters and arguments; they must match. [OK]
Common Mistakes:
  • Assuming missing arguments default to zero
  • Thinking function name is wrong
  • Ignoring argument count mismatch
5. Consider this function:
def multiply(a, b=2):
    return a * b

print(multiply(4))
print(multiply(4, 3))

What will be the output and why?
hard
A. Error; cannot mix default and non-default parameters.
B. 6 and 12; default parameter is ignored.
C. 8 and 8; default parameter always used.
D. 8 and 12; second argument overrides default parameter.

Solution

  1. Step 1: Understand default parameters

    Parameter b has a default value 2, used if no argument is given.
  2. Step 2: Analyze each function call

    First call multiply(4) uses default b=2, so 4*2=8.
    Second call multiply(4, 3) overrides default with 3, so 4*3=12.
  3. Final Answer:

    8 and 12; second argument overrides default parameter. -> Option D
  4. Quick Check:

    Default parameters used unless overridden [OK]
Hint: Default parameters fill missing arguments; explicit arguments override. [OK]
Common Mistakes:
  • Thinking default parameters are always used
  • Believing mixing default and non-default causes error
  • Ignoring argument overriding default