So, in this tutorial, we are going to learn about Python Comments and Statements. We’ll talk about what Python comments are, the different types, and how you can define them. We also have a brief section that demonstrates Python statements.
Previously we learned about Keywords and Identifiers in Python.
What are Python Comments?
Python comments are strings that begin with the # (hash/pound sign). They are used to document code and to help other programmers understand the same. You can use Python comments inline, on independent lines, or on multiple lines to include larger documentation.
These comments 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-
#this is a Python 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.
Different 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.
1. Single Line Comments
In Python for single line comments use # sign to comment out everything following it on that line.
#this is a comment myVar = "hello comments" # a variable containing something print(myVar) #print statement to print contents of a variable
2. Multiple Lines Comments
Multiple lines comments are slightly different. Simply use 3 single quotes before and after the part you want to be commented.
''' 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")
What are Python Statements?
Statements are logical lines we write in our code. Statements can be like below.
- Assignment Statement: myVariable1=”hello world” myVariable2=100 myVariable3=12.23
- Addition Statement: myVariable4=myVariable2 + myVariable3
- Subtraction Statement: myVariable4=myVariable2 – myVariable3
- Multiplication Statement: myVariable4=myVariable2 * myVariable3
- Division Statement: myVariable4=myVariable2 / myVariable3
And many more. These are only some examples.
Alright! That’s all for this tutorial on Python comments and statements. 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, jump into the water”. #happy_coding 🙂
In our next tutorial, let’s talk about the different data types in Python.