首页 > > > 几个小例子告诉你, 一行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原则相违背。
声明:这些小例子都来源于网络(最后给出参考网址),自己抱着学习的态度整理得到(除了FizzBuzz问题,有一次面试被问到了,自己写的)。如果侵犯了作者权益,私信我我会删除掉。按照惯例,先贴代码地址:GitHub - xianhu/LearnPython: 以撸代码的形式学习Python
如果你有其他这方面的小例子,也欢迎评论,我会加到文章中,文章也许会长期更新。
python -m SimpleHTTPServer 8080 # python2
python3 -m http.server 8080 # python3

a, b = 1, 2; a, b = b, aFizzBuzz问题:打印数字1到100, 3的倍数打印“Fizz”, 5的倍数打印“Buzz”, 既是3又是5的倍数的打印“FizzBuzz”
print(' '.join(["fizz"[x % 3 * 4:]+"buzz"[x % 5 * 4:] or str(x) for x in range(1, 101)]))
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 <= else ' ') for x in range(-30, 30)]) for y in range(30, -30, -1)]))
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 == else s(s, z*z+c, c, n-1))(, 0.02*x+0.05j*y, 40)) < 2 else ' ' for x in range(-80, 20)]) for y in range(-20, 20)]))
print('\n'.join([' '.join(['%s*%s=%-2s' % (y, x, x*y) for y in range(1, x+1)]) for x in range(1, 10)]))
print(' '.join([str(item) for item in filter(lambda x: not [x % i for i in range(2, x) if x % i == ], range(2, 101))]))
print(' '.join([str(item) for item in filter(lambda x: all(map(lambda p: x % p != , range(2, x))), range(2, 101))]))
print([x[] for x in [(a[i][], a.append([a[i][1], a[i][]+a[i][1]])) for a in ([[1, 1]], ) for i in range(30)]])
qsort = lambda arr: len(arr) > 1 and qsort(list(filter(lambda x: x <= arr[], arr[1:]))) + arr[:1] + qsort(list(filter(lambda x: x > arr[], arr[1:]))) or arr[__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]+i for i in range(8))) == len(set(vec[i]-i for i in range(8)))]
flatten = lambda x: [y for l in x for y in flatten(l)] if isinstance(x, list) else [x]array = lambda x: [x[i:i+3] for i in range(, len(x), 3)]print(sum(map(int, str(2**1000))))最后推荐一篇文章: ArunRocks - Python One-liner Games
参考:
=============================================================
作者主页:笑虎(Python爱好者,关注爬虫、数据分析、数据挖掘、数据可视化等)
作者专栏主页:撸代码,学知识 - 知乎专栏
作者GitHub主页:撸代码,学知识 - GitHub
欢迎大家拍砖、提意见。相互交流,共同进步!
==============================================================
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/756/viewspace-3142904/ ,如需转载,请注明出处,否则将追究法律责任。