python 特效之一行代码能做哪些丧心病狂的事情

一行代码 能做什么事情?


不要觉得一行代码太少,做不了什么事情,世间从不缺少智慧,缺少的只是动手能力。


一、爱心表白

  • 有心动的异性?那就主动出击
  • 代码
print('\n'.join([''.join([('autofelix520'[(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)]))
  • 结果
                felix5aut           felix5aut               
            utofelix5autofeli   utofelix5autofeli           
          autofelix5autofelix5autofelix5autofelix5a         
         autofelix5autofelix5autofelix5autofelix5aut        
        autofelix5autofelix5autofelix5autofelix5autof       
        utofelix5autofelix5autofelix5autofelix5autofe       
        tofelix5autofelix5autofelix5autofelix5autofel       
        ofelix5autofelix5autofelix5autofelix5autofeli       
        felix5autofelix5autofelix5autofelix5autofelix       
        elix5autofelix5autofelix5autofelix5autofelix5       
         ix5autofelix5autofelix5autofelix5autofelix5        
          5autofelix5autofelix5autofelix5autofelix5         
          autofelix5autofelix5autofelix5autofelix5a         
            ofelix5autofelix5autofelix5autofelix5           
             elix5autofelix5autofelix5autofelix5            
              ix5autofelix5autofelix5autofelix5             
                autofelix5autofelix5autofelix               
                  ofelix5autofelix5autofeli                 
                    lix5autofelix5autofel                   
                       autofelix5autof                      
                          felix5aut                         
                             x5a                            
                              a                             


二、芭蕉扇灭火

  • 夏天太热?来个芭蕉扇灭灭火
  • 代码
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==0else 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*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


四、排序

  • 数据杂乱无章?那就排个序吧
  • 代码
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]

print(qs([1, 3, 2, 10, 8, 6]))
  • 结果
[1, 2, 3, 6, 8, 10]


五、看小说

  • 空闲时间太多?那就看看小说消磨一下
  • 代码
import antigravity
  • 结果

python 特效之一行代码能做哪些丧心病狂的事情

一行代码看小说


六、造迷宫

  • 孩子假期太闹腾?来个迷宫过过脑
  • 代码
print(''.join(__import__('random').choice('/\\') for i in range(50*24)))
  • 结果
/\\/\\\\/\/\//////\///\\\\\\\\/\/////\\\\\\\/\/\//
\/\\/////\/\\//\\/\\\\\\///\//\\\/\//\/\/////\\///
\\/\\\/\///\\///\\\\/\\//\\\\//\\\/\//\\/\/\\//\\/
/\\\/\\\/\/\///\\/\////////\\\/\/\\\\\/\\//\\\\///
\\//\/\/\\///\\\\\\//////\\///\//\//\//\//\\\/\\\\
/\\//\/\//\////\\/\/\\/\\//\//\/////\////\/\///////
\//\\/\\//\\/\\/\//\/\/\\\\/\/\///\////////////////
/\/\///\//\//\/\\\////\\\/\////\/\/\//\\\/\\\\\\///
/////\//\\//\\\\\\/\//\\///\///\\\/\\\\/////\////\\
/\///\/////\/\/\/\///\//////\\\\/\///\\\\///\\\/\//
\\\////\\\////\///\/\///\\/\\\//\/\/\\/\\/\//\//\\\
/\\/\///\\\\//\/////\\/\\\\/\/\\\\///\\//\//\/\///\
///\\\/\\\\\//\//\\/\/////\\/\/\\//\\\//\\//\/\//\/
\\//\/\/\\/\////\\\//\\\\////\\//\/\\\/\\\/\\//\\\\
///\/\\/\/\\//\//\\\/////////\\\/\///\///\\\/\\////
//\\///\\\///////\\\\/\\\\\//\/\////\\/\\//\\//\\/\
\\\\/\/\////////\\//\/\//////\/\/\\\\\\/\//\///\/\/
/\\\///\\\//\//////\\\\\//\\/\////\\\/\/\\\\/\/\//\
///\//\\\\\//\//\/\\/\\\/\\\\\/\/////\\\\/\\/\\///\
\/\//\/\\\/\//\\//\//\\/\/\\/\/\//\//\\\\/\\\/\//\/
//\\////\///\/\\\\///\/\\\/\\/\\///\//\\/\///\////\


七、打印 python 之禅

  • 人生苦短,我用 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!
上一篇:用Dokcer创建开发环境


下一篇:依赖注入(Dependency Injection)----DI