w3resource

Python: Print the documents of Python built-in function(s)

Python Basic: Exercise-11 with Solution

Write a Python program to print the documents (syntax, description etc.) of Python built-in function(s).

Sample function: abs()

Python Docstring:

A docstring is a string literal that occurs as the first statement in a module, function, class, or method definition. Such a docstring becomes the __doc__ special attribute of that object.

All modules should normally have docstrings, and all functions and classes exported by a module should also have docstrings. Public methods (including the __init__ constructor) should also have docstrings.

Sample Solution:

Python Code:

# Print the docstring (documentation) of the 'abs' function
print(abs.__doc__)

Sample Output:

Return the absolute value of the argument.

Additional Examples:

Example 1: len()

Python Code:

 # Print the docstring (documentation) of the 'len' function
print(len.__doc__)

Sample Output:

Return the number of items in a container.

Example 2: sorted()

Python Code:

 # Print the docstring (documentation) of the 'sorted' function
print(sorted.__doc__)

Sample Output:

Return a new list containing all items from the iterable in ascending order.

A custom key function can be supplied to customize the sort order, and the
reverse flag can be set to request the result in descending order.

Example 3: sum()

Python Code:

 # Print the docstring (documentation) of the 'sum' function
print(sum.__doc__)

Sample Output:

Return the sum of a 'start' value (default: 0) plus an iterable of numbers
When the iterable is empty, return the start value.
This function is intended specifically for use with numeric values and may
reject non-numeric types.

Example 4: map()

Python Code:

 # Print the docstring (documentation) of the 'map' function
print(map.__doc__)

Sample Output:

map(func, *iterables) --> map object

Make an iterator that computes the function using arguments from
each of the iterables.  Stops when the shortest iterable is exhausted.

Example 5: filter()

Python Code:

 # Print the docstring (documentation) of the 'filter' function
print(filter.__doc__)

Sample Output:

filter(function or None, iterable) --> filter object

Return an iterator yielding those items of iterable for which function(item)
is true. If function is None, return the items that are true.

Explanation

  • Docstrings: These are essential documentation strings in Python that describe the purpose and usage of modules, functions, classes, or methods. The docstring is available through the "doc" attribute.
  • Built-in Functions: Python provides many built-in functions such as abs(), len(), sorted(), sum(), map(), filter(), etc. Each function has a corresponding docstring that describes its usage.

Python Code Editor:

 

Previous: Write a Python program that accepts an integer (n) and computes the value of n+nn+nnn.
Next: Write a Python program to print the calendar of a given month and year.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.