0
0
Pythonprogramming~20 mins

Docstrings and documentation in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Docstring Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
1:30remaining
Output of a function with a docstring
What is the output of this code when calling greet('Alice')?
Python
def greet(name):
    """Return a greeting message for the given name."""
    return f"Hello, {name}!"

print(greet('Alice'))
ANone
Bgreet
CHello, Alice!
DHello, name!
Attempts:
2 left
๐Ÿ’ก Hint
The function returns a formatted string using the input name.
โ“ Predict Output
intermediate
1:30remaining
Accessing a function's docstring
What will be printed by this code?
Python
def add(a, b):
    '''Add two numbers and return the result.'''
    return a + b

print(add.__doc__)
ASyntaxError
Badd
CNone
DAdd two numbers and return the result.
Attempts:
2 left
๐Ÿ’ก Hint
The __doc__ attribute holds the function's docstring.
โ“ Predict Output
advanced
1:30remaining
Docstring effect on function behavior
What is the output of this code?
Python
def multiply(x, y):
    '''Multiply two numbers.'''
    result = x * y

print(multiply(3, 4))
ANone
B12
CMultiply two numbers.
DTypeError
Attempts:
2 left
๐Ÿ’ก Hint
Check if the function returns a value.
๐Ÿง  Conceptual
advanced
1:00remaining
Purpose of docstrings in Python
Which of the following best describes the main purpose of a docstring in Python?
ATo store variables inside the function.
BTo document what a function, class, or module does for users and developers.
CTo improve the speed of the program.
DTo execute code inside the triple quotes.
Attempts:
2 left
๐Ÿ’ก Hint
Docstrings are meant for explaining code, not running it.
โ“ Predict Output
expert
2:00remaining
Docstring and help() function output
What will be the first line printed by help(divide) for this function?
Python
def divide(a, b):
    '''Divide a by b and return the result.'''
    if b == 0:
        raise ValueError("Cannot divide by zero")
    return a / b
AHelp on function divide in module __main__:
BDivide a by b and return the result.
CTraceback (most recent call last):
DNone
Attempts:
2 left
๐Ÿ’ก Hint
The help() function prints a header line before the docstring.