lundi 23 février 2015

Why does it work?

So I'm learning Python and working through a list of program ideas. Of course I wrote the obligatory FizzBuzz, which worked, but was basically if elif else blablabla. I googled it to see if there are other ways and found this dank one-liner:



for i in range(1,101):
print("Fizz" * (i % 3 == 0) + "Buzz" * (i % 5 == 0) or i)


There are no ifs, no elifs, nothing. I googled "string concatenation" and found info on the * symbol, but don't understand how it's working in this case. Can somebody explain?


Aucun commentaire:

Enregistrer un commentaire