SYSTEM NOTICE

Auto translation by AI. Be sure, accuracy, nuances and authorial intent may not be fully reflected.
見出し画像

[Python Introduction #02] Let's Understand How to Write Comments

・Why do we use comments?

It is common for others to find it difficult to understand the programs you have written. To prevent this, we use them as notes to make the code easier to read.



・What are comments?

Comments are annotations regarding the program, written to improve the readability of the program. By writing appropriate comments, the intent of the program becomes easier to convey, and debugging and maintenance become easier.


・Single-line comments

In Python, line-based comments are written by adding # at the beginning of the line.
* If you do not add '#' at the beginning, it will result in an error
* Adding a single space after # makes it easier to read

. Adding # at the beginning to comment out code is called commenting out.

# xに"Python"を代入しています
x = "Python"


・Comments in the middle of a line

Comments written in the middle of a line are called inline comments.

x = "Python" # xに"Python"を代入しています


・Multi-line comments

When coding, there are times when you want to comment out several lines of a program.
You could insert several '#' symbols, but if the number of lines is large, it becomes very tedious...

In such cases, you can comment out code by enclosing it with three single quotes (''') or three double quotes (""").

'''
a = 1  # 数値
b = "Python" # 文字列
c = [1, 2, 3] # 文字列
'''



・Points to note about comments

・Comments in the middle of a line

As shown below, if you write a second #, it will result in an error.

x = "Python" # xに"Python"を代入しています # Pは大文字です

As shown below, please write # as a single character!

x = "Python" # xに"Python"を代入しています(Pは大文字です)


・Multi-line comments

As shown below, if the indentation of the single quotation marks is misaligned, it will result in an error.

    '''
a = 1  # 数値
b = "Python" # 文字列
c = [1, 2, 3] # 文字列
    '''

Let's align the indentation as shown below!

'''
a = 1  # 数値
b = "Python" # 文字列
c = [1, 2, 3] # 文字列
'''

いいなと思ったら応援しよう!