Python Programming Notes and Practical Programs BCA 3rd Semester CSJM University Kanpur (lecture 2)

Python Programming Notes and Practical Programs BCA 3rd Semester CSJM University Kanpur

Comments in Python Programming

BY ANKIT SHUKLA (DAMS GOVIND NAGAR KANPUR, UP, INDIA)

Although comments do not change the outcome of a program, they still play an important role in any programming and not just Python. Comments are the way to improve the readability of a code, by explaining what we have done in code in simple english.

Python comments

Before we go through an example of comments in Python. Lets first understand the need of comments in Python or in any programming language.

A comment is text that doesn’t affect the outcome of a code, it is just a piece of text to let someone know what you have done in a program or what is being done in a block of code. This is especially helpful when someone else has written a code and you are analysing it for bug fixing or making a change in logic, by reading a comment you can understand the purpose of code much faster then by just going through the actual code.

Types of Comments in Python

There are two types of comments in Python.
1. Single line comment
2. Multiple line comment

Single line comment

In python we use # special character to start the comment. Lets take few examples to understand the usage.

# This is just a comment. Anything written here is ignored by Python

Multi-line comment:

To have a multi-line comment in Python, we use triple single quotes at the beginning and at the end of the comment, as shown below.

'''

This is a

multi-line

comment

'''

Python Comments Example

In this Python program we are seeing three types of comments. Single line comment, multi-line comment and the comment that is starting in the same line after the code.

'''

We are writing a simple program here

First print statement.

This is a multiple line comment.

'''

print("Hello Guys")

 

# Second print statement

print("How are You all?")

 

print("Welcome to BeginnersBook") # Third print statement



# character inside quotes

When # character is encountered inside quotes, it is not considered as comment. For example:

print("#this is not a comment")

Output:

#this is not a comment

No comments:

Post a Comment

Give your valuable feedback

Topic :Software & Types, Subject: Computer Fundamental Notes for CSJM University Kanpur(for different courses like BBA, BCA, etc..)

Software Software refers to the programs, data, and instructions that enable a computer or other digital device to perform specific tasks or...