首先你要了解一下 Python 之禪,一行程式碼輸出 “The Zen of Python”:

python -c “import this”
“””
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren’t special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one– and preferably only one –obvious way to do it.
Although that way may not be obvious at first unless you’re Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it’s a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea — let’s do more of those!
“””
從 “The Zen of Python” 也能看出,Python 倡導 Beautiful、Explicit、Simple 等原則,當然我們接下來要介紹的一行 Python 能實現哪些好玩的功能,可能和 Explicit 原則相違背。

如果你有其他這方面的小例子,也歡迎評論,我會加到文章中,文章也許會長期更新。
(1) 一行程式碼啟動一個 Web 服務
python -m SimpleHTTPServer 8080 # python2
python3 -m http.server 8080 # python3

(2) 一行程式碼實現變數值互換
a, b = 1, 2; a, b = b, a
(3) 一行程式碼解決 FizzBuzz 問題:
FizzBuzz 問題:列印數字 1 到 100, 3 的倍數列印 “Fizz”, 5 的倍數列印 “Buzz”, 既是 3 又是 5 的倍數的列印 “FizzBuzz”
for x in range(1, 101): print(“fizz”[x % 3 * 4:]+”buzz”[x % 5 * 4:] or x)

(4) 一行程式碼輸出特定字元”Love” 拼成的心形
print(‘n’.join([”.join([(‘Love'[(x-y) % len(‘Love’)] if ((x*0.05)**2+(y*0.1)**2-1)**3-(x*0.05)**2*(y*0.1)**3 <= 0 else ‘ ‘) for x in range(-30, 30)]) for y in range(30, -30, -1)])) (5) 一行程式碼輸出 Mandelbrot 影象 Mandelbrot 影象:影象中的每個位置都對應於公式 N=x+y*i 中的一個複數 print(‘n’.join([”.join([‘*’if abs((lambda a: lambda z, c, n: a(a, z, c, n))(lambda s, z, c, n: z if n == 0 else s(s, z*z+c, c, n-1))(0, 0.02*x+0.05j*y, 40)) < 2 else ‘ ‘ for x in range(-80, 20)]) for y in range(-20, 20)])) (6)一行程式碼列印九九乘法表 print(‘n’.join([‘ ‘.join([‘%s*%s=%-2s’ % (y, x, x*y) for y in range(1, x+1)]) for x in range(1, 10)])) (7)一行程式碼計算出1-100之間的素數(兩個版本) print(‘ ‘.join([str(item) for item in filter(lambda x: not [x % i for i in range(2, x) if x % i == 0], range(2, 101))])) print(‘ ‘.join([str(item) for item in filter(lambda x: all(map(lambda p: x % p != 0, range(2, x))), range(2, 101))])) (8)一行程式碼輸出斐波那契數列 print([x[0] for x in [(a[0], a.append([a[1], a[0]+a[1]])) for a in ([[1, 1]], ) for i in range(30)]]) (9)一行程式碼實現快排演算法 qsort = lambda arr: len(arr) > 1 and qsort(list(filter(lambda x: x <= arr[0], arr[1:]))) + arr[0:1] + qsort(list(filter(lambda x: x > arr[0], arr[1:]))) or arr

(10) 一行程式碼解決八皇后問題

[__import__(‘sys’).stdout.write(‘n’.join(‘.’ * i + ‘Q’ + ‘.’ * (8-i-1) for i in vec) + “n========n”) for vec in __import__(‘itertools’).permutations(range(8)) if 8 == len(set(vec+i for i in range(8))) == len(set(vec-i for i in range(8)))]

(11) 一行程式碼實現陣列的 flatten 功能: 將多維陣列轉化為一維

flatten = lambda x: [y for l in x for y in flatten(l)] if isinstance(x, list) else [x]

(12) 一行程式碼實現 list, 有點類似與上個功能的反功能

array = lambda x: [x[i:i+3] for i in range(0, len(x), 3)]

(13) 一行程式碼實現求解 2 的 1000 次方的各位數之和

print(sum(map(int, str(2**1000))))