Comments are a line of code which is not read and executed as part of the program. It is very useful while editing and debugging the program.
Python supports two types of comments –
1. Single line comment
2. Multi-line comment
1. Single Line Comment
In Python hash (#) character is used to create single line comment.
Syntax –
# [This is single line comment]
Example –
num1 = 5 # Initializing num1 num2 = 6 # Initializing num2 # Addition sum = num1 + num2 print("Sum of two number = ",sum)
Output –
Sum of two number = 11
2. Multi-Line Comment
Python multi-line comment is a little bit different from other languages. Multi-line comment starts and ends with “”” or ”’.
Syntax –
""" [This is multi-line comment] """
Or
''' [This is multi-line comment] '''
Example –
num1 = 5 # Initializing num1 num2 = 6 # Initializing num2 # Addition sum = num1 + num2 """ sub = num1 - num2 multi = num1 * num2 """ print("Sum of two number = ",sum)
Output –
Sum of two number = 11
Comments are useful for :-
To let others understood what you are doing – Comments let other programmers understand what you were doing in each step ( if you work in a group ).\
To remind yourself what you did – Most programmers have experienced coming back to other own work a year or too later and having to re-figure out what they did.