Posts

Showing posts from September, 2020

Day 30 - Learn the concept of comprehension,lambda,map,filter,reduce via simple examples

Image
 # -*- coding: utf-8 -*- """ Created on Tue Sep  8 11:20:22 2020 @author: Rakesh.Ranjan """ def square(x):     return x**2   def even(x):     return x%2 == 0   numbers = [1,2,3,4]  #Comprehension #1. the expression inside the brackets first starts with the operation/output that you desire, and then loops and conditionals occur in the same order of the regular code.  lc_square = [numbers[i]**2 for i in range(len(numbers))] print('Comprehension example:',lc_square) #lambda  #lambda input parameter(s):operation/output  countries = ['India','Pakistan','Bhutan','Nepal','Bangladesh'] func_lambda = lambda x: x.lower() print('lambda example:',func_lambda('India')) #A. Map  #map(function,iterable object)  #1. The function here can be a lambda function or a function object.  #2. The iterable object can be a string, list, tuple, set or dictionary.  lm_map = map(square, numbers)   print('map exa...