Python Cheat Sheet


  # Single-line comments use the hash (#) symbol
  # They are useful for brief explanations or notes
  
  # Example of a single-line comment
  x = 42  # This is an inline comment explaining that x is set to 42
  
  """
  Multi-line comments, often called docstrings when placed at the beginning of a module, class, or function,
  are enclosed in triple quotes (either ''' or """). They can span multiple lines.
  """
  
  """
  Example of a multi-line comment:
  This is a longer comment that can span multiple lines.
  It's useful for detailed explanations or documentation.
  """
  
  # Alternatively, multi-line comments can also use multiple single-line comments:
  # This is a multi-line comment
  # using single-line comment symbols.
  # It can be just as effective for longer explanations.
  
  # Docstrings are a special type of multi-line comment used for documentation.
  def example_function():
      """
      This is a docstring for the example_function.
      It explains what the function does.
      """
      pass
  
  # Docstrings can also be used in classes and methods
  class ExampleClass:
      """
      This is a docstring for the ExampleClass.
      It describes the purpose and usage of the class.
      """
      def method(self):
          """
          This is a docstring for the method.
          It explains the method's functionality.
          """
          pass
  
© 2024 CheatsheetCoder, All Rights Reserved.