Exploring Built-in Functions in Python
The Python interpreter has a number of functions and types built into it that are always available. These do not require an import statement. The Python built-in functions are always available to you in Python, in any app you are creating, as well as at the Python command prompt. These built-in functions are also part of the standard library.
In fact, if you do a web search for Python built-in functions, some of the search results will point directly to the Python documentation. On that page, you can click the name of any function to learn more about it. Examples of built-in functions:
The print() function
You can use it to display formatted messages on the screen. This is among the first functions encountered by most beginners of the Python programming language.
Example:
>>> print('Hi there')
Hi there
>>>
The len() function
The len() function returns the length (the number of items) of an object. The argument may be a sequence (such as a string, bytes, tuple, list, or range) or a collection (such as a dictionary, set, or frozen set).
Example:
>>> y = 'Hello'
>>> print(len(y))
5
>>>
The list() function
The list() function is used to create an empty list.
Example:
>>> mylist = list()
>>> print(mylist)
[]
>>>
The type([object]) function
The type() function in Python usually identifies the type of a piece of data. In this case, the data could be an item such as an integer, string, list, and dictionary. The below examples were run in Python’s IDLE (Integrated Development and Learning Environment).
Example 1:
>>> x = 3
>>> print(type(x))
<class 'int'>
>>>
The above output tells us that x is an integer and an instance of the int class from the standard library.
Example 2:
>>> y = 'Hello'
>>> print(type(y))
<class 'str'>
>>>
Here, y contains data that is of the type string (or str object or str instance), created by the Python str class.
Example 3:
>>> mylist = list()
>>> type(mylist)
<class 'list'>
>>>
The dir() function
The dir() function displays a list of all the attributes associated with a type. For example, in the preceding example, the result <class ‘list’> tells us that the data is the list data type. So we know that it is an instance of a class called list. If we are looking to find out the available attributes for a given object, we can use the dir() function. The function will return a list of the relevant attributes.
Example:
>>> mylist = list()
>>> dir(mylist)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
>>>
Names surrounded by double-underscores, such as __add__ and __class__, are sometimes called dunder-named items, where dunder is short for double underscores. (Dunder-named items are often referred to as special variables or magic methods). Each dunder-named item represents something built into Python that plays a role we do not necessarily access directly.
Using the help() function
We can get more detailed information by using help() rather than dir. The output will provide substantial information about the object in parentheses. For example, where dir(mylist) lists the names of attributes of that type, help(mylist.pop) provides more detail about each attribute such as the pop() method associated with list objects.
Example:
>>> mylist = list()
>>> help(mylist.pop)
Help on built-in function pop:
pop(index=-1, /) method of builtins.list instance
Remove and return item at index (default last).
Raises IndexError if list is empty or index is out of range.
>>>
Conclusion
In this article, we have explained what Python built-in functions are. We have provided examples of some of the available built-in functions. We have demonstated with examples how some of these functions can be used to achieve different desired results.