Informatics Practices

Write the output of the following:

for j in range (10,6,-2) :
   print (j*2)

Python Control Flow

2 Likes

Answer

20
16

Working

1. range(10, 6, -2): This generates a sequence of numbers starting from 10 (inclusive) and decreases by 2 each time, stopping before it reaches 6 (exclusive).

The general form of the range function is range(start, stop, step), where:

  • start is the starting number of the sequence.
  • stop is the endpoint (the number is not included in the sequence).
  • step specifies the increment (or decrement, if negative) between each number in the sequence.

2. The sequence generated by range(10, 6, -2) is:

  • Start at 10.

  • Decrement by 2: 10 - 2 = 8.

  • The next decrement would be 8 - 2 = 6, but 6 is not included as it is exclusive.

    So, the sequence generated is: 10, 8.

3. for j in range(10, 6, -2):: The for loop iterates over each number in the sequence generated by the range function.

4. print(j * 2): This prints the value of j multiplied by 2 for each iteration.

Let’s analyze the loop iteration by iteration:

First iteration (j = 10):

print(10 * 2)

This prints:

20

Second iteration (j = 8):

print(8 * 2)

This prints:

16

Putting it all together, the full output will be:

20
16

Answered By

2 Likes


Related Questions