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.
The given code will find the minimum of two number 4 and 3 and print the result. You can give any two numbers. It will print the minimum if it.
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.
The above code will find the factorial of the given number and print out the value.
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.
These are the few functions which you can use it whenever you required it and the usage can be far more than what I have shown above. You just need to find out the right place to use it.