Built-in functions in Python are defined as those types of functions that are pre-defined in Python. The programming language Python offers several functions for performing a wide variety of tasks. Those functions which are already present in the Python interpreter are known as the built-in functions. Lots of built-in functions are available in Python. A few of the built-in functions are hash(), filter(), int(), print(), set()
Table of Contents
built-in functions in Python
built-in functions in Python along with its examples.
1. abs function()
Using the abs() function in Python is used for returning the absolute value of a given number. The function takes only one argument: the function’s number to return its absolute value. This argument can be of any type, such as an integer or a floating point. If, supposedly, the argument is a complex number, then the function will return the magnitude of the complex number.
An example of the Python abs() is shown below:
The output of the program will be:
2. all() function
This is another type of built-in function in Python. The function all() accepts an iterable object. Such iterable objects might be a dictionary, list, etc. Using the function all() returns the value of either true or false. If all the elements are valid in the iterable, the function returns true. Else, the function returns false.
An example of the all() function in Python is shown below.
3. bin() function in Python
Python’s bin() function is used for any specified integer to return that integer’s binary representation. The results of the function always have the prefix 0b.
An example of the function bin() in Python is shown below.
4. bool() in Python
This is another type of built-in function in Python. As the function’s name goes, the function converts any value to the Boolean form that means true or false. This is done through the use of the standard procedure of truth testing.
An example of the function is shown below.
The output of the program is shown below.
5. bytes() in Python
The function bytes() is used in Python for returning an object of byte. The function is immutable compared to the bytearray() function in Python. Empty bytes are created for an object of a specified size.
An example of the Python bytes() function I showed below.
6. Callable() function in Python
The callable() function, as the name specifies, can be called in the Python program. The function’s task is to check and then return the true value if the object passed through the function is callable. Else, the function returns the value of false.
An example of the callable() function in Python is
x = 8
The output of the program is False.
7. Compile() function
The function compiles()in Python uses the input as source code and then returns an object of the code, which can be executed later through the function exec().
The compile() function in Python can be used as shown below.
8. Exec() function
The function exec() in Python is used for executing the Python program dynamically. It can be an object or a string, and the function accepts large segments of code compared to the function eval(), which accepts only a single expression.
9. Sum() function
Python’s sum() function is used for getting the sum of numbers from an iterable source such as a list.
10. Any() function in Python
If any item turns out to be true in the iterable, the function any() returns true. Else, the function returns false.
11. Ascii() function in Python()
This is another type of built-in function in Python. It returns a string that contains object representation and then escapes the characters in the string, which are non-ASCII using \u, \U, or \x escapes.
12. Bytearray() in Python
The function bytearray() in Python returns an object of bytearray and can also carry out conversion of objects into byte arrays. It is also able to create an object of byte array of any specific size which is empty.
13. Eval() function in Python
The function eval() in Python performs the task of parsing the expression passed on to it. The function also runs the expression(code) in Python that is present within the code.
14. Float function in Python
The built-in function float() in Python returns a floating-point number from a string or a number.
An example of the float() function in Python is shown below.
15. globals() function in Python
The function globals() in Python returns a dictionary for the current global symbol table. The term Symbol table means a type of data structure that stores all the information necessary for the program. The symbol table includes the names of variables, classes, methods, etc.
16. iter() function in Python
If the program has to return an object of the type iterator, then Python’s function iter() can be used. The function leads to creating an object that one element can iterate at a time.
An example of the function iter() is shown below.
The output of the program is
17. locals() function in Python
The built-in function of Python i.e., locals() is used to return a local symbol table dictionary. It is also used for the modification of the dictionary.
An example of the locals() function in Python is shown below.
18. Chr() function in Python
The function chr() is used in Python to achieve a string representing a character that further points to an integer of Unicode code. If the user uses the function chr(97), the function will return a string “a”. The function will throw an error taking an argument of the integer type that exceeds the range specified. Usually, the standard range that is specified in the argument ranges from 0-1,114,111.
An example of code using the chr() function in Python is shown below.
19. Complex() function in Python
This built-in function in Python is used for the conversion of numbers into a complex number. The function complex() is also able to convert the strings into a complex number. The first parameter is the real parts, and the second parameter is the imaginary parts.
20. delattr() function in Python
If the user wants to delete any attribute from a specified class in Python, the function delattr() can be used. The function takes two parameters. The first parameter taken is an object from the class, and the second parameter taken is the attribute that needs to be deleted. Once an attribute gets deleted, there is no more presence of the attribute in the class. After deletion of the attribute, if the attribute is tried to be called through the object of the class, then the program throws an error.
An example of code showing the use of the function delattr() in Python is shown below.
21. dir() function in Python
For returning a list of the names in the local scope, the function dir() is used in Python.
22. divmod() function in Python
In Python, the function divmod() is used for getting the quotient and the remainder of two numbers. The function takes two arguments in the numeric form, and a tuple is returned. For proper functioning, the function should have both arguments and it should be numeric.
23. dict() function in Python
The function dict() in Python acts as a constructor that leads to creating a dictionary. The Python dictionary provides three different types of constructors for creating a dictionary.
- An empty dictionary is created when no arguments are passed.
- The same pair of key-value dictionaries are created when positional arguments are used.
- In case of the keyword arguments, the arguments of the keyword and the values are further added to the created dictionary from positional arguments.
Apart from the above-mentioned built-in functions, Python has many other built-in functions specially designed to perform special functions. If you want to know more about the Python built-in functions and want to have practical knowledge of the programming language, check the Online Data Science Program offered by upGrad. The certificate course is provided in association with the IIIT Bangalore. Further details of the course can be explored on our website. Also, if any assistance is required, our team may be contacted for help.
What does the built-in function in Python mean?
The built-in functions in Python are predefined functions in Python.
What do the built-in abs() function do?
The function helps in returning the absolute value for any given number.
In Python, how many built-in functions are present?
There are around 69 built-in functions in Python.