Functions give different output based on the input given to them. For example, If you define a string as a list within a function, the function will output the list whenever given the string as an input.
As stated in previous tutorials, the len() function returns the length, or number of items within a given list. However, to find the length in characters of one of those given strings within your list, include its position number. For example, to find the length of the third item of the list menInTub, type len(menInTub[2]).
The help() function will briefly explain to you the uses of other functions in Python if need. For example, typing help(len) will explain the purpose of the len() function, albeit using jargon. Typing help() followed by a list, such as help(menInTub) will print out terse but helpful information on list objects.
Note that you can just specify a data type instead of a specific variable name when calling help(): help(list) produces the same result as help(menInTub), because menInTub is the list type.
help(list) prints out a LONG message, because it includes documentation on every attribute and method for the list data type. A better approach is starting with the dir() 'directory' command. It displays all attribute and method names defined on a data type:
After glancing through the result, if any method catches your fancy you can display a help message on that particular method by executing dir(obj.method). Below, help(list.extend) displays a help message on the list method .extend():
>>> help(list.extend)
Help on method_descriptor:
extend(...)
L.extend(iterable) -- extend list by appending elements from the iterable
>>>
Practice
Using help(), find out what the list method reverse() does and try it out. Do the same with sum().
>>> help(list.reverse)
Help on method_descriptor:
reverse(...)
L.reverse() -- reverse *IN PLACE*>>> planets
['mercury', 'venus', 'earth', 'mars', 'jupiter', 'saturn', 'uranus', 'neptune', 'pluto']>>> planets.reverse()
>>> planets
['pluto', 'neptune', 'uranus', 'saturn', 'jupiter', 'mars', 'earth', 'venus', 'mercury']>>> help(sum)
Help on built-in function sum in module builtins:
sum(iterable, start=0, /)
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.>>> sum()
Traceback (most recent call last):
File "<pyshell#29>", line 1, in
sum()
TypeError: sum expected at least 1 arguments, got 0>>> sum([])
0>>> sum([1, 2, 3, 4])
10>>> sum([29384, 389, 234490])
264263
Explore
Python 3.5 documentation 2. Built-in Functions lists them all. If you think there's a lot, think again! Python actually doesn't give you a large number of built-in functions. That's because many useful functions are packaged into their own modules.