[Python] Chapter 02-04 Basics of Python Program (About Comments)

[Python] Chapter 02-04 Comments

In this section, we will explain the comments. As the chapter progresses to a certain extent, the program becomes longer, and when you look back at it later, you often don't know what you were writing.

Curiously, once you look back at a program you wrote the next day, you often don't know what you were writing at all.

Also, when you hand over the program to a successor due to a change of department, etc., the successor will not understand at all even if you read the program, and you will have trouble ** during program maintenance **.

** Comments ** are used there.

In this comment, you can write a comment in the program and write a descriptive text. There are two types of comments, so let's take a look at each one.

This time as well, I would like to proceed by writing a program and executing it.

About comment description using "\ #"

First, copy the program 02-03-05.py </ font> that you wrote last time and chap02 </ font> in the same chap02 </ font>. Create = "# 00cccc"> 02-04-01.py </ font>.

You can write a comment by using "\ #" as shown below.

02-04-01.py


#Variable x,y,Enter a numerical value for each z
x = input('Variable x:')
y = input('Variable y:')
z = input('Variable z:')

#Convert each input to a numerical value, add them, and substitute the result for wa.
wa = int(x) + int(y) + int(z)

#Output the result
print(x+'When'+y+'When'+z+'The sum of' +str(wa)+ 'is.')

The area to the right of "** \ # " is ignored even if the program is executed. In this way, writing comments using ** \ # has the advantage of being easier to understand when you look back at the program later.

It is also used when debugging a program (*) </ font>, commenting out one line of the program code with \ #, and checking the operation.

(*) </ font> Debugging is the work of executing a program and removing the bug when it occurs.

About writing comments using three single quotes

The comment using \ # explained earlier is in the ** state where only one line can be commented out **. For example, when commenting on a long sentence, you have to put \ # on each line.

The comments used there are three single quotes. Copy the program 02-04-01.py </ font> you wrote earlier and chap02 </ font> in the same chap02 </ font> Create # 00cccc "> 02-04-02.py </ font>.

You can also comment out long sentences at once by listing three single quotation marks as shown below.

02-04-02.py


'''
Creator: Light
Program description date: 2020/05/16
Program content: A program that calculates the sum by inputting three numbers
'''

#Variable x,y,Enter a numerical value for each z
x = input('Variable x:')
y = input('Variable y:')
z = input('Variable z:')

#Convert each input to a numerical value, add them, and substitute the result for wa.
wa = int(x) + int(y) + int(z)

#Output the result
print(x+'When'+y+'When'+z+'The sum of' +str(wa)+ 'is.')

Finally

This time, I introduced the material that can be used in programming rather than programming, but commenting out is often used in practice. Please try to use it as appropriate.

Return to [Table of Contents Link]

Recommended Posts