如果人民不欢迎我们, 我们就该下台了!@胡耀邦

Python 一些有趣的技巧!

Python 2017-03-26 浏览量: 1791 字数统计: 174 最后更新: 2017-06-02 19:46

文章目录[显示]

python 如一股清流,可以说屌到飞起,下面咱就来看看一些屌的东西

python2 最简单的web服务

python -m SimpleHTTPServer 8080

E:\test>python -m SimpleHTTPServer 8080
Serving HTTP on 0.0.0.0 port 8080 ...
127.0.0.1 - - [26/Mar/2017 20:18:35] "GET / HTTP/1.1" 200 -

1e6b9c22e92cb64652a25ee39c6acec2.png


小彩蛋

>>> import antigravity

你会发现一个小彩蛋(python主题漫画)的哦!!!!

标题是

I wrote 20 short programs in Python yesterday. It was wonderful. Perl, I'm leaving you.
我昨天用Python写了20个小程序,感觉爽的飞起来了~ Perl 我不要你了

python.png

哈哈哈哈哈哈哈!


一行式

列出当前 文件夹下面所有的文件

[ item for item in os.listdir('.') if os.path.isfile(item)]

列出当前 文件夹下面所有什么什么结尾的文件

[ item for item in glob.glob('*.py')]

列出指定范围所有偶的平方

比如10以内

{ i : i * i for i in range(1, 10) if i % 2 == 0}

列出当前目录下所有文件以及文件的大小

{item: os.path.getsize(item) for item in os.listdir('.') if os.path.isfile(item) }

来一波乘法口诀

>>> 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*1=1 
1*2=2   2*2=4 
1*3=3   2*3=6   3*3=9 
1*4=4   2*4=8   3*4=12  4*4=16
1*5=5   2*5=10  3*5=15  4*5=20  5*5=25
1*6=6   2*6=12  3*6=18  4*6=24  5*6=30  6*6=36
1*7=7   2*7=14  3*7=21  4*7=28  5*7=35  6*7=42  7*7=49
1*8=8   2*8=16  3*8=24  4*8=32  5*8=40  6*8=48  7*8=56  8*8=64
1*9=9   2*9=18  3*9=27  4*9=36  5*9=45  6*9=54  7*9=63  8*9=72  9*9=81

再来一波我们最喜欢的 Pythonic

>>> print'\n'.join([''.join([('Pythonic'[(x-y)%8]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(15,-15,-1)])

76d0e457acfc4186e3841c3f66159e23.png

最后Python哲学

>>> 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!
小蜗牛 说:
Freedom is the source from which all meaning and all values spring .


文章版权归 原文作者所有丨本站默认采用CC-BY-NC-SA 4.0协议进行授权| 转载必须包含本声明,并以超链接形式注明原文作者和本文原始地址: https://www.tougetu.com/2017/03/Python-interesting.html

5 条评论

  1. 小土豆

    列出文件夹下面所有什么什么结尾的文件
    这个你后面的if条件写错了(ノ°ο°)ノ

    1. 小蜗牛

      我把前面改了 哈哈哈~

      1. 小土豆

        呀?咱不是要列出某个文件夹下特定扩展名的文件嘛?这一句不是在列文件大小嘛……ヾ(≧∇≦*)ゝ

        1. 小蜗牛

          哈哈哈,才发现 俺好像误人子弟了

          1. 小土豆

            叫我抓虫小能手!好简洁,我用的是这么一大串,哈哈(/ω\)
            [item for item in os.listdir('./') if 'jpg' in item]

添加新评论

代码 Pastebin Gist 加粗 删除线 斜体 链接 签到