脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - 有趣的python小程序分享

有趣的python小程序分享

2020-12-22 00:15garfieldtom Python

这篇文章主要介绍了有趣的python小程序分享,具有一定参考价值,需要的朋友可以了解下。

python可以简单优美,也很有趣,下面是收集的例子:

1.一句话开始一个http的文件服务器:

$ python -m SimpleHTTPServer

Serving HTTP on 0.0.0.0 port 8000 ...

在浏览器中就可以http://localhost:8000访问目录及文件了。

也可以直接指定端口:

$ python -m SimpleHTTPServer 6666

如果想在代码中实现,也很简单:

?
1
2
3
4
5
6
7
8
9
10
11
import SimpleHTTPServer
import SocketServer
 
PORT = 8000
 
Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
 
httpd = SocketServer.TCPServer(("", PORT), Handler)
 
print "serving at port", PORT
httpd.serve_forever()

下面是图示:

有趣的python小程序分享

python3下为:python3 -m http.server

2.一行python打印一个迷宫:

?
1
2
3
4
5
6
7
python 2.x:
 
python -c "print ''.join(__import__('random').choice(u'\u2571\u2572') for i in range(50*24))"
 
python 3.x:
 
python -c "print(''.join(__import__('random').choice('\u2571\u2572') for i in range(50*24)))"

自己运行看看吧。

有趣的python小程序分享

3.神奇的代码,生成分形图片

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
_                   =  (
                    255,
                   lambda
                V    ,B,c
               :c  and Y(V*V+B,B, c
                -1)if(abs(V)<6)else
        (       2+c-4*abs(V)**-0.4)/i
         ) ;v,   x=1500,1000;C=range(v*x
         );import struct;P=struct.pack;M,\
      j ='<QIIHHHH',open('M.bmp','wb').write
for X in j('BM'+P(M,v*x*3+26,26,12,v,x,1,24))or C:
      i ,Y=_;j(P('BBB',*(lambda T:(T*80+T**9
         *i-950*T **99,T*70-880*T**18+701*
         T **9   ,T*i**(1-T**45*2)))(sum(
        [       Y(0,(A%3/3.+X%v+(X/v+
                A/3/3.-x/2)/1j)*2.5
               /-2.7,i)**2 for \
                A    in C
                   [:9]])
                    /9)
                    )  )

将上面的代码粘贴到一个文件中,比如1.py,然后运行:

python 1.py

耐心地等等,将会在目录下生成一个漂亮的分形图案!

总结

以上就是本文关于有趣的python小程序分享的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

原文链接:http://www.cnblogs.com/GarfieldTom/archive/2012/12/06/2806278.html

延伸 · 阅读

精彩推荐