Check your understanding of the previous reading: comments, docstrings, and how to read and write function documentation.
Enter the symbol (not its name) used for single-line comments in Python.
A single-line comment starts with a # character. Everything after it, to the end of that line, is ignored by Python.
True or False: Multi-line comments in Python are actually just triple-quoted strings.
Select one answer, then check.
A docstring is also a triple-quoted string, just one placed right after a def line and picked up specially by Python. What does an unused triple-quoted string elsewhere in the code do?
True or False: Single-line comments are ignored by Python, and so are only for human benefit.
Select one answer, then check.
# on a line; comments exist purely to help human readers, not the interpreter.Does changing or removing a # comment ever change what a program does when it runs?
True or False: You should always document every line of a program.
Select one answer, then check.
Think about a line like x = x + 1: is a comment on that line always worth the clutter, or does it depend on the code?
def make_time(hour:int, minute:int, second:float)->str:
'''
Creates a string representation of the given hour, minute, and second.
Note that this takes into account Daylight Savings Time.
Args:
hour (int): The number of hours past midnight (military time).
minute (int): The number of minutes past the hour.
second (float): The number of seconds past the minute, which can be a
decimal number (indicating milliseconds).
Returns:
str: A string representation of the time ("hh:mm:ss.sss") including
milliseconds.
'''
Interpret the documentation above to fill in the blanks.
1. This function takes parameters.
2. The second parameter is named .
3. The third parameter's type is .
4. The function's return type is .
5. We want to change the type of hour to be text. You need to replace int with .
6. We want to add a new parameter named is_dst, a boolean flag for whether it is Daylight Savings Time. What is the name and type that would go in the Args section of the documentation? Don't include indentation, the colon, or a description. Just give the name and type in parentheses: .
Read the docstring like a table: each parameter is listed in the Args section in the same order it appears in the function's signature, with its type in parentheses right after its name.
hour, then minute, then second.-> arrow in the signature, and is repeated at the start of the Returns section.str, the same type the Returns section already uses.One correct way to fill each blank:
1: 3 2: minute 3: float 4: str 5: str 6: is_dst (bool)
Distractors (not used): 2 and 4 (wrong parameter counts), hour and second (parameter names that don't answer these particular blanks), int (hour's old type, not its new one), text (the idea, not Python's type name), bool alone (missing the parameter name), is_dst alone (missing the type), is_dst (boolean) and dst (bool) (close, but not matching the exact style the rest of the docstring uses).
Which of the following are reasons for commenting code?
Select every one that applies, then check.
assert_equal, not documentation.# than documenting what the code does.Two of these options describe things comments genuinely help with. The other two describe something else entirely: testing code, and temporarily disabling code.