Class - 12 CBSE Computer Science Important Output Questions 2025
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)
Python
Linear Lists
1 Like
Answer
[2, 4, 6, 8]
Working
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.
Answered By
3 Likes