Predict the Output.

Solved33.17K viewsProgrammingPython ( DATA STRUCTURES)

Predict the Output.

def even(n):
return n%2 == 0:
list1 = [1,2,3,4,5,6,7,8,9]
ev = [n for n in list1 if n%2 ==0]
evp = [n for n in list1 if even(n)]
print(evp)

Question is closed for new answers.
Chiikoo Changed status to publish May 11, 2022
5

Hello pallab_bapari,
This program is written in Python language and this code is an example of List Comprehension.

About Program Function
You write a function even() which can take n parameter and function will return True or False as per calculation of n%2==0. Means if we divide n by 2 and get the quotient 0 then function will return True, otherwise it will return False. After that, ev and avp both are doing same work because in ev, you directly use same method to solve query of even() function, so we get ev=evp here. When it fall True, number will be append as value of n otherwise nothing will append and at end ev and evp get his value.

Answer
We will get same output for both ev and evp, but in the program, only evp has printed. so output will be

>>>
[2,4,6,8]

Code My Life - Official Changed status to publish April 27, 2023

thanks for the detailed description 💖

4