Python Built-In Functions | FACE Prep

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.

What are Python Built-In Functions?

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.

Why do we need built-in functions?

Let’s 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.

List of Python Built-In Functions 

abs()

abs() function returns the absolute value of the given input number. In simple, this function converts a negative number into a positive number.

all()

all() function returns True if all elements of an iterable are True and if the iterable is empty. Else for the rest of the cases, it returns False.

any()

any()  function returns True if even one element of an iterable is True and returns False for an empty iterable.

bin()

This function returns the binary value of the given input number.

bool()

This function returns the boolean value of the given input number. If we do not pass anything to this function, it will return False.

callable()

This function returns True if the specified object is callable. In Python, a function is callable and a variable is not callable.

chr()

This function returns the character of the given Unicode value.

complex()

This function takes two values as inputs and returns the complex form of both the values.

setattr()

This function sets the value of the specified attribute of an object.

getattr()

This function returns the value of the specified attribute from an object.

hasattr()

This function returns True if the specified object has the specified attribute. Else it returns False.

delattr()

This function deletes the specified attribute from the specified object. 

divmod()

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.

format()

This function is used to format the given input value to the specified format. The format can be anything like percentage, scientific, etc.

frozenset()

This function returns an unchangeable object of the given input iterable.

id()

This function returns a unique id for the specified object. Id is the memory address of that object.

map()

This function executes another user-defined function for each element in an iterable. The element of the iterable is the argument for that user-defined function.

max()

This function returns the maximum value of an iterable. If the iterable contains a string, then the comparison is done alphabetically.

min()

This function returns the minimum value of an iterable.

pow()

This function returns the value of the first argument raised to the power of the second argument. If we pass the third argument to this function, then the output will be the first argument raised to the power of the second argument, modulus third argument.

range()

This function returns a sequence of numbers starting from 0 to a specified number. By default, each number is incremented by one step. But, we can also explicitly specify the start number and the step count.

round()

This function rounds up the specified float value to the specified number of decimals.

sum()

This function sums up all the elements of the given iterable.

help()

This function calls the Python in-built help system. 

exec()

This function is used to run the code which is created dynamically.

Next, let us discuss Python Built-In Functions with examples.

Python Built-In Functions with Examples

#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[0] = "FACE Prep"
Output:
True
(3, 1)
12
Traceback (most recent call last):

  File "main.py", line 14, in <module>

    a[0] = "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 = 30\nprint(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