0

Explain the code it prints 120 how?

b=3 a=1 for i in range(5): a=a+a*i print(a)

22nd Jan 2021, 4:57 PM
MALLIKHARJUNA
MALLIKHARJUNA - avatar
3 odpowiedzi
+ 11
Initially a = 1 for i = 0 a = 1 + 1 * 0 = 1 Now a = 1 for i = 1 a = 1 + 1 * 1 = 1 + 1 = 2 Now a = 2 for i = 2 a = 2 + 2 * 2 = 2 + 4 = 6 Now a = 6 for i = 3 a = 6 + 6 * 3 = 6 + 18 = 24 Now a = 24 for i = 4 a = 24 + 24 * 4 = 24 + 96 = 120 finally when for loop ends a is 120.
22nd Jan 2021, 5:04 PM
RKK
RKK - avatar
+ 4
a=1+1*0 #1 a=1+1*1 #2 a=2+2*2 #6 a=6+6*3 #24 a=24+24*4 #120 that's why it prints 120
22nd Jan 2021, 5:07 PM
Ratnapal Shende
Ratnapal Shende - avatar
+ 3
What does b do?
23rd Jan 2021, 2:11 AM
David Ashton
David Ashton - avatar