Description:
This article provides a comprehensive guide on writing Python functions with Doc Strings. Learn the importance of documentation in programming, how to use help function, and best practices for writing Doc Strings. Follow along with code examples and hands-on tasks to enhance your understanding.
Explanation for the video:
[Video Placeholder]
Key Concepts Explanation
Documentation Importance
In programming, documentation is essential for understanding code. Learn how to use help function to retrieve information about functions and classes in Python.
help(str)
help(str.startswith)
str.startswith?
Doc Strings Usage
Doc Strings are used to provide detailed information about functions. They should be the first line in the function body and not assigned to any variable.
def get_commission_amount(sales_amount, commission_pct):
"""Function to compute commission amount. commission_pct should be passed as percent notation (eg: 20%)
20% using percent notation is equal to 0.20 in decimal notation.
"""
commission_amount = (sales_amount * commission_pct / 100) if commission_pct else 0
return commission_amount
Hands-On Tasks
- Write a Doc String for a custom function you develop.
- Use help function to retrieve information about a built-in Python function.
Conclusion
In conclusion, writing Doc Strings is a crucial aspect of Python programming for maintaining code readability and providing useful information to users. Practice writing Doc Strings for your functions and leverage the help function to enhance your coding experience.
Doc Strings
Documentation is one of the key aspects related to programming. However, it should be crisp and informative. In Python, we can use Doc Strings for the documentation of our code. One of the key aspects of documentation is to provide information about the usage of a function.
- In Python, we can get information about the function by using help.
- We can get help for a class like
str
usinghelp(str)
and help for a function likestr.startswith
usinghelp(str.startswith)
. - If you want to provide help for a user-defined function, you can leverage the feature of Doc Strings. It is nothing but a string that is provided as the first statement in a function.
- Doc Strings should be the first line in the function body and should not be assigned to any variable.
- It is a good practice to provide crisp and concise Doc String for each of the custom functions developed.