David Janes' Code Weblog

October 24, 2009

Fizz Buzz in one line of Python

code fragments,python · David Janes · 5:58 am ·

Since Libin points to “Fizz Buzz” in one line of Ruby, I feel it’s only fair to do it in one line of Python:

print [ not i % 15 and "Fizz Buzz" or not i % 5 and "Buzz" or not i % 3 and "Fizz" or i for i in xrange(1, 101) ]

My preference is to really have a few more brackets in there, for clarity but apparently terseness is considered a virtue in and off itself sometimes. There’s other implementations of this in one line of Python:


No comments yet.

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

Powered by WordPress