Python In-Built Function [With Syntax and Examples]
By Rohit Sharma
Updated on Feb 27, 2024 | 11 min read | 7.3k views
Share:
For working professionals
For fresh graduates
More
By Rohit Sharma
Updated on Feb 27, 2024 | 11 min read | 7.3k views
Share:
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()
built-in functions in Python along with its examples.
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:
Learn data science course from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.
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.
Source
upGrad’s Exclusive Data Science Webinar for you –
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.
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.
Our learners also read: Top Python Free Courses
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.
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
print(callable(x))
The output of the program is False.
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.
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.
Python’s sum() function is used for getting the sum of numbers from an iterable source such as a list.
If any item turns out to be true in the iterable, the function any() returns true. Else, the function returns false.
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.
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.
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.
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.
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.
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
1
2
3
4
5
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.
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.
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.
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.
For returning a list of the names in the local scope, the function dir() is used 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.
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.
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.
Check out The Trending Python Tutorial Concepts in 2024
Get Free Consultation
By submitting, I accept the T&C and
Privacy Policy
Start Your Career in Data Science Today
Top Resources