Informatics Practices
Find the output of the following program segment:
for i in range(20, 30, 2):
print(i)
Python Control Flow
1 Like
Answer
20
22
24
26
28
Working
Below is a detailed explanation of this code:
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 therangefunction isrange(start, stop, step),where:
startis the starting number of the sequence.stopis the endpoint (the number is not included in the sequence).stepspecifies the increment between each number in the sequence.
for i in range(20, 30, 2):: Theforloop iterates over each number generated by therangefunction and assigns it to the variableiduring each iteration.print(i): This prints the current value ofifor 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
stopparameter is exclusive of the value.
Therefore, the sequence of numbers is: 20, 22, 24, 26, 28.
Answered By
3 Likes
Related Questions
Give the output of the following:
print(5 % 10 + 10 < 50 and 29 <= 29)What is the difference between else and elif constructs of if statement?
Find the output of the following program segment:
country = 'INDIA' for i in country: print (i)Find the output of the following program segment:
i = 0; sum = 0 while i < 9: if i % 4 == 0: sum = sum + i i = i + 2 print (sum)