CBSE Class 12 Computer Science
Question 55 of 68
Data Structures - I : Linear Lists — Question 11
Back to all questions[2, 4, 6, 8]
def even(n)— This line defines a function namedeventhat takes a parametern.return n % 2 == 0— This line returns True if the input numbernis even (i.e., when n % 2 equals 0), and False otherwise.list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]— This line initializes a list namedlist1with integers from 1 to 9.ev = [n for n in list1 if n % 2 == 0]— This line creates a new listevusing a list comprehension. It iterates over each elementninlist1and includesninevif it's even (i.e., if n % 2 == 0).evp = [n for n in list1 if even(n)]— This line creates a new listevpusing a list comprehension. It iterates over each elementninlist1and includesninevpif the functioneven(n)returns True for thatn. Effectively, this also creates a new list from the even numbers oflist1. Therefore, bothevandevpwill contain same values.print(evp)— This line prints the listevp.