Need an explanation to this program ...can't understand | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Need an explanation to this program ...can't understand

https://code.sololearn.com/cN6T6Q1q5D4u/?ref=app

18th Apr 2021, 6:55 AM
Sindhuja Selvakumar
Sindhuja Selvakumar - avatar
3 Answers
+ 5
I'm guessing you understand how the for loop works, but just need this part explained? ["Fizz","",][i%3!=0]+["Buzz",""][i%5!=0] or i It's probably best if you break down into parts. ["Fizz","",] This is just a list with 2 elements. The first with index 0 the second with Index 1 [i%3!=0] This is used to evaluate to True or False, which as an index would be interpreted as 0 or 1, which would select either the string "Fizz" if False or the empty string "" if True. Now you should be able to see that the second part; ["Buzz",""][i%5!=0] Would behave the same way. With the + operator between 2 strings they will be concatenated together. So if the 1st part is False "Fizz" and the second is True "", the result is "Fizz" + "" which finally results in "Fizz". If True and False "" + "Buzz" If False and False "Fizz" + "Buzz" If True and True "" + "" This leaves the last part If the string is anything other than empty then it will be output. If the str is empty then it is Falsy and the number will be output.
18th Apr 2021, 7:14 AM
ChaoticDawg
ChaoticDawg - avatar
+ 3
Here's a similar 1 liner, see if you can analyze it and figure it out. [print("Fizz" * (i % 3 < 1) + (i % 5 < 1) * "Buzz" or i) for i in range(1, 101)]
18th Apr 2021, 7:17 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Tq soo much for the explanation.it is clear now.
18th Apr 2021, 7:23 AM
Sindhuja Selvakumar
Sindhuja Selvakumar - avatar