← Back to course
Code Crew G6+ / Week 01 / Thursday
4/6
Week 01 Β· Hello, Mission Control

Thursday

Notes for humans
// Your first real Python programs
⏱ about 30 min

Thursday: Notes for humans

Rocket opens a program he wrote last month and frowns at the screen. "I know I made this," he says slowly, "but I have no idea what the third part was supposed to do."

Raven reads over his shoulder, line by line, and taps her notebook. "What do you notice?" she asks. "You wrote notes for every experiment in here. Your program has no notes at all."

Nova blinks thoughtfully. "Interesting," she says. "Python can skip any line that starts with a hash mark. That means you can write notes that only people will read."

Rocket laughs and adds a short note above each part of the program. His future self will thank him.

Comments

A comment starts with the # sign. Python ignores everything from the # to the end of that line. Comments are for people: they explain what the program does and why.

Good programmers put a comment at the top of each file with its name and its job. They also add short comments above parts that are not obvious.

A comment can sit at the end of a line of code too. Python runs the code and skips the note after the #.

# launch_banner.py
# Shows a banner at the start of each session.

print("*" * 24)
print("    MISSION CONTROL")   # the title line
# print("TEST LINE")
print("*" * 24)
Predict: how many lines does launch_banner.py show?
WHY THIS EXERCISEThe TEST LINE print starts with #, so Python skips it. Only three print lines run.
************************
    MISSION CONTROL
************************

Commenting out a line

Putting a # in front of a line of code is called commenting it out. The line stays in the file, but Python skips it. Programmers do this to switch a line off for a test without deleting it.

To switch the line back on, delete the # and save again.

Line in a programDoes Python run it?
print("Ready")?
# print("Ready")?
print("Go") # start here?
# This program draws a banner.?
WHY THIS EXERCISEA line that starts with # is skipped. Code before a # on the same line still runs.
WHICH COMMENT HELPS MOST?
  • Read the question.
  • Tap your answer.
Which comment best explains print("=" * 30)?
At your computer
1. Choose File, then New File.
2. Type launch_banner.py, including the comments.
3. Save it in your mission folder as launch_banner.py.
4. Press F5 and check that you see three lines.
5. Delete the # in front of the TEST LINE print. Save and run again.
6. Put the # back. Then add a comment at the top that says when you wrote the program.

A # inside quotes is not a comment

There is one surprise. A # inside a string is just a character in the text. Only a # outside quote marks starts a comment.

# bay_sign.py
print("Docking Bay #4")   # the sign on the door
# print("Closed for repairs")
print("Status:", "open")
Docking Bay #4
Status: open
COMMENT OR TEXT?
  • Read the question.
  • Tap your answer.
Why does #4 appear in the output?
Why is Closed for repairs not shown?
Which comment is most helpful at the top of a file?
How many lines does bay_sign.py show?
What is the second word on the first output line?
LineIs part of it a comment?
print("Room #2")?
print("Room 2") # second room?
# print("Room 3")?
print("#" * 5)?
WHY THIS EXERCISEOnly a # outside quote marks starts a comment. Inside quotes it is part of the text.

Comments make your programs easier to read, test and fix. Tomorrow you will meet error messages, and you will learn to read them like a detective reads clues.

← Wednesday