Daily Python Puzzle: Comments

What is the output of this code snippet?


# this is the first comment
spam = 1 # and this is the second comment
 # ... and now a third!
text = "# Is this a comment?"

This puzzle introduces two basic concepts. First, variables can hold strings. In fact, variables can hold any data type. The interpreter determines the data type of a variable at runtime. The data type of a variable can change: you can assign a string to a variable, followed by an integer. Second, comments in the code start with the hash character # and end with the start of the next line. Comments are important to improve readability of your code.

The small twist in this puzzle is the question whether the hash character within the string literal starts a new comment.
This is not the case as there are only two positions for a comment. First, at the beginning of a line. Second, after whitespace or code. But a comment cannot appear within a string.

Are you a master coder?
Test your skills now!


Related Video


# Is this a comment?


Leave a Comment