Skip to content

Commit de9fc5e

Browse files
committed
DocString Added
1 parent 98b22b6 commit de9fc5e

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Welcome to the Python Repository! This repository serves as a comprehensive guid
4949
- Default Arguments
5050
- Gather Positional Arguments \*
5151
- Gather Keyword Arguments \*\*
52+
- Docstrings
5253

5354
## What is Python
5455

@@ -1285,6 +1286,62 @@ display_info(name="Alice", age="30", city="New York")
12851286
# city: New York
12861287
```
12871288

1289+
**[⬆ Back to Top](#table-of-contents)**
1290+
1291+
### Docstrings
1292+
1293+
**Definition:** Docstrings (Document Strings) are string literals used to document a Python module, class, function, or method. They serve as a form of documentation and are accessible using the built-in `help()` function or within integrated development environments (IDEs) like Jupyter Notebook or code editors.
1294+
1295+
**Clarification:** Docstrings provide explanations about the purpose, usage, parameters, and return values of functions and methods. They help developers understand how to use and work with the code they encounter, making it easier to collaborate and maintain codebases.
1296+
1297+
**Syntax:**
1298+
1299+
```python
1300+
def function_name(parameter1, parameter2):
1301+
"""
1302+
Brief description of the function or method.
1303+
1304+
More detailed explanation of what the function does and how to use it.
1305+
1306+
:param parameter1: Description of parameter1.
1307+
:param parameter2: Description of parameter2.
1308+
:return: Description of what the function returns.
1309+
"""
1310+
# Function body
1311+
# ...
1312+
```
1313+
1314+
**Example:**
1315+
1316+
```python
1317+
def calculate_total(price, quantity, tax_rate=0.1):
1318+
"""
1319+
Calculate the total cost of items including tax.
1320+
1321+
This function takes the price, quantity, and an optional tax rate to calculate
1322+
the total cost of items including tax.
1323+
1324+
:param price: The price of each item.
1325+
:param quantity: The quantity of items.
1326+
:param tax_rate: The tax rate (default is 0.1).
1327+
:return: The total cost including tax.
1328+
"""
1329+
subtotal = price * quantity
1330+
tax_amount = subtotal * tax_rate
1331+
total = subtotal + tax_amount
1332+
return total
1333+
```
1334+
1335+
To access the docstring of a function or method, you can use the `help()` function or by using the `.__doc__` attribute.
1336+
1337+
```python
1338+
print(help(calculate_total))
1339+
# Output: Displays the docstring of the calculate_total function.
1340+
1341+
print(calculate_total.__doc__)
1342+
# Output: Prints the docstring of the calculate_total function means it will print whatever we have written in the docstring.
1343+
```
1344+
12881345
---
12891346

12901347
**[⬆ Back to Top](#table-of-contents)**

0 commit comments

Comments
 (0)