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)
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]
ages = [11, 14, 15, 17, 13, 18, 25]— This line initializes a listages.print(ages)— This line prints the listages.Elig = [x for x in ages if x in range(14, 18)]— This line uses list comprehension to create new listElig, by taking the values between 14 to 17 fromageslist.print(Elig)— This line prints the listElig.
Related Questions
Predict the output of following code if the input is :
(i) 12, 3, 4, 5, 7, 12, 8, 23, 12
(ii) 8, 9, 2, 3, 7, 8
Code :
s = eval(input("Enter a list : ")) n = len(s) t = s[1:n-1] print(s[0] == s[n-1] and t.count(s[0]) == 0)Predict the output :
def h_t(NLst): from_back = NLst.pop() from_front = NLst.pop(0) NLst.append(from_front) NLst.insert(0, from_back) NLst1 = [[21, 12], 31] NLst3 = NLst1.copy() NLst2 = NLst1 NLst2[-1] = 5 NLst2.insert(1, 6) h_t(NLst1) print(NLst1[0], NLst1[-1], len(NLst1)) print(NLst2[0], NLst2[-1], len(NLst2)) print(NLst3[0], NLst3[-1], len(NLst3))Predict the output :
L1= [x ** 2 for x in range(10) if x % 3 == 0] L2 = L1 L1.append(len(L1)) print(L1) print(L2) L2.remove(len(L2) - 1) print(L1)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)