0
0
Pythonprogramming~10 mins

Docstrings and documentation 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 add a docstring that describes the function.

Python
def greet(name):
    [1]
    return f"Hello, {name}!"
Drag options to blanks, or click blank then click option'
Aprint("Hello")
B# This function greets the user
C"""Return a greeting message."""
D'''Print greeting'''
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using comments (#) instead of docstrings.
Using single quotes instead of triple double quotes.
Placing the docstring outside the function.
2fill in blank
medium

Complete the code to add a docstring that explains the parameters.

Python
def add(a, b):
    [1]
    return a + b
Drag options to blanks, or click blank then click option'
A"""Add two numbers. Args: a (int): First number. b (int): Second number. Returns: int: Sum of a and b. """
B# Adds a and b
C"""Add two numbers and return the result."""
D'''Sum two values'''
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Not explaining parameters in the docstring.
Using comments instead of docstrings.
Missing the Returns section.
3fill in blank
hard

Fix the error in the docstring format.

Python
def multiply(x, y):
    [1]
    return x * y
Drag options to blanks, or click blank then click option'
A"""Multiply two numbers and return the product."""
B"Multiply two numbers and return the product."
C'''Multiply two numbers and return the product'''
D# Multiply two numbers
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using single quotes instead of triple double quotes.
Using comments instead of docstrings.
Not enclosing the docstring properly.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that documents word lengths for words longer than 3 characters.

Python
words = ['apple', 'bat', 'carrot', 'dog']
lengths = {word: [1] for word in words if len(word) [2] 3}
Drag options to blanks, or click blank then click option'
Alen(word)
B<=
C>
Dword
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using the wrong comparison operator.
Using the word itself instead of its length.
Not filtering words correctly.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase words to their lengths if length is greater than 4.

Python
words = ['tree', 'house', 'car', 'elephant']
result = [1]: [2] for word in words if len(word) [3] 4
Drag options to blanks, or click blank then click option'
Aword.upper()
Blen(word)
C>
Dword
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using the word itself instead of uppercase.
Using wrong comparison operator.
Mixing keys and values in the comprehension.