Python challenge plss Explain for | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
+ 2

Python challenge plss Explain for

x=1 word="python" y="" for i in word: y=y+str(x) x+=1 print(y) Answer 123456 For Abhinaya

1st May 2019, 1:54 PM
Edwin
Edwin - avatar
3 Respuestas
+ 5
Let's start from the loop part. It iterates over every character of the word (python). So there will be 6 iterations which is the length of the word. Inside the loop it add x as a string to y and increment the value of x and it will print y. So actually the answer is like this 1 12 123 1234 12345 123456
1st May 2019, 2:02 PM
Seniru
Seniru - avatar
+ 5
Thanks both of you The explanation Abhinaya
1st May 2019, 2:04 PM
Edwin
Edwin - avatar
+ 3
The code goes through the word python letter by letter, and increases x by one for each letter there (for i in word), then prints y (which is x as a string) plus the new x as a string. i in word = p; y = "1"; x+=1 = 2; i in word = y; y = "1" + "2" = "12" x+=1 = 3; ...and so on, until the end of the word when the loop ends.
1st May 2019, 2:02 PM
Rincewind
Rincewind - avatar