Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

How to Comment in Python - DesignLinux

designlinux 0 Comments

When writing Python code, it is always a good practice to make your code clean and easily understandable. Organizing the code, giving variables and functions descriptive names are several ways to do this.

Another way to improve the readability of your code is to use comments. A comment is a human-readable explanation or annotation that is used to explain the code. For example, if you wrote a complex regex, you add a comment describing what the code does.

Adding comments to your Python code will save you a lot of time and effort when you look at your code in the future. Let’s say you want to change a script that you wrote a few months or years ago. The chances are that you will not remember why you wrote some complicated piece of code unless you added a comment. The comments also help other developers to understand your code and its purpose.

Comments should be short and to the point. Do not explain something that is obvious to the reader.

This article covers the basics of writing comments in Python.

Writing Comments in Python #

Python ignores everything written on the line after the hash mark (#).

Comments can be added at the beginning on the line or inline with other code:

# This is a Python comment.
print("Hello World") # This is an inline Python comment.

The blank space after the hash mark is not mandatory, but it will improve the comment’s readability.

A hash character within a string literal doesn’t indicate the start of a comment line. It is simply a hash character:

paragraph = "# Hash inside quotes is not a comment."

Comments should be at the same indent level as the code beneath it:

```py
def factorial(n):
  if n == 0:
    return 1
  else:
    # Use the factorial function
    return n * factorial(n-1)

If your text editor supports syntax highlighting, comments are usually represented in green.

Comments are also useful when debugging a script. Instead of deleting some lines or blocks, you can comment them out:

# for fruit in fruits:
#   print(fruit)

Multiline Comments in Python (Comment Blocks) #

Unlike other popular programming languages, Python supports only single line comments.

The simplest way to write multiline comments in Python is to add single line comments one after another:

# This is the first line.
# This is the second line.

Another option is to use docstrings.

Docstrings are multiline string literals that are used to document what a module, function, class, or method does.

A docstring starts and ends with triple double quotes (""") and can span on one or multiple lines:

"""This is
a multiline
docstring.
"""

Docstrings are not technically comments. When a docstring occurs as the first statement in a module, function, class, or method, it ends up in the bytecode and becomes the __doc__ special attribute of that object. You should prefer using regular single-line hash comments.

Shebang #

If you’re reading Python scripts, you might notice that on some of them the first line start with the #! characters and the path to the Python interpreter:

#!/usr/bin/env python3

This sequence of characters is called shebang and is used to tell the operating system which interpreter to use to parse the rest of the file. Scripts that start with shebang and are executable can be run in the terminal without typing python before the script name.

Because the shebang line starts with the hash character, it is considered as a comment and automatically ignored by the Python interpreter.

Conclusion #

Writing comments is a good practice and helps other developers, including future self, to understand what the code does. In Python, everything after the hash mark (#) and until the end of the line is considered to be a comment.

If you have any questions or feedback, feel free to leave a comment.

python

Related

Tags: python

How to Install TeamViewer on RHEL 8

Prev Post

How To Install Linux Mint 20 “Ulyana”

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 1
  • 62
  • 605,812

DesignLinux.com © All rights reserved

Go to mobile version