Python Comment and Statement

Filed Under: Python

So, in this tutorial we are going to learn about Python Comment and Statement. Previously we learned about Keywords and Identifiers in Python.

Python Comment

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-


#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-
python statement, python comment example output
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.


#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.


'''

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")

Python Statement

Statements are logical lines we write in our code. Statements can be like below.

  1. Assignment Statement:
    
    myVariable1="hello world"
    
    myVariable2=100
    
    myVariable3=12.23
    
  2. Addition Statement:
    
    myVariable4=myVariable2 + myVariable3
    
  3. Subtraction Statement:
    
    myVariable4=myVariable2 - myVariable3
    
  4. Multiplication Statement:
    
    myVariable4=myVariable2 * myVariable3
    
  5. Division Statement:
    
    myVariable4=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”.
#happy_coding 🙂

Comments

  1. sameer says:

    python language is one of the peak demand in the IT field. .Types of Python Comments article which is very helpful thanks for sharing your efforts

  2. bdevils464 says:

    Thanks for publishing python information. This is very understanding and useful to me

  3. Kelly Technologies says:

    Inspirational content, have achieved a good knowledge from the above content on Python Training useful for all the aspirants of Python Training.

  4. Terry says:

    Hi Dev, Your tutorial is excellent for beginners. I have a feedback. At the bottom of “Python Keywords and Identifiers” page, the next page link goes to Android CollapsingToolbarLayout Example rather than “Python Data Types”.

    Thanks
    TB

    1. Kuhak Chakraborty says:

      Yes, because this is wordpress blog, so this shows according to publishing time.

  5. Python says:

    Thanks for publishing python information. This is very understanding and useful to me

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages