Module 2.5 - A Comment on Comments

Ace your homework & exams now with Quizwiz!

Comments may be useful in another respect - you can use them to mark a piece of code that currently isn't needed for whatever reason. Look at the example below, if you uncomment the highlighted line, this will affect the output of the code:

# This is a test program. x = 1 y = 2 # y = y + x print(x + y)

If you want a comment that spans several lines, you have to put a hash in front of them all. Just like here:

# This program evaluates the hypotenuse c. # a and b are the lengths of the legs. a = 3.0 b = 4.0 c = (a ** 2 + b ** 2) ** 0.5 # We use ** instead of square root. print("c =", c) Good, responsible developers describe each important piece of code, e.g., explaining the role of the variables; although it must be stated that the best way of commenting variables is to name them in an unambiguous manner.

Comment

A remark inserted into the program, which is omitted at runtime, is called a

Good, responsible developers describe each important piece of code, e.g., explaining the role of the variables; although it must be stated that the best way of commenting variables is to name them in an unambiguous manner.

For example, if a particular variable is designed to store an area of some unique square, the name square_area will obviously be better than aunt_jane. We say that the first name is self-commenting.

# (hash) sign and extends to the end of the line.

In Python, a comment is a piece of text that begins with a

If you want to place a comment that spans several lines, you need to place # in front of them all

Moreover, you can use a comment to mark a piece of code that is not needed at the moment (see the last line of the snippet below), e.g.: # This program prints # an introduction to the screen. print("Hello!") # Invoking the print() function # print("I'm Python.")

TIP for Commenting

TIP If you'd like to quickly comment or uncomment multiple lines of code, select the line(s) you wish to modify and use the following keyboard shortcut: CTRL + / (Windows) or CMD + / (Mac OS)

Comments can be used to leave additional information in code. They are omitted at runtime.

The information left in source code is addressed to human readers. In Python, a comment is a piece of text that begins with #. The comment extends to the end of line.

Whenever possible and justified, you should give self-commenting names to variables,

e.g., if you're using two variables to store a length and width of something, the variable names length and width may be a better choice than myvar1 and myvar2.


Related study sets

anatomy - broken bones and repair

View Set

Lesson 4.4 Graphing Linear Equations - X & Y Intercepts

View Set

Accounting 102 Chapter 6 & 5a Review

View Set