If you have gone through the previous chapters of our “Learn Python with me” tutorial series of our blog, you must be very comfortable with Python already due to its easy and versatile features. For those of you who are visiting for the first time, we will share a link below that has the links of all the Python Chapters. And today, in this post, we will learn about Python Comments, Literals, Constants, and Docstrings.
Also, find links to all chapters of Python here: Learn Python
1. Python Comments
We shared several examples of Python programs and code in our previous posts. You might have noticed that we added several comments in each code in those programs, which started with the ‘#’ symbol. In Python, we add single-line comments using this symbol. If you wonder why comments are required for a program, comments are something that programmers use to understand the code. Also, they are usually ignored by compilers and interpreters. And we have two kinds of comments in Python – Single Line Comments and Multiline Comments.
1.1 Python Single Line Comments
#Simple Python code to understand variables in Python
p=2
q=p
print(p) #displays 2
print(q) #displays 2
p=1
print(p) #displays 1
print(q) #displays 2
If you observe the first line of the program above, you will see that I began it with the ‘#’ symbol. In Python, we use this symbol to add single-line comments. The entire first line until it ends is considered a comment, and Python ignores this line while running the program and begins/continues the next set of execution steps from the next line. In the example mentioned above, Python stores the value 2 in variable ‘p’ after ignoring the first line of the Python comment.
1.2 Python Multi Line Comments
If you want to add more lines of comments in a program, you can use the ‘#’ symbol at the beginning of each line. But it will be tiresome if you have more lines. Consider the example below.
#this is the first line of comment
#this is the second line of comment
#this is the third line of comment
#this is the fourth line of comment
I was able to add 4 consecutive lines of comments beginning with the ‘#’ symbol at each line of the code. The same can be achieved for multi-line comments using triple single-quotes or triple double-quotes. However, this way of commenting is advised to be followed for Docstrings and that’s why they are identified by several as Docstring comments in the Python world. Please view the examples below for more details.
"""
this is the first line of comment
this is the second line of comment
this is the third line of comment
this is the fourth line of comment
"""
or
'''
this is the first line of comment
this is the second line of comment
this is the third line of comment
this is the fourth line of comment
'''
Using triple double quotes or triple single quotes, you can write as many lines of comments as you want, as shown in the examples above. All comments are non-executable statements in Python, which means they are disregarded by both PVM (Python Virtual Machine) and Python Compiler.
2. Python Literals
A fixed value that is stored in the variable or constant of a program is called literal. If you consider p=2, the value 2 is called literal as it is saved into the variable p. It is also called integer literal since it is of integer datatype. We have the following kinds of literals in Python, and we will go through each one of them in detail.
- String Literals
- Boolean Literals
- Numeric Literals
2.1 Python String Literals
A group of characters stored together in single quotes or double quotes or triple quotes is called a string literal. In our previous post, we learned that Python does not differentiate between Single quoted strings or Double quoted strings.
string1='Say something baby'
string2="Say something baby"
Output
>>> string1='Say something baby'
>>> string2="Say something baby'
>>> string2="Say something baby"
>>> print(string1)
Say something baby
>>> print(string2)
Say something baby
If a string literal exceeds a single line, then you should rely on triple quotes, as shown in the example below:
strings3=""" Why don't you say something baby?
Are you angry with me?? I'm so sorry !! """
Output
>>> strings3=""" Why don't you say something baby?
... Are you angry with me?? i'm so sorry !! """
>>> print(strings3)
Why don't you say something baby?
Are you angry with me?? i'm so sorry !!
Python String Escape Characters
Using escape characters in Python strings, you can perform additional tasks like adding a horizontal tab space, newline, display a single quote or double quote, etc.
Python String Escape Character | Description |
\n | New Line |
\v | vertical tab |
\t | Horizontal tab space |
\r | Enter |
\b | backspace |
\” | show a double quote |
\’ | show a single quote |
\\ | show a single \ |
\ | Newline continuance |
#Python program to show the String Escape Characters
#Horizontal tab space and next line escape characters
str1="This\tis cool !! \nisn't it?"
print(str1)
Output
>>> str1="This\tis cool !! \nisn't it?"
>>> print(str1)
This is cool !!
isn't it?
2.2 Python Boolean Literals
In Python, True or False stored into a bool type variable is called a Boolean Literal.
2.3 Numeric Literals
As mentioned in the earlier section of this post, if an integer value is stored into a variable, it is called an integer literal. Like integers, there are other numerical kinds of data in Python. They are listed below with examples.
Name of the Literal | Examples |
Complex Literal | 3+5j |
Binary Literal | 0B110101 |
Octal Literal | 0o777 |
Hexadecimal Literal | 0xA180 |
Float Literal | 3.7, -4.40 |
Integer Literal | -7, 7 |
Python Constants
Constants also behave like variables. But the major difference between both variables and constants is that the latter’s value cannot be changed or updated throughout the course of program execution. It means that if you declare a value to a variable as constant, you cannot update its value again while running the program. For example, we have several constant values in the field of Mathematics and Physics, like the ‘pi’ value (22/7).
Unfortunately, you cannot declare a constant in Python, unlike other programming languages like Java. Instead, you can choose to declare the variables in CAPITAL LETTERS to identify the constants for your reference. For example, MIN_VALUE. But do note that its value can be altered later.
3. Python Docstrings
It is important to document your programs in any programming language. Let me tell you why. Imagine you coded an entire project and took an entire month to complete it. You are smart, and I am sure that you added the comments in each block of code to indicate or identify why you added that particular block of code. However, we are humans at the end of the day, and that’s why you might come across situations where you feel confused about your code and question yourselves whether you wrote it or not.
If you get confused about your own code, what about others? As a solution for all of this, Python provides Docstrings for you to create an API (Application Programming Interface) Documentation file from a Python program. Using this way, you can describe all functions, modules, classes, etc., for other audiences to understand your code easily in a separate text file or HTML file.
Let us look at the example below to understand more about how to implement Docstrings in Python programs.
def sub(x,y):
return x-y
'''
This function takes two numbers and finds their subtraction value
It displays the subtracted value in the output
'''
print(sub(10,5))
Output
E:\Python\Programs>python DocstringsExample.py
5
E:\Python\Programs>python -m pydoc DocstringsExample
5
Help on module DocstringsExample:
NAME
DocstringsExample
FUNCTIONS
sub(x, y)
FILE
e:\python\programs\docstringsexample.py
E:\Python\Programs>python -m pydoc -w DocstringsExample
5
wrote DocstringsExample.html
- In the python program above, we declared a function that added the logic to subtract two numbers and saved it as DostringsExample.py.
- In the command prompt, we used the command, python DocstringsExample.py (“python <filename>.py“) to run the program.
- We then used python -m pydoc DocstringsExample (“python -m pydoc <filename>“) to display the documentation of the DocstringExample.py python program.
- To export the documentation to an HTML file, we used the command, python -m pydoc -w DocstringsExample (“python -m pydoc -w <filename>“). You can see the screenshot of the exported HTML file below
In this post, we learned about Comments in Python, Literals in Python, the concept of Constants, and DocStrings. The theory of Comments and Docstrings with examples shared above will be constructive for you to learn the Python language and professionally write Python code.
Would you mind sharing with us your experience of learning these Python concepts in the comments section below? We would love to hear from you.