Comments in Python start with # for single line starting.

One way, Manually you can type it. The second way is using the shortcut key.

Shortcut key to comment a code in PyCharm

Shortcut keys are used to comment and uncomment a single line or block of lines. Please follow the below keys to comment and uncomment in Pycharm.

  • Open Code editor with Python code

  • Select a line or multiple lines

  • Use shortcut keys

    Windows: Ctrl + / or Ctrl + NumPad / MacOS: Cmd + /

  • For normal lines, The above shortcut comments line(s). if you use the same shortcut on commented lines, It does uncomment the lines.

The above shortcut works in almost all OS.

Shortcuts are mapped in the Keymap settings.

You can still change or update shortcut keys using the below options.

  • Open File Menu, Select Settings Menu Item

pycharm settings

  • It opens the Settings window

  • Select Keymap on the left side tab, Type comments in the Search box on the Right side

  • It shows Comment Actions with two items with existing shortcut keys

    • Comment with Line Comments
    • Comment with Block Comments
  • Right-click on one of the items, and Select Add Keyboard Shortcut to assign new keys for comments. Check the below diagram for the options pycharm Settings Keymap

  • It opens a popup by taking a new shortcut key.

  • Click on OK to close the popup and a new shortcut will be used from here to comment and uncomment

How to add documentation comments in Pycharm

Follow the steps to create a documentation skeleton for any function.

  • First, Move the caret or cursor over the function
    def add(m,n):
        return m+n;
  • Use the Alt + Enter key, that display intention Actions Popup

  • Select Insert documentation string stub as shown below pycharm Intention Actions docstring

  • It generates documentation template stub as given below

class calculator:
    def add(m,n):

        :param m:
        :param n:

        return m+n;

Modify and update the doc string to give meaning to full text.

You can check more about here