CBSE Class 12 Computer Science Question 53 of 68

Data Structures - I : Linear Lists — Question 9

Back to all questions
9
Question

Question 9

Predict the output :

ages = [11, 14, 15, 17, 13, 18, 25]
    print(ages)
Elig = [x for x in ages if x in range(14, 18)] 
print(Elig)
Answer

The above code will raise an error due to an indentation error in line 2.

Explanation

The correct code is:

ages = [11, 14, 15, 17, 13, 18, 25]
print(ages)
Elig = [x for x in ages if x in range(14, 18)] 
print(Elig)
Output
[11, 14, 15, 17, 13, 18, 25]
[14, 15, 17]
  1. ages = [11, 14, 15, 17, 13, 18, 25] — This line initializes a list ages.
  2. print(ages) — This line prints the list ages.
  3. Elig = [x for x in ages if x in range(14, 18)] — This line uses list comprehension to create new list Elig, by taking the values between 14 to 17 from ages list.
  4. print(Elig) — This line prints the list Elig.