背景

python有很多优雅有趣的代码写法,同时还很简短,以至于当我刚开始接触这个编程语言的时候,就爱不释手。而前几天的编程语言榜单中python也超越了java成为了第一,挺替python开心的。

python到底有多有趣呢?一行代码告诉你!

正文

1 画爱心

 print('\n'.join([''.join([('www.h3blog.com'[(x-y)%10]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)]))

输出:

                h3blogwww           h3blogwww               
            ww.h3blogwww.h3bl   ww.h3blogwww.h3bl           
          www.h3blogwww.h3blogwww.h3blogwww.h3blogw         
         www.h3blogwww.h3blogwww.h3blogwww.h3blogwww        
        www.h3blogwww.h3blogwww.h3blogwww.h3blogwww.h       
        ww.h3blogwww.h3blogwww.h3blogwww.h3blogwww.h3       
        w.h3blogwww.h3blogwww.h3blogwww.h3blogwww.h3b       
        .h3blogwww.h3blogwww.h3blogwww.h3blogwww.h3bl       
        h3blogwww.h3blogwww.h3blogwww.h3blogwww.h3blo       
        3blogwww.h3blogwww.h3blogwww.h3blogwww.h3blog       
         logwww.h3blogwww.h3blogwww.h3blogwww.h3blog        
          gwww.h3blogwww.h3blogwww.h3blogwww.h3blog         
          www.h3blogwww.h3blogwww.h3blogwww.h3blogw         
            .h3blogwww.h3blogwww.h3blogwww.h3blog           
             3blogwww.h3blogwww.h3blogwww.h3blog            
              logwww.h3blogwww.h3blogwww.h3blog             
                www.h3blogwww.h3blogwww.h3blo               
                  .h3blogwww.h3blogwww.h3bl                 
                    blogwww.h3blogwww.h3b                   
                       www.h3blogwww.h                      
                          h3blogwww                         
                             ogw                            
                              w                             

这个可谓是表白神器,每逢过节都可以拿出来炫耀下,懂我意思吧?

2 打印99乘法表

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

3 一行代码实现快速排序

qs = lambda xs : ( (len(xs) <= 1 and [xs]) or [ qs( [x for x in xs[1:] if x < xs[0]] ) + [xs[0]] + qs( [x for x in xs[1:] if x >= xs[0]] ) ] )[0]

调用:

>>> qs = lambda xs : ( (len(xs) <= 1 and [xs]) or [ qs( [x for x in xs[1:] if x < xs[0]] ) + [xs[0]] + qs( [x for x in xs[1:] if x >= xs[0]] ) ] )[0]
>>> a = [6,3,4,9,2,5,8,1,7]
>>> qs(a)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> 

4 可以让两个变量交换值

a, b = b, a

这个应该是简单的。也是pythonic风格的。

5 打印python之禅

import this

输出:

>>> 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!
>>> 

这个估计有很多人不知道,你们常说的 “人生苦短,我用python” 这句话就是出自这里的。python的美常常就体现在上面。好的python程序员也应该去遵守它。

6 开启web

python -m http.server 8080

7 打印迷官

print(''.join(__import__('random').choice('/\\') for i in range(50*24)))

输出:

\/////\/\//\\\/\\/\/\\\//\/\////\/////\\//\/\//\\\/\///\/\///\/\/\\\//\//\\//\/\\\//\\///\\\//\///\\/\//\\////\\\//\//\\//\\\/\/\///\\\\\////\//\//\\\/\\\\/\/\\/\/\\\/\///////\\\\/\\\/\\///\/\/\//\/\//\//\\\/\\\//\\\\\//\////\\//\///\///\/\/\/\/\\/////\\\\\/\/\\\\//\/\/\/\/\///\//\//\\\///////\\\////\/\/\//\\//\\//\\\\\/\/\\//////\\\\\/\//\///\/\/\\/\/\\\/\\\\\/\//\\//\///\/\/\\/\////\/\/\\//\\///\\/\//\///\/\///\\/\\\\///\////\/\/\/\//\\////\/\\/\/\//\\\\/\/\\\/\/\//\\\\\/\//\/\//\\\///\/\/\/\\/\/\/\////\/\\///\\/\//\/\\/\//\///\///\\////\/\/\/\/\\//\//////\/\\///////\\\\\\/\/\\\\/\\/\\///\/\/////\/\\\\\//\\\\\\/\/\\\//\\//\/\\\\\////\///\/\\\///\\/\//\\///\\\/\\\/\////\\\////////\\\//\/\\//\\/\\\/////\\\\///\/////\/////\////\\////\/\///\\\/\/\\\/\\//////\\\\/\\\//\//\////\///\\\\//////\/////\\/\///\////\//\\\\/\//\////\\\//\///\\/\/\\\////\\/\\//\/////\///\//////\\\\\\/\\//\\///\/\\//\\\\/\\/\///\//\\/\\/\\\\\\\/\\\\/\\\\//\/\/\///\\\////\//\\\/\/////\\\/\\\//\////\\/\\\\///\\/\\\///\\\/\/\/\//\/\\\\/\\\/\/\//////\\\//\/\//\\//\\\//\//\/\/\/\\//\/\//\/\\\\\\/\\\\\\\///\\\\\\/\/\\\\//\\\\///\/\\/\\/\/\///\\\\\\\/\///\/\\\\//\\/\\\/\//\/\//\\\///\\\\///\\/\//\\/\\\\/\\\\\\//\//\\\\