Informatics Practices
Write the output of the following:
for x in range (10, 20):
if (x % 2 == 0):
continue
print (x)
Python Control Flow
2 Likes
Answer
11
13
15
17
19
Working
Below is the detailed explanation of this code:
for x in range(10, 20):→ This loop iterates over the range of numbers starting from 10 (inclusive) up to but not including 20 (exclusive). So,xwill take on the values: 10, 11, 12, 13, 14, 15, 16, 17, 18, 19.if x % 2 == 0:→ For each value ofx, this condition checks ifxis an even number. The modulus operator%returns the remainder ofxdivided by 2. If the remainder is 0,xis even.continue→ If the conditionx % 2 == 0is true (i.e.,xis even), thecontinuestatement is executed. This statement skips the rest of the code inside the loop for the current iteration and moves to the next iteration.print(x)→ This line prints the value ofxif it is not even (i.e., if thecontinuestatement is not executed).
Let’s analyze the loop iteration by iteration:
When
x = 10:10 % 2 == 0is true.continueis executed, soprint(x)is skipped.When
x = 11:11 % 2 == 0is false.print(x)outputs:11When
x = 12:12 % 2 == 0is true.continueis executed, soprint(x)is skipped.When
x = 13:13 % 2 == 0is false.print(x)outputs:13When
x = 14:14 % 2 == 0is true.continueis executed, soprint(x)is skipped.When
x = 15:15 % 2 == 0is false.print(x)outputs:15When
x = 16:16 % 2 == 0is true.continueis executed, soprint(x)is skipped.When
x = 17:17 % 2 == 0is false.print(x)outputs:17When
x = 18:18 % 2 == 0is true.continueis executed, soprint(x)is skipped.When
x = 19:19 % 2 == 0is false.print(x)outputs:19
Putting all these outputs together, the full output will be:
11
13
15
17
19
Answered By
1 Like
Related Questions
Write the output of the following:
for x in range (1, 6) : for y in range (1, x+1): print (x, ' ', y)Write the output of the following:
for x in range (10, 20): if (x == 15): break print(x)Write the output of the following program on execution if x = 50:
if x > 10: if x > 25: print ( 'ok' ) if x > 60: print ( 'good' ) elif x > 40: print ( 'average' ) else: print ('no output')Write the output of the following code:
for i in range (2) : for j in range (1) : if i+2 == j: print ("+",end=" ") else: print ("o",end=" ")