So, in this tutorial we are going to learn about Python Comment and Statement. Previously we learned about Keywords and Identifiers in Python.
Table of Contents
Python Comment are statements that are not part of your program. For this reason, comment statements are skipped while executing your program. Usually we use comments for making brief notes about a chunk of code. Also comments are important so that other can understand easily while reading your program. On the other hand, comments are also useful for the programmer himself. One can understand a program done a long time a ago simply from the comments of the program.
Here’s an example of comment-
Copy#this is a comment. I can write whatever I want here #print("I will not be executed") print("I will be executed")
If you run this, then you will see output will be like this below picture-
So you can see the lines starting with # are not executed. Those are the comments.
Types of Python Comments
In Python there are two types of comments- Single line comments and Multiple lines comments. Single line commenting is commonly used for a brief and quick comment (or to debug a program, we will see it later). On the other hand we use the Multiple lines comments to note down something much more in details or to block out an entire chunk of code.
Single Line Comments
In Python for single line comments use # sign to comment out everything following it on that line.
Copy#this is a comment myVar = "hello comments" # a variable containing something print(myVar) #print statement to print contents of a variable
Multiple Lines Comments
Multiple lines comments are slightly different. Simply use 3 single quotes before and after the part you want to be commented.
Copy''' print("I am in Multiple line comment line 1") print ("I am in Multiple line comment line 2") ''' print("I am out of Multiple line comment")
Statements are logical lines we write in our code. Statements can be like below.
- Assignment Statement:CopymyVariable1="hello world" myVariable2=100 myVariable3=12.23
- Addition Statement:CopymyVariable4=myVariable2 + myVariable3
- Subtraction Statement:CopymyVariable4=myVariable2 - myVariable3
- Multiplication Statement:CopymyVariable4=myVariable2 * myVariable3
- Division Statement:CopymyVariable4=myVariable2 / myVariable3
And many more. These are only some examples.
Alright, we are done with Python comment and statement. Try to run every piece of code snippet given here in your own machine. As there’s an old saying – “if you want to learn how to swim, then jump into the water”.