Skip to content
Exercises · Q11

Q.Find the output of the following program segments:

a)
for i in range(20,30,2):
print(i)
b)
country = 'INDIA'
for i in country:
print
(i)
c)
i = 0; sum = 0
while i < 9:
if i % 4 == 0:
sum = sum + i
i = i + 2
print (sum)
Puducherry CbseNCERTSubjective· 3mImportance★★★★★est
96% · 24/25 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

This solution explains the output of three Python code segments: a for loop with range() and step, a for loop iterating over a string, and a while loop with a conditional sum and increment.

Let's analyze each program segment to determine its output. Understanding how loops iterate and how conditional statements affect variable values is key here.

a) for i in range(20,30,2): print(i)

This segment uses a for loop with the range() function. The range(start, stop, step) function generates a sequence of numbers.

  • start: The sequence begins from this number (inclusive). Here, it's 20.
  • stop: The sequence goes up to, but does not include, this number (exclusive). Here, it's 30.
  • step: The increment between numbers in the sequence. Here, it's 2.

The loop will iterate, assigning i to each number generated by range(20, 30, 2), and then print the value of i.

for i in range(20,30,2):
     print(i)

Explanation and Trace:

  1. i = 20: 20 is the starting value. print(20) executes.
  2. i = 22: 20 + 2 = 22. print(22) executes.
  3. i = 24: 22 + 2 = 24. print(24) executes.
  4. i = 26: 24 + 2 = 26. print(26) executes.
  5. i = 28: 26 + 2 = 28. print(28) executes.
  6. i = 30: 28 + 2 = 30. This value is equal to the stop value (30), so the range() function does not include it. The loop terminates.

Output:

20
22
24
26
28

b) country = 'INDIA'; for i in country: print (i)

This segment demonstrates iteration over a string. In Python, strings are iterable sequences of characters. A for loop iterating directly over a string will assign each character of the string, one by one, to the loop variable.

country = 'INDIA'
for  i in country:
     print (i)

Explanation and Trace:

  1. country = 'INDIA': The string 'INDIA' is assigned to the variable country.
  2. i = 'I': The first character of country is 'I'. print('I') executes.
  3. i = 'N': The second character is 'N'. print('N') executes.
  4. i = 'D': The third character is 'D'. print('D') executes.
  5. i = 'I': The fourth character is 'I'. print('I') executes.
  6. i = 'A': The fifth character is 'A'. print('A') executes.
  7. There are no more characters in the string country. The loop terminates.

Output:

I
N
D
I
A

c) i = 0; sum = 0; while i < 9: if i % 4 == 0: sum = sum + i; i = i + 2; print (sum)

This segment uses a while loop, which continues to execute as long as its condition (i < 9) remains true. Inside the loop, there's a conditional statement (if i % 4 == 0) that checks if i is perfectly divisible by 4. If it is, i is added to sum. In every iteration, i is incremented by 2. The final print(sum) statement is outside the loop, meaning it will execute only once after the loop has finished.

i = 0; sum = 0
while i < 9:
     if i % 4 == 0:
               sum = sum + i
     i = i + 2
print (sum)

Explanation and Trace:

Let's trace the values of i and sum through each iteration of the while loop: …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.