0
0
Pythonprogramming~10 mins

Variable-length arguments (*args) in Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a function that accepts any number of arguments.

Python
def greet([1]):
    for name in names:
        print(f"Hello, {name}!")
Drag options to blanks, or click blank then click option'
A&names
Bnames
C**names
D*names
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using ** instead of * for positional arguments.
Forgetting the asterisk and just writing the parameter name.
2fill in blank
medium

Complete the code to call the function with multiple arguments.

Python
def greet(*names):
    for name in names:
        print(f"Hello, {name}!")

greet([1])
Drag options to blanks, or click blank then click option'
A["Alice", "Bob", "Charlie"]
B"Alice", "Bob", "Charlie"
C{"Alice", "Bob", "Charlie"}
DAlice, Bob, Charlie
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Passing a list or set instead of separate arguments.
Not using quotes around string arguments.
3fill in blank
hard

Fix the error in the function definition to correctly accept variable-length arguments.

Python
def add_numbers([1]):
    total = 0
    for num in numbers:
        total += num
    return total
Drag options to blanks, or click blank then click option'
A*numbers
Bnumbers
C**numbers
D&numbers
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using ** instead of *.
Not using any asterisk.
4fill in blank
hard

Fill both blanks to create a function that sums only positive numbers from variable arguments.

Python
def sum_positive([1]):
    return sum(num for num in numbers if num [2] 0)
Drag options to blanks, or click blank then click option'
A*numbers
B>
C<
Dnumbers
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using < instead of > to filter positive numbers.
Not using * to accept variable arguments.
5fill in blank
hard

Fill all three blanks to create a function that returns a dictionary with word lengths for words longer than 3 characters.

Python
def word_lengths([1]):
    return [2]: len(word) for word in words if len(word) [3] 3 }
Drag options to blanks, or click blank then click option'
A*words
Bword
C>
Dwords
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Not using * to accept variable arguments.
Using parentheses instead of curly braces for dictionary comprehension.
Using < instead of > for filtering.