In python, what is returned when evaluating [n for n in range(10) if n % 2]?

Expression “[n for n in range(10) if n%2]” will return a list in one go.
This creates a list of odd numbers between 0 and 9. The for loop goes from 0 to 9 and the number which is not divisible by 2 is added to the list.
[n for n in range(10) if n%2]

This code is a compact version of the code given below.
list=[] for n in range(10): if n%2: list.append(n) list

Subscribe
Login
Please login to comment
0 Discussion