Computer Science
Predict the output of the following code fragments:
x = 10
y = 5
for i in range(x-y * 2):
print (" % ", i)
Python
Python Control Flow
40 Likes
Answer
This code generates No Output.
Working
The x-y * 2
in range(x-y * 2)
is evaluated as below:
x - y * 2
⇒ 10 - 5 * 2
⇒ 10 - 10 [∵ * has higher precedence than -]
⇒ 0
Thus range(x-y * 2)
is equivalent to range(0)
which returns an empty sequence — [ ].
Answered By
21 Likes
Related Questions
Predict the output of the following code fragments:
for z in range(-500, 500, 100): print (z)
Predict the output of the following code fragments:
for y in range(500, 100, 100): print (" * ", y)
Predict the output of the following code fragments:
for x in [1,2,3]: for y in [4, 5, 6]: print (x, y)
Predict the output of the following code fragments:
for x in range(3): for y in range(4): print (x, y, x + y)