Tweetable #Python — Tweetable Python 3.7 documentation


(128 Python programs in 280 chars or less)

_images/book-cover-twp.png

Each programs in this books fits in a single tweet (280 chars or less) and does something useful, powerful or fun.

These programs will serve a reference, inspire and delight. In most cases, but not always, the code is Pythonic, without any obfuscation or code golfing.

These programs serve as a testament to Python’s power, conciseness and clarity

Take this example

import random, string
"".join( [random.choice(string.ascii_letters + string.digits) for i in range(8)]
)

In less than a 100 chars, you can generate truly random passwords.

Now look at this function which fits a tweet.

ch= "█"
def col(sz): mn,mx=min(sz),max(sz) df = (mx-mn)//8 bkt = [(el-mn)//df for el in sz] hrz = [f"{b}{c}" for b,c in [(ch*(el+1)," "*(8-el)) for el in bkt] ] return "\n".join([" ".join(el) for el in list(map(list, zip(*hrz)))[::-1]])

You can now generate a columns chart for any integer series, visible on terminals.

In [2]: import random In [3]: series = [random.randint(10, 99) for _ in range(25)] In [4]: print(col(series)) █ █ █
█ █ █ █ █ █
█ █ █ █ █ █ █ █ █ █
█ █ █ █ █ █ █ █ █ █ █
█ █ █ █ █ █ █ █ █ █ █ █
█ █ █ █ █ █ █ █ █ █ █ █
█ █ █ █ █ █ █ █ █ █ █ █ █ █ █
█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █
█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █

We will look at things like file manipulation, file conversion, password generation, ascii art and even some games. This book is inspired by Peteris Krumins’ Perl One Liners.

Note

Judge me by my size, do you? Hmm, hmm. - Yoda.

Lets get started.