Ask Question
14 September, 11:25

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

+3
Answers (1)
  1. 14 September, 14:09
    0
    First we need to understand what "n for n in range (10) " means. Basically, we are iterating through all the values n that are in the range of 10 (meaning 0, 1, 2, 3, ..., 9).

    Next we are evaluating if n%2. The percentage sign is used to represent modulus which is the remainder of when two numbers are divided. In this case, we are dividing n by 2, and the remainder is our result. When dividing by 2, the only possible remainders are 0 or 1, which when used in an if statement represent the boolean vlaues false and true, respectively. We are doing this calculation for each n from 0 to 9, and if the result is true, we output n to an array.

    The numbers that will result in true (a remainder of 1) are the odds numbers. For example, 4/2 = 2 with nothing left over while 5/2 = 2 with 1 left over.

    So the following is returned:

    [1, 3, 5, 7, 9]
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “In Python, what is returned when evaluating [n for n in range (10) if n % 2]? ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers