Python tips

What I thought about writing Python. Update when you think of it.

Make the code easier to read while benefiting from comprehension

Reference) Detailed python comprehension

fizzbuzz=[]
for i in range(1,16):
    if i%15==0:
        fizzbuzz.append("fizzbuzz")
    elif i%3==0:
        fizzbuzz.append("fizz")
    elif i%5==0:
        fizzbuzz.append("buzz")
    else:
        fizzbuzz.append(i)
#>>> [1, 2, 'fizz', 4, 'buzz', 'fizz', 7, 8, 'fizz', 'buzz', 11, 'fizz', 13, 14, 'fizzbuzz']

If this is intensional notation

["fizzbuzz" if i%15==0 else "fizz" if i%3==0 else "buzz" if i%5==0 else i for i in range(1,16)]
#>>> [1, 2, 'fizz', 4, 'buzz', 'fizz', 7, 8, 'fizz', 'buzz', 11, 'fizz', 13, 14, 'fizzbuzz']

It's hard to read because it says the road to darkness, but maybe it's still readable if you set it below.

["fizzbuzz" if i % 15 == 0
else "fizz" if i % 3 == 0
else "buzz" if i % 5 == 0
else i
for i in range(1, 16)]

The for line comes with an iterator, so if you want to know the contents of i, look at the bottom. Above that is the value when the conditional expression is satisfied.

Recommended Posts

python tips
Python Tips
Python tips
Python Conda Tips
Python debugging tips
Python click tips
Unexpectedly (?) Python tips
Python and numpy tips
Python
Python Tips (my memo)
[Blender x Python] Blender Python tips (11/100)
Python / Numpy np.newaxis thinking tips
Receiving standard input tips @ python
[Python + Selenium] Tips for scraping
Google Drive Api Tips (Python)
~ Tips for beginners to Python ③ ~
kafka python
Python basics ⑤
python + lottery 6
Built-in python
Python comprehension
Python technique
Studying python
Python 2.7 Countdown
Python memorandum
Python FlowFishMaster
Python service
python function ①
Python basics
Python memo
Tips on Python file input / output
ufo-> python (3)
Python comprehension
Jupyter Tips 4
install python
Python Singleton
Python basics ④
Python Memorandum 2
python memo
Python Jinja2
Python increment
numpy tips
Installing Python 3.4.3.
Try python
Python memo
Python iterative
Jupyter Tips 5
Python algorithm
Scapy Tips
[Python] Variables
Python functions
Python sys.intern ()
Python decimals
Jupyter Tips 3
python underscore
Python summary
Start python
[Python] Sort
Note: Python
Python basics ③
Python basics