Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
FizzBuzz one-liner in Python 3
# Using a lambda function
print(list(map(lambda i: "Fizz"*(i%3==0)+"Buzz"*(i%5==0) or str(i), range(1,101))))
# Using a for loop
for i in range(1, 101): print("Fizz"*(i%3==0)+"Buzz"*(i%5==0) or str(i))
@JonathanDagan

This comment has been minimized.

Copy link

@JonathanDagan JonathanDagan commented Sep 3, 2020

list comprehension verion:

[print("Fizz"*(i%3==0)+"Buzz"*(i%5==0) or i) for i in range(101)]

also the:

str(i)

isn't really necessary

otherwise nice implementation

@leoschur

This comment has been minimized.

Copy link

@leoschur leoschur commented Mar 23, 2021

Neat!
Didn´t like the print in the loop though:

FizzBuzzChallenge = "\n".join(["Fizz"*(i%3==0)+"Buzz"*(i%5==0) or str(i) for i in range(101)])
print(FizzBuzzChallenge)

In this case str(i) is necessary.

@khanfarhan10

This comment has been minimized.

Copy link

@khanfarhan10 khanfarhan10 commented Oct 3, 2021

print('\n'.join(list(map(lambda i: "Fizz"*(i%3==0)+"Buzz"*(i%5==0) or str(i), range(1,int(input())+1)))))
@Dummyapt

This comment has been minimized.

Copy link

@Dummyapt Dummyapt commented Oct 17, 2021

print(list(map(lambda i: "Fizz" * (i % 3 == 0) + "Buzz" * (i % 5 == 0) or str(i), range(0, 101))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment