CBSE Class 11 Informatics Practices Question 43 of 75

Conditional and Looping Constructs — Question 15

Back to all questions
15
Question

Question 6(a)

Find the output of the following program segment:

for i in range(20, 30, 2):                        
   print(i)                                         
Answer
Output
20
22
24
26
28
Explanation

Below is a detailed explanation of this code:

  1. range(20, 30, 2): This generates a sequence of numbers starting from 20 (inclusive) up to but not including 30 (exclusive), incrementing by 2 each time.
    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 between each number in the sequence.
  2. for i in range(20, 30, 2):: The for loop iterates over each number generated by the range function and assigns it to the variable i during each iteration.

  3. print(i): This prints the current value of i for each iteration.

Let's list the numbers generated by range(20, 30, 2):

  • Start at 20.
  • Increment by 2: 20 + 2 = 22.
  • Increment by 2: 22 + 2 = 24.
  • Increment by 2: 24 + 2 = 26.
  • Increment by 2: 26 + 2 = 28.
  • The next increment would be 28 + 2 = 30, but 30 is not included as the stop parameter is exclusive of the value.

Therefore, the sequence of numbers is: 20, 22, 24, 26, 28.