EXTRA BUILT-IN FUNCTIONS IN PYTHON
1) Lambda – They are anonymous functions. It is used to define an unnamed
function. It accepts any count of inputs but only evaluates and returns one
output.
X=lambda a:a+10
print(X(5))
O/P: 15
2) Maps – This function executes a specified function for each item in an iterable.
def myfunc(n):
return len(n):
x=map(myfunc, (‘apple’, ‘banana’, ‘cherry))
O/P: <map object at 0x056D44F0> [5,6,6]
3) Filter – This function returns an iterator where the items are filtered through a
function to test if the item is accepted or not.
def find_even(num):
return num%2==0
numbers=[1,2,3,4,5,6,7,8,9]
even_list= list(filter(find_even, numbers))
print(even_list)
O/P: [2,4,6,8]
4) Itertools – It is a module that provides us a collection of functions to handle
iterators.
from itertools import (function_name)
5) Iter – This function is used to return an iterator for the object. It is used to
create an object that will iterate one element at a time.
phones=[‘apple’, ‘samsung’, ‘oneplus]
phones_iter= iter(phones)
print(next(phones_iter))
print(next(phones_iter))
print(next(phones_iter))
O/P: apple
samsung
oneplus
6) Bin – This function returns the binary version of a specified integer. The
output always starts with 0b.
X=bin(36)
print(X)
O/P: 0b100100
7) Casefold – This function returns a string where all the characters are
lowercase.
text=’pYThoN’
st=text.casefold()
print(st)
O/P: python
8) Divmod – This function returns a tuple containing the quotient and the
remainder when arg1 (dividend) is divided by arg2 (divisor).
print(divmod(10,3))
O/P: (3,1)
9) Enumerate – This function converts a data collection object into an enumerate
object. It returns an object that contains as a key for each value within an
object, making items within the collection easier to access.
A=[2,5,31]
print(list(enumerate(A))
O/P: [(0,2), (1,5), (2,31)]
10) Eval – This function evaluates the specified expression. If the expression is a
legal python statement, it will be executed.
print(eval(‘2**8’)
O/P: 256
11)Format – This function returns a formatted representation of a given value
specified by the format specifier. The placeholder is defined using curly
brackets ({}).
st=’This page is written in {}’
print(st.format(‘Python’))
O/P: This page is written in Python.
12) Next – This function returns the next item in an iterator. You can add a
default return value, to return if the iterable has reached its end.
L=[1,2,3]
L.iter=iter(1)
print(next(L.iter))
O/P: 1
13)Oct – This function converts an integer into an octal string. The output starts
always with 0o.
X=oct(255)
print(X)
O/P: 0o377
14) Hex – This function converts the specified number into a hexadecimal value.
The output always starts with 0x.
X=hex(255)
print(X)
O/P: 0xff
15) Pow – This function returns the value of x to the power of y. If a third
parameter is present, it returns x to the power of y, modulus z.
print(pow(2,3)) O/P: 8
print(pow(2,3,3)) O/P: 2
16) Reverse – This function reverses the sorting order of the elements.
print(reverse([1,2,3])
O/P: [3,2,1]
17) Reversed – This function returns an iterator that accesses the given sequence
in the reverse order.
print(list(reversed(‘Shashank”)))
O/P: [‘k’, ‘n’, ‘a’, ‘h’, ‘s’, ‘a’, ‘h’, ‘s’]
18) Sort – This function sorts the list in ascending order by default.
cars=[‘Ford’, ‘BMW’, ‘Volvo’]
cars.sort()
print(cars)
O/P: [‘BMW’, ‘Ford’, ‘Volvo’]
19) Sorted – This function returns a sorted list of the specified iterable object. You
can specify ascending or descending order.
A=(‘h’, ‘b’, ‘f’)
X=sorted(A, reverse=True)
print(X)
O/P: [‘h’, ‘f’, ‘b’]
20) Sum – This function returns a number, the sum of all items in an iterable.
A=(1,2,3,4,5)
print(sum(A,7))
O/P: 22
21) Slice – This function returns a slice object. You can specify where to start the
slicing, where to finish and also the step.
A=(‘a’, ‘b’, ‘c’, ‘d’)
X=slice(2)
print(A[X])
O/P: (‘a’, ‘b’)
X---------------------------X