In the article on Functions, we came across Python built-in functions which are one of the types of functions. In this article, let us discuss these built-in functions in detail.
Python has a large number of functions called built-in functions that are pre-defined in the Python interpreter to perform some specific tasks. These functions can be used in Python programs by simply calling them with their specific names without having to define them.
Before understanding them in detail, let us see how they make coding much simpler.
Lets say you want to add all the elements of different iterators and print the result one by one. Now assume there are five lists, each containing five elements. Since you now want to add all the elements of each iterators and print the results, an addition operator can help you do this as shown below.
#program to sum all the elements of the iterators without using built-in function list1 = [62, 45, 24, 76, 35] list2 = [275, 38, 567, 97, 30] list3 = [10, 39, 21, 87, 90] list4 = [59, 17, 91, 25, 789] list5 = [49, 589, 235, 47, 76] print(62 + 45 + 24 + 76 + 35) print(275 + 38 + 567 + 97 + 30) print(10 + 39 + 21 + 87 + 90) print(59 + 17 + 91 + 25 + 789) print(49 + 589 + 235 + 47 + 76)
Output: 242 1007 247 981 996
In the print statements of the above code, you need to type all the elements of the iterator with care. If not you may end up with the wrong result. In short, the above code needs a lot of effort from your end.
But, what if a single built-in function does the addition of all elements for you?Yes, Python interpreter has a built-in function named 'sum()'. This function sums up all the elements of the iterator. So, let us rewrite the same code using the built-in function.
#program to sum all the elements of the iterators using built-in function list1 = [62, 45, 24, 76, 35] list2 = [275, 38, 567, 97, 30] list3 = [10, 39, 21, 87, 90] list4 = [59, 17, 91, 25, 789] list5 = [49, 589, 235, 47, 76] print(sum(list1)) print(sum(list2)) print(sum(list3)) print(sum(list4)) print(sum(list5))
Output: 242 1007 247 981 996
Python 3.x version contains 68 built-in functions. Some of them are discussed below.
Name of the function Discription This function returns the binary value of the given input number. This function returns True if the specified object has the specified attribute.
Else it returns False.
This function takes two arguments as inputs and returns a
tuple containing the quotient and remainder when the first argument is divided by the second argument.
#prints the positive value of the given input number print(abs(-30)) list1 = ["FACE Prep", "Python"] print(all(list1)) #prints 'True' since all the elements of the iterator are 'True' list1 = ["FACE Prep", 0] print(any(list1)) #prints 'True' since one element of the iterator is 'True' #prints the binary value of 10 print(bin(10)) #prints the boolean value of 0 print(bool(0))
Output: 30 True True 0b1010 False
def x(): a = 5 print(callable(x)) #prints 'True' since a function is callable in Python a = 5 print(callable(a)) #prints 'False' since variable is not callable in Python #prints the equivalent character of the Unicode value 104 print(chr(104)) #prints a complex value with 3 as a real part and 6 as an imaginary part print(complex(3, 6)) class Website: name = "FACE" course = "Python" setattr(Website, "name", "FACE Prep") #sets FACE Prep for the attribute 'name' of the class 'Website' a = getattr(Website, "name") #gets the value stored in the attribute 'name' of the class 'Website' print(a)
Output: True False h (3+6j) FACE Prep
class Website: name = "FACE" course = "Python" print(hasattr(Website, "course")) #prints 'True' since the class 'Website' contains attribute 'course' delattr(Website, "course") #deletes the attribute 'course' from the class 'Website' print(divmod(10, 3)) #prints quotient and remainder in the form of a tuple after dividing 10 by 3 print(format(10, 'o')) #formats 10 into its octal representation 12 list1 = ["FACE", "Python"] a = frozenset(list1) #freezes the list and makes it unchangeable a = "FACE Prep"
Output: True (3, 1) 12 Traceback (most recent call last): File "main.py", line 14, in <module> a = "FACE Prep" TypeError: 'frozenset' object does not support item assignment
Note:format() function is used to format the given input number to any representation such as binary (b), hexadecimal (x), percentage (%), decimal (d), scientific (e), etc.
a = 10 print(id(a)) #prints the memory location of 'a'. Output differs each time when you execute the code def func(a):return len(a) list1 = ["FACE Prep", "Python"] res = map(func, list1) #map() function passes each element of the iterator to the function 'func'. print(list(res)) #prints the length of each element of the iterator in the form of a list tuple1 = (29, 345, 67, 89, 344, 936) print(max(tuple1)) #prints the maximum element of the iterator tuple1 = (29, 345, 67, 89, 344, 936) print(min(tuple1)) #prints the minimum element of the iterator a, b = 10, 2 print(pow(a, b)) #prints the output value when 10 is raised to the power of 2
Output: 139862555624672 [9, 6] 936 29 100
a = range(1, 5, 2) #start number = 1, end number = 5 (exclusive), step count = 2 for res in a: print(res) #starts printing from 1 to 4 with the step count of 2 print(round(30.1678, 2)) #rounds up the given float value to 2 decimal places list1 = [20, 49, 10, 478, 367, 456, 267, 12] print(sum(list1)) #prints the sum of all elements of the iterator help(len) #inbuilt help page of the function 'len' gets printed pgm = 'a = 30nprint(a)' exec(pgm) #executes the program assigned to 'pgm'
Output: 1 3 30.17 1659 Help on built-in function len in module builtins: len(obj, /) Return the number of items in a container. 30