Comments in Python Code
Comments in Python Code
Comments in Python code are the lines of code which are not interpreted by the python interpreter at all. They are in fact ignored by it. The main purpose of comments in any programming or scripting language like python is to make pieces of code more understandable by the programmers. They also enhance the readability of the code very effectively.
Comments are used in any program for the following reasons:
-
- To make the code more readable.
- Provides explanation of the code or metadata of the project.
- To prevent execution of some code while you are trying to see a problem in you python code.
Types of Comments in Python Code
In Python, there are three types of comments, which are:
- Single-Line Comments
- Multi-Line Comments
- Python Docstring
Single-Line Comments in Python Code
Single-line comments as the name suggests are comments that span a single line, so they are basically used when you want to write short and sweet comments related to your code such as comments on variable declaration, function definition, function calling and many more. They start with a hash symbol (#) and anything written beyond that but before the next line is neglected or simply ignored by the interpreter.
Syntax of Single-line Comments:
# This is a single-line comment in python
Example of Single-line Comments:
#prints "StudyExperts" to the console print("StudyExperts")
Output:
StudyExperts
Multi-Line Comments in Python Code
Multi-line comments as the name suggests are the comments that span more than a line, so they are basically used when you want to provide thorough details or explanation regarding your code so that it can be easily understood in the future when the code is being reviewed or has been given to some other programmer to do the coding. Python does not really have multi-line comments but there are ways with which we can create them. They are:
- Using multiple hashtags(#)
- Using string literals
Using Multiple Hashtags(#) in Python Code
You can just write the comments in multiple lines and prefix them with a hashtag.
Syntax of Multi-line Comments using multiple Hashtags:
# This is a # multi-line comment # using multiple # hashtags(#) # tada!!
Example of Multi-line Comments using multiple Hashtags:
# This is an example to show how # multiple hastags can be used to # make multi-line comments in Python #prints "StudyExperts" to the console print("StudyExperts")
Output:
StudyExperts
Using String Literals in Python Code
In python, string literals are ignored if not assigned to a variable, so they can be used to make multi-line comments. triple quotes are used to make them.
Syntax of Multi-line Comments using String Literals:
''' Multi-line comment using string literal with three single quotes at the start of the first line and end of the last line ''' """ Multi-line comment using string literal with three double quotes at the start of the first line and end of the last line """
Example of Multi-line Comments using String Literals:
'''This is an example to show how string literals can be used to make multi-line comments in Python prints "StudyExperts" to the console''' """This is an example to show how string literals can be used to make multi-line comments in Python prints "StudyExperts" to the console""" print("StudyExperts")
Output:
StudyExperts
Python Docstring
It is a string literal with triple quotes and is used to link documentation with functions, modules, or classes to explain what they do. It can be made available by the __doc__ attribute.
Syntax of Python Docstring:
""" This is a docstring """
Example of Python Docstring:
def add(a,b): """ adds value of a and b """ return a+b #prints docstring of add function defined above print(add.__doc__)
Output:
adds value of a and b
Pingback: runtimewarning: invalid value encountered in true_divide - Study Experts