Python - Built-in Functions

Built-in functions are those functions that are pre-defined in the Python interpreter and you don’t need to import any module to use them. These functions help to perform a wide variety of operations on strings, iterators, and numbers. For instance, the built-in functions like sum(), min(), and max() are used to simplify mathematical operations.

How to Use Built-in Function in Python?

To use built-in functions in your code, simply call the specific function by passing the required parameter (if any) inside the parentheses. Since these functions are pre-defined, you don’t need to import any module or package.

Example of Using Built-in Functions

Consider the following example demonstrating the use of built-in functions in your code:

# Using print() and len() function

text = "TheMak Pro"

print(len(text)) # Prints 10

In the above example, we are using two built-in functions print() and len().

List of Python Built-in Functions

As of Python 3.12.2 version, the list of built-in functions is given below −

Sr.No.Function & Description
1Python aiter() functionReturns an asynchronous iterator for an asynchronous iterable.
2Python all() functionReturns true when all elements in iterable is true.
3Python anext() functionReturns the next item from the given asynchronous iterator.
4Python any() functionChecks if any Element of an Iterable is True.
5Python ascii() functionReturns String Containing Printable Representation.
6Python bin() functionConverts integer to binary string.
7Python bool() functionConverts a Value to Boolean.
8Python breakpoint() functionThis function drops you into the debugger at the call site and calls sys.breakpointhook().
9Python bytearray() functionReturns array of given byte size.
10Python bytes() functionReturns immutable bytes object.
11Python callable() functionChecks if the Object is Callable.
12Python chr() functionReturns a Character (a string) from an Integer.
13Python classmethod() functionReturns class method for given function.
14Python compile() functionReturns a code object.
15Python complex() functionCreates a Complex Number.
16Python delattr() functionDeletes Attribute From the Object.
17Python dict() functionCreates a Dictionary.
18Python dir() functionTries to Return Attributes of Object.
19Python divmod() functionReturns a Tuple of Quotient and Remainder.
20Python enumerate() functionReturns an Enumerate Object.
21Python eval() functionRuns Code Within Program.
22Python exec() functionExecutes Dynamically Created Program.
23Python filter() functionConstructs iterator from elements which are true.
24Python float() functionReturns floating point number from number, string.
25Python format() functionReturns formatted representation of a value.
26Python frozenset() functionReturns immutable frozenset object.
27Python getattr() functionReturns value of named attribute of an object.
28Python globals() functionReturns dictionary of current global symbol table.
29Python hasattr() functionReturns whether object has named attribute.
30Python hash() functionReturns hash value of an object.
31Python help() functionInvokes the built-in Help System.
32Python hex() functionConverts to Integer to Hexadecimal.
33Python id() functionReturns Identify of an Object.
34Python input() functionReads and returns a line of string.
35Python int() functionReturns integer from a number or string.
36Python isinstance() functionChecks if a Object is an Instance of Class.
37Python issubclass() functionChecks if a Class is Subclass of another Class.
38Python iter() functionReturns an iterator.
39Python len() functionReturns Length of an Object.
40Python list() functionCreates a list in Python.
41Python locals() functionReturns dictionary of a current local symbol table.
42Python map() functionApplies Function and Returns a List.
43Python memoryview() functionReturns memory view of an argument.
44Python next() functionRetrieves next item from the iterator.
45Python object() functionCreates a featureless object.
46Python oct() functionReturns the octal representation of an integer.
47Python open() functionReturns a file object.
48Python ord() functionReturns an integer of the Unicode character.
49Python print() functionPrints the Given Object.
50Python property() functionReturns the property attribute.
51Python range() functionReturns a sequence of integers.
52Python repr() functionReturns a printable representation of the object.
53Python reversed() functionReturns the reversed iterator of a sequence.
54Python set() functionConstructs and returns a set.
55Python setattr() functionSets the value of an attribute of an object.
56Python slice() functionReturns a slice object.
57Python sorted() functionReturns a sorted list from the given iterable.
58Python staticmethod() functionTransforms a method into a static method.
59Python str() functionReturns the string version of the object.
60Python super() functionReturns a proxy object of the base class.
61Python tuple() functionReturns a tuple.
62Python type() functionReturns the type of the object.
63Python vars() functionReturns the dict attribute.
64Python zip() functionReturns an iterator of tuples.
65Python import() functionFunction called by the import statement.
66Python unichr() functionConverts a Unicode code point to its corresponding Unicode character.
67Python long() functionRepresents integers of arbitrary size.

Built-in Mathematical Functions

There are some additional built-in functions that are used for performing only mathematical operations in Python, they are listed below −

Sr.No.Function & Description
1Python abs() functionThe abs() function returns the absolute value of x, i.e. the positive distance between x and zero.
2Python max() functionThe max() function returns the largest of its arguments or largest number from the iterable (list or tuple).
3Python min() functionThe function min() returns the smallest of its arguments i.e. the value closest to negative infinity, or smallest number from the iterable (list or tuple)
4Python pow() functionThe pow() function returns x raised to y. It is equivalent to xy. The function has third optional argument mod. If given, it returns (xy) % mod value
5Python round() Functionround() is a built-in function in Python. It returns x rounded to n digits from the decimal point.
6Python sum() functionThe sum() function returns the sum of all numeric items in any iterable (list or tuple). An optional start argument is 0 by default. If given, the numbers in the list are added to start value.

Advantages of Using Built-in Functions

The following are the advantages of using built-in functions:

  • The use of the built-in functions simplifies and reduces the code length and enhances the readability of the code.
  • Instead of writing the same logic repeatedly, you can use these functions across different sections of the program. This not only saves time but also helps in maintaining consistency of code.
  • These functions provide a wide range of functionalities including mathematical operations, datatype conversion, and performing operations on iterators.
  • These functions have descriptive names that make the code easier to understand and maintain. Developers need not write additional complex code for performing certain operations.

FAQs about Built-in Functions

How do I handle errors with built-in functions?

While working with built-in functions, you may encounter errors and to handle those errors you can use the try-except blocks. This may help you identify the type of error and exceptions raised.

Can we extend the functionality of built-in functions?

Yes, we can extend the functionality of built-in functions by using it with other methods and by applying your logic as per the need. However, it will not affect the pre-defined feature of the used function.

Can I create my built-in functions?

No, you cannot create your built-in function. But, Python allows a user to create user-defined functions.

How do I use built-in functions?

Using a built-in function is very simple, call it by its name followed by parentheses, and pass the required arguments inside the parentheses.

© 2024 All rights reserved. | Made With 🤍 By The_MAK Team