Go to: Na-Rae Han's home page  

Python 3 Notes

        [ HOME | LING 1330/2330 ]

Tutorial 10: Built-in Functions

<< Previous Tutorial           Next Tutorial >>
On this page: len(), help(), dir().

Video Tutorial

Python 3 Changes

print(x,y) instead of print x, y

Video Summary

  • 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.
  • Visit https://docs.python.org/3/library/functions.html for more information on some of the built in functions in Python.

Learn More

  • 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:
    >>> dir(list)
    ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', 
    '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', 
    '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', 
    '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', 
    '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__',
    '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__',
    '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 
    'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
  • 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:
        L.extend(iterable) -- extend list by appending elements from the iterable


Using help(), find out what the list method reverse() does and try it out. Do the same with sum().


  • 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.