Python Fizzbuzz One-liner
Here’s fizzbuzz in one line of Python:
This illustrates several interesting features of Python:
- When you apply
*
to astr
andint
, you will get thestr
repeated the number of times of theint
- When you apply
*
to astr
andbool
, it will give you either0
or1
copies of thestr
, becausebool
is a subclass ofint
in Python
- When you apply
- When you apply
+
to twostr
s, you get thestr
s concatenated together - You can
or
togetherstr
s and they will be interpreted based on Python truthiness rules. That is, an empty string isFalse
and everything else isTrue
.- Furthermore, the result of
or
ing together any data types will not always beTrue
orFalse
, but one of the inputs of theor
expression.x or y
really meansx if x else y
.
- Furthermore, the result of