Q.Find the output of the following program segments:
for i in range(20,30,2):
print(i)
country = 'INDIA'
for i in country:
i = 0; sum = 0
while i < 9:
if i % 4 == 0:
sum = sum + i
i = i + 2
print (sum)
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's20.stop: The sequence goes up to, but does not include, this number (exclusive). Here, it's30.step: The increment between numbers in the sequence. Here, it's2.
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:
i = 20:20is the starting value.print(20)executes.i = 22:20 + 2 = 22.print(22)executes.i = 24:22 + 2 = 24.print(24)executes.i = 26:24 + 2 = 26.print(26)executes.i = 28:26 + 2 = 28.print(28)executes.i = 30:28 + 2 = 30. This value is equal to thestopvalue (30), so therange()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:
country = 'INDIA': The string'INDIA'is assigned to the variablecountry.i = 'I': The first character ofcountryis 'I'.print('I')executes.i = 'N': The second character is 'N'.print('N')executes.i = 'D': The third character is 'D'.print('D')executes.i = 'I': The fourth character is 'I'.print('I')executes.i = 'A': The fifth character is 'A'.print('A')executes.- 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.