You are going to print the string "Buzz" either once if (i % 5 == 0) is True, or return i :
In [5]: "foo" * 2 Out[5]: 'foofoo' In [6]: "foo" * 3 Out[6]: 'foofoofoo' In [7]: i = 5 In [8]: "foo" * (i % 5 == 0) or i Out[9]: 'foo' In [9]: "foo" * (i % 5 == 1) or i Out[22]: 5
The same applies to "Fizz", sometimes (i % 3 == 0) will be True, so we see it once, when it is False, we do not see it.
When you use the * operator in a line, it will repeat the line n times, in which case it will be no more, because either the line will be printed only based on the result of a boolean test.
In ipython, you can see what happens with True and False :
In [26]: "foo" * True Out[26]: 'foo' In [27]: "foo" * False Out[27]: ''
Basically True * "foo" equivalent to 1 * "foo" "foo" * False equivalent to 0 * "foo"
Bool is a subclass of int, so the code uses this fact, sometimes you see the same logic used to index the list based on the test, but it is not recommended:
In [31]: d = ["bar","foo"] In [32]: d[3<2]
source share