In this section of python programming, we will understand the python code to print all the even numbers in the given list.
Expression: num % 2 == 0
input_list = [1, 2, 12, 17, 40] #Using lambda expression new_list = list(filter(lambda x: (x%2 == 0), input_list)) print("Even numbers in the list using lambda expression", new_list) #Using for loop new_list1 =  for i in input_list: if i % 2 == 0: new_list1.append(i) print("Even numbers using for loop", new_list1) #Using while loop new_list2 =  temp = 0 while(temp < len(input_list)): if input_list[temp] % 2 == 0: new_list2.append(input_list[temp]) temp += 1 print("Even numbers using while loop", new_list2)
In the above python code, we have used three methods to find the even numbers in the list. First we will discuss the “for and while loop” which we have used and after that we will discuss the “lambda expression”.
The loops (for and while) iterates over the elements in the list and checks the condition whether the number leaves remainder 0 on being divided by 2. If the condition satisfies, the element is printed.
The same even number property is followed in other method. The lambda expression calculates the value of the element for expression num % 2 and filters out the output if the result is equals to 0 ( i.e. remainder is zero). And the filtered list is printed on the screen.