Lesson 18: help()
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.
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:
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.:
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.
Example 1: Print
Here is a detailed documentation about print() function.
>>> help (print)
Help on built-in function print in module builtins:
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 on built-in function len in module builtins:
Return the number of items in a container.
Example 3: Sorted
Here is for 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.
Example 4: Zip
Here is for zip():
| 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).
| 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.
1- Sometimes it can be confusing to
.sort() is a list method that sorts the list’s elements directly.
>>> lst = [1, 5, 66, 7]
>>> lst.sort(key=lambda x: x)
if you’d like to use a function as an argument in .sort() you need to use it with “key=” keyword.
Advanced Concepts (Optional)
Example 6: keywords
Inside map() function, first part tells what function to map values to, second part is the iterable which is filtered with filter function.
Here is a list of the Python keywords. Enter any keyword to get more help.
None continue global
async except lambda
finally nonlocal yield
- When you’re using a filter, map or zip function as an iterable in a loop or another function you don’t have to use the list() function to make them look readable and pretty. You can directly iterate on them.
Heavens Fire by John Einerssen