GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Home
  • Learn Python
  • Python Tutorial
  • Basics
  • Lists
  • Dictionary
  • Code Snippets
  • Comments
  • Modules
  • API
  • Beautiful Soup
  • Cheatsheet
  • Games
  • Loops

This view

  • Original page
  • New address

GeezerVison is showing www.pythonforbeginners.com — How to use comments in Python - PythonForBeginners.com

You are here: Home / Comments / How to use comments in Python

When working with any programming language, you include comments in the code to notate your work. This details what certain parts of the code are for, and lets other developers – you included – know what you were up to when you wrote the code. This is a necessary practice, and good developers make heavy use of the comment system. Without it, things can get real confusing, real fast.

How to Write Comments in Python

In Python, there are two ways to annotate your code.

The first is to include comments that detail or indicate what a section of code – or snippet – does.

The second makes use of multi-line comments or paragraphs that serve as documentation for others reading your code.

Think of the first type as a comment for yourself, and the second as a comment for others. There is not right or wrong way to add a comment, however. You can do whatever feels comfortable.

Single-line comments are created simply by beginning a line with the hash (#) character, and they are automatically terminated by the end of line.

For example:


#This would be a comment in Python

Comments that span multiple lines – used to explain things in more detail – are created by adding a delimiter (“””) on each end of the comment.


""" 
This would be a multiline comment
in Python that spans several lines and
describes your code, your day, or anything you want it to
""" 

Please note: The recommended method for commenting multiple lines is using # on each line. The (“””) method isn’t actually a comment but defines a Text constant of the text between the (“””). It isn’t displayed, but exists and could potentially cause unexpected errors.

Remember to comment as often as possible. It’s important!

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files formatting Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scripts Split Strings System & OS urllib2
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.