Docstrings

'''
Docstring in Python:
A docstring in Python is a special type of comment used to describe a module, class, method, or function. 
It is a string literal that appears as the first statement in the definition of the object it documents. 
Docstrings are typically enclosed in triple quotes (""" or '' to allow multi-line text.

Why Use Docstrings?
Documentation: Explains what the code does, its parameters, return values, exceptions, etc.
Readability: Makes code easier to understand for other developers (or your future self).
Automatic Documentation: Tools like help() and pydoc use docstrings to display information.
'''
#Example:
def square(n):
    '''Takes in a number n, returns the square of n'''
    print(n**2)
square(10)               #output = 100
#Here '''Takes in a number n, returns the square of n''' is a docstring which will not appear in the output.

print(square.__doc__)    #output = Takes in a number n, returns the square of n

'''
Python Comments vs Docstring:
1.Docstrings:
Syntax: Triple quotes(""" or '').
Placement: Inside functions, methods, classes, or modules. 
Purpose: Describes what the code does (documentation).
Access: Accessible at runtime using .__doc__ or help().
Best For: High-level description (purpose, usage, parameters, return values).
Tools Usage: Used by documentation tools (like pydoc, Sphinx).
Multi Lines: Supports multi-line text without # on each line.

2.Comments:
Syntax: Hash Symbol #.
Placement: Anywhere in the code.
Purpose: Explains specific lines or logic of code.
Access: Not accessible at runtime.
Best For: Explaining complex logic, clarifying code.
Tools Usage: Not used by documentation tools.
Multi Lines: Multi-line comments require multiple #.
'''

'''
PEP 8:
PEP 8 (Python Enhancement Proposal 8) is the official style guide for Python code. 
It provides guidelines and best practices for writing clean, readable, and consistent Python code.
It was written in 2001 by Guido Van Rossum, Barry Warsaw, and Nick Coghlan.
'''

    




 




Comments

Popular posts from this blog

Print Function

Arithmetic Operators

Sets in Python