Lesson 18: Python help() function
- ESTIMATED TIME: 7 mins.
- DIFFICULTY: Intermediate
- FUNCTIONS: help()
- 5 EXERCISES
Help function benefits
help() function is a great step to more programming independence. It can be argued that there are key aspects to programming such as logic, mathematics, collaboration, purpose. But another important topic is independence.
As you grow as a programmer and your coding skills improve you will realize that it’s not convenient to ask others or look up on the internet every question you might come up with.
Having tools such as help() function and official Python website will tremendously contribute to your advancement in Python programming.
The topics and keywords you can look up with help() function are quite vast, they can be functions, modules, classes and other keywords.
Let’s take a look:
Function 1: help()
Python’s help() function is very useful to get some help and understand other functions.
It’s also very practical to use since it prints directly to your console and you don’t have to search for external documentation or open other applications.
Used Where?
help() can be used to understand other functions or data types whenever needed.
You can look up for the construction and syntax as well as the arguments and parameters of other functions and data types.
Some functions, classes and modules you might want to look up with help() function can be:
- len
- dict
- str
- zip
- map
- filter
- sorted
Syntax do(s)
1) Simply the object you'd like to look up inside help()'s parenthesis.
2) If you'd like to look up data classes type as: dict, str, list etc.:
help(object)
3) However, object inside help() is optional and you can also just type help() and hit enter. This will enter the help() console and then you can look up the keywords you are interested in.
Syntax don't(s)
1)
Example 1: Print
Here is a detailed documentation about print() function.
>>> help (print)
Help on built-in function print in module builtins: print(...) print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False) Prints the values to a stream, or to sys.stdout by default. Optional keyword arguments: file: a file-like object (stream); defaults to the current sys.stdout. sep: string inserted between values, default a space. end: string appended after the last value, default a newline. flush: whether to forcibly flush the stream.
Example 2: Len
Here is help for len():
>>> help(len)
Help on built-in function len in module builtins: len(obj, /) Return the number of items in a container.
Example 3: Sorted
Here is for sorted():
>>> help(sorted)
Help on built-in function sorted in module builtins:
sorted(iterable, /, *, key=None, reverse=False)
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.
Tips
1- Sometimes it can be confusing to demystify a built-in function. Help can be very helpful and an efficient source to gain programming independence.
Example 4: Zip
Here is for zip():
>>> help(zip)
class zip(object) | zip(iter1 [,iter2 [...]]) --> zip object | | Return a zip object whose .__next__() method returns a tuple where | the i-th element comes from the i-th iterable argument. The .__next__() | method continues until the shortest iterable in the argument sequence | is exhausted and then it raises StopIteration. | | Methods defined here: | | __getattribute__(self, name, /) | Return getattr(self, name). | | __iter__(self, /) | Implement iter(self). | | __next__(self, /) | Implement next(self). | | __reduce__(...) | Return state information for pickling. | | ------------------------------------- | Static methods defined here: | | __new__(*args, **kwargs) from builtins.type | Create and return a new object. See help(type) for accurate signature.
Advanced Concepts (Optional)
You can also try help function on Python concepts below:
help> True
help> collections
help> builtins
help> modules
help> keywords
help> symbols
help> topics
help> LOOPING
Example 5: keywords
>>> help(‘keywords’)
Here is a list of the Python keywords. Enter any keyword to get more help.a
False
class
from
or
None continue global
pass
True
def
if
raise
and
del
import return
as
elif
in
try
assert
else
is
while
async except lambda
with
await
finally nonlocal yield
break
for
not
Next Lesson: Debugging