KnowledgeBoat Logo

Computer Science

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)

Python

Linear Lists

2 Likes

Answer

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

Working

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)
[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.

Answered By

1 Like


Related Questions