Some of the Useful Functions in Python 3
The built-in functions come handy when you have to do the same thing again and again. You can just call the function and use it. It becomes difficult sometimes if you do not know where to find which functions are available and how to use it too. Therefore I will talk about a few handy functions with an example each so that you get familiarises with it. Mainly the functions are a lambda, reduce, filter, map, enumerate and zip functions.Lambda Function - It is also called an anonymous function. The function is created without using the keyword 'def'.
Syntax: lambda arguments: expressionExample:
To find the minimum of two numbers. The lambda function will take two arguments and find the minimum of it.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
minimum = lambda x,y: x if x < y else y | |
print(minimum(4,3)) |
Reduce Function - It will use the output of operation as an input to its next operation. In order to use it, you have to import it. The module name is functools from where you can import it and use it.
Syntax : reduce(function, sequence)Example:
To find the factorial of the given positive integer.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from functools import reduce | |
print(reduce(lambda x,y: x*y,range(1,eval(input("Enter the valu n:"))+1))) |
Filter Function - It will filter and do the operation as the term itself says filter. Since filter will perform the same task/operation, again and again, the output will be a list. Therefore we need to store it in a list.
Syntax : filter(function, sequence)Example:
To print the number greater than number 5
Map Function - It is used to perform the same operation again and again over the iteration of the sequence as the term itself says map which means mapping. The output is stored in the list.
Syntax : map(function, sequence)Example:
To get find the square of the given n numbers
Enumerate Function - It will return the index and value from the sequence. The index can be initialized to any.
Syntax : enumerate(sequence,[start])Example:
To display the items in a list with the index number.
Zip Function - It will return two values from two sequences at the same time. It can be used to compare index wise of any elements.
Syntax : zip(sequence1, sequence2)Example:
Two compare in two lists whether the same items are present in the same position or not.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
item1=[1,2,3,4,5] | |
item2=[1,2,6,4,2] | |
for a,b in zip(item1,item2): | |
if a is b: | |
print('match') | |
else: | |
print('mismatch') |