# Python Golf

• Using my old CheckiO solution:

``````def fizzBuzz(self, n):
return['FizzBuzz'[i%-3&-4:i%-5&8^12]or`i`for i in range(1,n+1)]
``````

Maybe I could shorten it to use `range(n)`, but as you can tell from my above link, that was exhausting enough :-)

And a cleaner one I once saw somewhere:

``````def fizzBuzz(self, n):
return ['Fizz' * (not i % 3) + 'Buzz' * (not i % 5) or str(i) for i in range(1, n+1)]``````

• great solution!

• @StefanPochmann haha great, was able to do just this

``````class Solution(object):
def fizzBuzz(self, n):
return [str(i) * (i % 3 != 0 and i % 5 != 0) + "Fizz" * (i % 3 == 0) + "Buzz" * (i % 5 == 0)
for i in range(1, n + 1)]

``````

• The `or` in second solution is brilliant. My original solution was

``````class Solution(object):
def fizzBuzz(self, n):
return ['Fizz'*(not i%3) + 'Buzz'*(not i%5) + str(i)*((i%3 and i%5) > 0) for i in range(1, n+1)]
``````

• The use of "or" is fabulous! As a beginner of Python, I wonder in what circumstances this usage could be applied? Does the "or" means if the previous is equal to "0", then we will return str(i)? Thank you!

• @StefanPochmann Thanks a lot! I got it!

• @StefanPochmann My 2¢ by 2c, reads like a book :)

``````def fizzBuzz(self, n):
return['Fizz'*(i%3/2)+'Buzz'*(i%5/4)or`i+1`for i in range(n)]
``````

or even better:

``````def fizzBuzz(self, n):
return[i%3/2*'Fizz'+i%5/4*'Buzz'or`i+1`for i in range(n)]
``````

• This post is deleted!

• I couldn't understand the first solution but the second solution is a great way of doing it.

• I saw a good solution somewhere:

``````return ["Fizz"[num%3*4::]+"Buzz"[num%5*4::] or num for num in range(1,n+1)]
``````

• @StefanPochmann
are you kidding me?great jobs!

• Good solution!

• I am totally new in Python. Could anybody explain [i%-3&-4:i%-5&8^12]
Thank you and I appreciate it.

• @evalai Hi, what does it mean [num%3*4::]. I am still a new python learner. Thanks!

• @DeyanChen let's split into two parts:first,to be clear,[xxx:xxx] is a slice of 'FizzBuzz';Second,it is made up of two parts:i%-3&-4 and i%-5&8^12,the former is either 0 or -4,and the latter is either 4 or 12.So i guess you already how it works,and this is just a simple math trick.Hope you understand

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.