Programming fundamentalsLesson 3 of 4 in the course Statements

Python Comments, Code Organization, and Docstrings

5 flashcards · 5 quiz questions · 5 match pairs · 5 fill-in sentences · English

Lesson progress
0 / 5

Your progress is kept in this browser only.Sign up to save it

Summary

Comments and docstrings help people understand the purpose and structure of a Python program. They do not change how the program runs, but they can make code easier to read, maintain, and use.

A comment is text in source code intended for readers rather than the Python interpreter. A comment begins with #; everything from that character to the end of the line is ignored when Python runs the program.

All flashcards

What is a Python comment?
A comment is text for readers that begins with `#`. Python ignores it from the hash character to the end of that line when running the program.
Does every hash character begin a comment?
No. A hash character inside a string is ordinary string content, such as the `#1` in `"Item #1: "`.
What makes a comment useful and readable?
Use a space after `#` and describe the purpose of the code. Avoid comments that merely repeat what the statements visibly do.
How do blank lines and comments help organize a program?
Blank lines separate sections of related code, while concise comments can identify each section's purpose. Comments are useful where they clarify the structure; they are not needed on every line.
What is a docstring, and what is its common structure?
A docstring is a string documenting a program or component for users and other readers. It is commonly enclosed in triple quotes and contains a one-line summary, a blank line, and a longer description.

Quiz questions

  1. What does Python do with text from a comment's hash character to the end of that line?Answer: It ignores that text when running the program.
  2. In the string `"Item #1: "`, what is the role of the hash character?Answer: It is ordinary text inside the string.
  3. What can documentation tools do with Python docstrings?Answer: Use them to produce reference material.
  4. How can a programmer make the menu and preference-input parts of a program easier to distinguish?Answer: Separate the parts with a blank line and use concise comments to describe their purposes.
  5. Which structure is commonly used for a multi-line program docstring?Answer: A short summary, a blank line, and a longer description, enclosed in triple quotes.

Based on Introduction to Python Programming by Udayan Das, Aubrey Lawson, Chris Mayfield, Narges Norouzi (OpenStax), licensed CC BY 4.0. Adapted into a summary, flashcards and quizzes.

All Programming fundamentals lessons