一行Python代码
2016-11-08 21:05
453 查看
输出 “The Zen of Python”
启动一个Web服务
变量值赋值或者值互换
解决FizzBuzz问题:FizzBuzz问题:打印数字1到100, 3的倍数打印“Fizz”, 5的倍数打印“Buzz”, 既是3又是5的倍数的打印“FizzBuzz”
输出"Love"拼成的心形
输出Mandelbrot图像
打印九九乘法表
计算出1-100之间的素数(两个版本)
输出斐波那契数列
解决八皇后问题
写一个计算器
>>> 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!
启动一个Web服务
C:\Users\Administrator>python -m http.server 8080 Serving HTTP on 0.0.0.0 port 8080 ... 然后,在你的浏览器中输入:http://localhost:8080/ 你会发现你的浏览器能正常访问,关掉Python之后就会页面出错
变量值赋值或者值互换
a,b,c,d = 1,2,3,4 #变量赋值 a,b,c,d = d,b,c,a #值的互换
解决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)
输出"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)])) #天地良心,只有一行,没有回车
输出Mandelbrot图像
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)]))
打印九九乘法表
print('\n'.join([' '.join(['%s*%s=%-2s' % (y, x, x*y) for y in range(1, x+1)]) for x in range(1, 10)]))
计算出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))]))
输出斐波那契数列
print([x[0] for x in [(a[i][0], a.append([a[i][1], a[i][0]+a[i][1]])) for a in ([[1, 1]], ) for i in range(30)]])
解决八皇后问题
[__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)))]
写一个计算器
print(input())
相关文章推荐
- Python 一行代码实现5!+4!+3!+2!+1!
- 人生第一份python脚本 ---- 遍历文件夹中文件,删除特定文件中的一行代码
- 一行python代码实现神奇功能
- python一行代码
- Python的神奇一行代码
- 学习Python列表内涵:一行代码搞定双倍超立方数计算
- python 关于git下unix和windows换行符不同导致稍微改动一行代码git diff就是全部文件对比的解决方案
- python一行代码能干什么
- Python实现判断一行代码是否为注释
- python——一行代码将列表里的元素去重
- 一行python代码搞定快排
- 一行代码的Python GUI弹框
- python输出问题,一行代码解决
- python简单速成,一行代码写爬虫
- 使用一行Python代码打印出9*9 乘法表
- Python 一行代码实现5!+4!+3!+2!+1!
- Python一行核心代码实现杨辉三角
- 一行python代码实现树结构
- Python 一行代码可以做的事
- python实战===一行代码就能搞定的事情!