0

why the answer is 8?

for i in range (5): i += i print (i)

15th Jan 2022, 7:26 PM
HamidReza
HamidReza - avatar
2 Answers
+ 3
'i' is a temporary variable, each iteration replaced by the next value from the itearable range(5) so last value 4 is replaced by previous value and it's i+=i cause it's value 4+4=8 , it is retained after loop and it printed. Try to test printing inside loop then you can see each values... Hope it helps...
15th Jan 2022, 7:31 PM
Jayakrishna 🇼🇳
+ 1
Indent the third line (the print (i) statement) and see how the output works: for i in range (5): i += i print (i)
10th Dec 2022, 8:41 AM
TheBubble001
TheBubble001 - avatar