The many Built-in functions are hands down one of the most important parts of the Python programming language. Not only are they so commonly used, but there are many things that are only possible with the use of these functions. There are dozens of built-in functions, each with their unique abilities.
In this Section, we have a compilation of all the Built-in functions in Python. Each Built-in function is given a brief description and a single example. There is also a link for each Built-in Function which leads to a more detailed page on that function with more examples. Make sure to check them out, you’ll be missing out otherwise.
Compilation
1. abs()
The Python abs() Built-in-Function returns the absolute value for a given number. In the case of a complex number, the absolute value is the magnitude of it’s real and imaginary parts.
>>> abs(-5.6)
5.6
2. chr()
The Python chr() function is a built-in function that returns the string representing a character whose Unicode is an integer. This function is the reverse of the ord() function, which takes a number and outputs it’s ASCII equivalent.
>>> chr(97)
'a'
3. callable()
A callable object is an object that can be called. The Python callable() built-in function determines whether an object is callable or not. If it is, it returns True. Otherwise, it returns False.
>>> callable(5)
False
4. complex()
A complex number is a number that can be expressed in the form a + bi. The complex function takes integers or strings as inputs and returns an appropriate complex number. Attempting to pass a inappropriate value will possibly result in a ValueError.
>>> complex(3)
(3+0j)
>>> complex(-3,-2)
(-3-2j)
5. dict()
The python dict function is used to create dictionaries. You can, of course create them manually, but this function allows for greater flexibility and options while creating dictionaries. You may for instance, pass a dictionary into this function, combining it with others to create a larger dictionary.
>>> dict({"a":1, "b":2}, c = 3)
{'a': 1, 'b': 2, 'c': 3}
>>> list = [["a",1],["b",2]]
>>> dict(list)
{'a': 1, 'b': 2}
6. dir()
The Python dir() function has the ability to retrieve a list of all attributes and methods belonging to any object. If no object is specified, the dir() function will return all the module names in the local namespace.
X = ["Apple", "Orange", "Grape"]
print(dir(X))
7. enumerate()
The enumerate() takes as input a sequence, iterates over it including a counter for every element, returning it as an enumerate object. The benefit of having an enumerate object is that you can place it in a loop to iterated over.
X = "String"
print(list(enumerate(X)))
[(0, 'S'), (1, 't'), (2, 'r'), (3, 'i'), (4, 'n'), (5, 'g')]
8. eval()
eval is a built-in-function used in python, eval function parses the expression passed to it and evaluates it as a python expression. Once this expression has been evaluated the value is returned. The eval function is typically used to evaluate mathematical functions.
x = 5
y = 10
z = eval('x + y')
print(z) # Outputs 15
9. filter()
The Python Filter function, as the name implies is used to filter through iterables or sequences such as lists, tuples and dictionaries. Before calling the filter function however, you need to have an appropriate function created that will be used to test each element in the iterable to see if it qualifies or not. If it passes, it will be returned in the output, else not.
list1 = [3,5,4,8,6,33,22,18,76,1]
result = list(filter(lambda x: (x%2 != 0) , list1))
print(result)
9. float()
float() is a built-in Python function that converts a number or string to a float value and returns the result. If, due to invalid input this conversion fails, the Exception for ValueError or TypeError is thrown by the Python interprete.
>>> float('356')
356.0
>>> float(56)
56.0
10. hash()
Most objects in python have a “hash” number. The hash() function returns this hash value when it is applied to one of these objects. Objects with the __hash__() are the ones that have a hash value.
>>> hash('Hello World')
-2864993036154377761
>>> hash(True)
1
11. help()
The Python help() function provides an easy and internet free way to access the python documentation for a specific function, keyword, class or module.
>>> help(print)
12. input()
Python gives us the Built in Function input() as a quick and easy way to take input from the user. Calling this function gives the user the opportunity to enter on screen his input. The user’s input is then converted to a string and returned to the python program.
value = input("Please enter a value: ")
13. int()
The Python int function is used to return an integer from a the object passed into it’s parameters. It can also be used to convert numbers of different bases, (hexadecimal or binary) into integer.
>>> int(5.6)
5
>>> int('0101', 2)
5
14. iter()
The Python iter() function takes as input an object and returns an iterable object. This iterable object is useless on its own, but when used with something like a for loop or while loop, it becomes a powerful weapon. The iterable object it creates can be iterated over one element at a time.
lis = ['a','b','c','d','e']
X = iter(lis)
print(next(X), 'End')
print(next(X), 'End')
print(next(X), 'End')
print(next(X), 'End')
print(next(X), 'End')
print(next(X), 'End')
15. max()
The Python max function is used to find the ‘max’ value of a specific sequence, iteratable and more. By changing it’s parameters we can even change how this max value is calculated.
>>> max('a','A')
'a'
>>> x = [5,7,8,2,5]
>>> max(x)
8
>>> x = ["Apple", "Orange", "Automobile"]
>>> max(x, key = len)
'Automobile'
16. min()
The Python min function is used to find the ‘min’ value of a specific sequence or iteratable. By changing it’s parameters we can even change how this min value is calculated.
>>> min('a','A')
'A'
>>> x = [5,7,8,2,5]
>>> min(x)
2
>>> x = ["Grape", "Mango", "Fruit", "Strawberry"]
>>> min(x)
'Fruit'
17. len()
As the name suggests, the Python len Function is used to calculate the length of an sequence or iteratable. The len function will count the number of elements within the sequence or iteratable.
>>> X = (2, 3, 1, 6, 7)
>>> len(X)
5
>>> len("String")
6
18. list()
This article covers the use of the Python list function. The Python list function takes as input an iterable and returns a object of type list. This function allows for greater flexibility and speed while creating lists objects as opposed to creating them normally.
>>> list("Hello")
['H', 'e', 'l', 'l', 'o']
>>> list({1:"a", 2:"b", 3:"c"})
[1, 2, 3]
19. map()
The map() function is used apply a specified function to a given iterable and return the result in the form of an iterable object (lists, tuples, sets). There may be more than iterable passed into the map() function, hence the dots. However, the more iterables, the more parameters the given function must have.
def inc(x):
x = x + 1
return x
lis = [1,2,3,4,5]
result = map(inc,lis)
for x in result:
print(x)
2
3
4
5
6