+ 1

Can anyone explain the output?

The below code prints a = [20, 20], can anyone please explain why so. Code: a = [i // 2 for i in range(0,50) if i // 2 == 20] print(a) I thought the answer would be 20 and the length of a would be 1 for i = 40, not sure where the another 20 came from

28th May 2022, 6:03 PM
Sam
2 Answers
+ 1
# Try to output i as well: a = [(i, i // 2) for i in range(0,50) if i // 2 == 20] print(a)
28th May 2022, 6:15 PM
Lisa
Lisa - avatar
0
Thanks for your suggestion Lisa . I tried and got (40, 20), (41,20) as output. Since it's floor division 41//2 results in 20 as well. Thanks
28th May 2022, 6:34 PM
Sam