Computer Science, asked by Jamesbawngkawn1035, 10 months ago

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

Answers

Answered by Anonymous
13

Answer:

Explanation:

[n for n in range(10) if n%2]

Thus,

>>> [n for n in range(10) if n % 2]

[1, 3, 5, 7, 9]

The expression will generate a comprehension list, which means that the program will creat a list in one go. It will give a list of integers between 0 to 9 inclusive - if n is even, then n%2 = 0, otherwise it will be = 1.

Thus, the list produced when conditional evaluates to true (=1) is [1, 3, 5, 7, 9].

The % sign in the code is the modulus operator, generating the remainder when dividing the left operand by the right operand. When an odd number will be divided by 2, the remainder will always be 1, and when an even number will be divided the remainder will be zero.

Hence, nothing is returned while the python is evaluating the expression.

Answered by Arslankincsem
3

Answer:

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

The list is created in a single go. The code generates the list of all the odd numbers starting from 0 and up to 9. The loop goes on from 0 to 9 and tracks down a number each time in case it is not divisible by the number 2. This gives the result in the output format 1, 3, 5, 7, and 9.

Similar questions