Computer Science Grade 5 20 min

Comments in Code

Comments in Code

Tutorial Preview

1

Introduction & Learning Objectives

Learning Objectives Define what a comment is in Python. Explain why comments are important for writing readable code. Identify the '#' symbol as the way to start a single-line comment. Write a single-line comment on its own line. Write a single-line comment at the end of a line of code. Use comments to temporarily disable a line of code, a technique called 'commenting out'. Differentiate between code that the computer runs and comments that the computer ignores. Ever written a secret note in the margin of your notebook that only you understand? 📝 What if you could write secret notes right inside your computer code? In this lesson, you will learn how to write special notes called 'comments' in your Python code. These notes are for humans to r...
2

Key Concepts & Vocabulary

TermDefinitionExample CommentA note written in the code for humans to read. The computer's interpreter completely ignores comments.# This line is a comment. The computer will skip it. SyntaxThe set of rules for how a programming language is written. The syntax for a Python comment is to start it with a '#' symbol.The correct syntax is `# This is a comment`, not `This is a comment`. InterpreterThe program that reads your Python code line by line and runs it. The interpreter is trained to ignore any line that starts with a '#' symbol.When the interpreter sees `print('Hello')`, it runs it. When it sees `# Say hello`, it does nothing. ReadabilityHow easy it is for a human to read and understand computer code. Good comments make code much more readable.Code w...
3

Core Syntax & Patterns

The Hashtag Rule # This is a comment In Python, any text on a line that follows a hashtag symbol (#) is a comment. The computer will ignore the hashtag and everything after it on that line. The 'Commenting Out' Technique # code_that_should_not_run() To temporarily stop a line of code from working without deleting it, place a hashtag (#) at the very beginning of the line. This turns the entire line into a comment.

4 more steps in this tutorial

Sign up free to access the complete tutorial with worked examples and practice.

Sign Up Free to Continue

Sample Practice Questions

Challenging
A programmer suspects the line `energy = energy - 5` is causing a bug. They comment it out like this: `# energy = energy - 5`. When they run the program again, the bug is gone. What does this most likely tell the programmer?
A.That the line `energy = energy - 5` is part of the problem.
B.That comments can fix bugs automatically.
C.That the variable `energy` should be deleted.
D.That the program no longer needs that line of code.
Challenging
Imagine a different programming language used `//` to start comments instead of `#`. How would you correctly 'comment out' the line `score = 50` in that language?
A.# score = 50
B.// score = 50
C.comment(score = 50)
D.score = 50 //
Challenging
A programmer wants to test two different ways to calculate a bonus. What is the most efficient way to use comments to switch between testing Version A and Version B? # Version A bonus = score * 0.1 # Version B # bonus = score + 5
A.Delete the lines for the version you are not testing.
B.Add another '#' to the version you are not testing.
C.Put a '#' in front of one version's code, and remove the '#' from the other.
D.Move the comment `# Version A` above `# Version B`.

Want to practice and check your answers?

Sign up to access all questions with instant feedback, explanations, and progress tracking.

Start Practicing Free

More from Python Fundamentals

Ready to find your learning gaps?

Take a free diagnostic test and get a personalized learning plan in minutes.