Fizzbuzz in Python (in one line)

I had the opportunity to teach the basics to python beginners, and I had them do it with the aim of getting used to intensional expressions in the Fizzbuzz problem. This time I decided to try from 1 to 30. The environment is Python 3.5.0

Ordinary

fizzbuzz.py


for n in range(1, 31): 
	if n % 3 == 0 and n % 5 == 0:
		print("FizzBuzz")
	elif n % 3 == 0:
		print("Fizz")
	elif n % 5 == 0:
		print("Buzz")
	else:
		print(n)

Example of one-line binding (3 and 5 are annoying, so set it to 15)

fizzbuzz2.py


["FizzBuzz" if n % 15 == 0 else "Fizz" if n % 3 == 0 else "Buzz" if n % 5 == 0 else n for n in range(1,31)]

This is a little improved because it only creates a list and is not displayed

fizzbuzz3.py


print("\n".join(["FizzBuzz" if n % 15 == 0 else "Fizz" if n % 3 == 0 else "Buzz" if n % 5 == 0 else str(n) for n in range(1,31)]))

Since the length of one line ignores PEP8 as much as possible, line breaks should be made appropriately when the inclusion actually becomes long.

FizzBuzz is also possible with the description in shiracamus's comment. It's very tricky, but it feels like python's unique writing style, so if you can see that as well

Recommended Posts

Fizzbuzz in Python (in one line)
FizzBuzz in Python
Make python segfault in one line
CGI server (1) python edition in one line
One liner in Python
Decompose command arguments in one line in Python
[Python] Invert bool value in one line
Make a rock-paper-scissors game in one line (python)
DMD in Python one dimension
Try LINE Notify in Python
Prime number enumeration in one line
I tried Line notification in Python
[Introduction] Insert line breaks in Python 3
Implemented in 1 minute! LINE Notify in Python
Decrypt one line of code in Python lambda, map, list
Handle multiple python versions in one jupyter
Quadtree in Python --2
Python in optimization
CURL in python
[Python] [3D line graph] Multiple data in one graph, axis values in characters
FizzBuzz with Python3
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Line graphs and scale lines in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Read the file line by line in Python
Read the file line by line in Python
Plink in Python
Constant in python
LINE heroku python
One liner webServer (with CGI) in python
Lifegame in Python.
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
In search of the fastest FizzBuzz in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python