偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

一行Python代碼

開發(fā) 開發(fā)工具
自從08年接觸Python,就有愛不釋手的感覺,逐漸地,有些不忍地疏遠了Perl 和Shell編程,因為python 的優(yōu)雅么? 不全是,主要是可以高效開發(fā)吧。高效,那一行代碼可以干什么呢?

Life is short, just use Python.

自從08年接觸Python,就有愛不釋手的感覺,逐漸地,有些不忍地疏遠了Perl 和Shell編程,因為python 的優(yōu)雅么? 不全是,主要是可以高效開發(fā)吧。

高效,那一行代碼可以干什么呢?

有趣

我孩子的英文名叫andy,也許當初教他寫程序的時候,如果先秀一下這行代碼,可能就更能激起他對代碼的興趣了。

  1. print'\n'.join([''.join([('AndyLove'[(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)]) 

在python 里執(zhí)行它,會輸出一個字符拼成的愛心。

LoveAndy

LoveAndy

字符圖形還是很有趣的,有一個著名的圖像叫mandelbrot。Mandelbrot圖像中的每個位置都對應于公式N=x+y*i 中的一個復數(shù),高中學過復數(shù)的都還應該有印象。每個位置用參數(shù)N來表示,它是x*x+y*y的平方根。如果這個值大于或等于2,則這個數(shù)字對應的位置值是0。如果參數(shù)N的值小于2,就把N的值改為NN-N(N=(xx-yy-x)+(2xy-y)i)),并再次測試這個新N值。wiki百科給出的圖像是這樣的:

[[177670]]

Mandelbrot

讓我們用一行代碼畫一個Mandelbrot:

  1. 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)]) 

 

[[177671]]

高效

對于隨手小工具而言,更是Python的拿手好戲。

一行代碼打印九九乘法表:

  1. 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-1000之間的素數(shù)

  1. print(*(i for i in range(2, 1000) if all(tuple(i%j for j in range(2, int(i**.5)))))) 

一行代碼可以輸出前100項斐波那契數(shù)列的值:

  1. 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 xrange(100) ]] 

一行代碼實現(xiàn)階乘,而且還帶交互:

  1. reduce ( lambda x,y:x*y,  range(1,input()+1))103628800 

一行代碼實現(xiàn)攝氏度與華氏度之間的轉換器:

  1. print((lambda i:i not in [1,2] and "Invalid input!" or i==1 and (lambda f:f<-459.67 and "Invalid input!" or f)(float(input("Please input a Celsius temperature:"))*1.8+32) or i==2 and (lambda c:c<-273.15 and "Invalid input!" or c)((float(input("Please input a Fahrenheit temperature:"))-32)/1.8))(int(input("1,Celsius to Fahrenheit\n2,Fahrenheit to Celsius\nPlease input 1 or 2\n"))))1,Celsius to Fahrenheit2,Fahrenheit to Celsius 
  2. Please input 1 or 21Please input a Celsius temperature:2882.4 

至于字符串排序和快速排序更是手到擒來。

  1. "".join((lambda x:(x.sort(),x)[1])(list(‘string’))) 
  2.  
  3. qsort = lambda arr: len(arr) > 1 and  qsort(filter(lambda x: x<=arr[0], arr[1:] )) +  

內(nèi)涵

看一看下面一行python代碼,可能就要暈了:

《跟孩子一起學編程》

這是《跟孩子一起學編程》一書中,為了激發(fā)孩子編程興趣,讓孩子練習的代碼——猜數(shù)游戲,它的真實面貌是大致這樣的:

  1. def guess_my_number(n): 
  2.     while True: 
  3.         user_input = raw_input("Enter a positive integer to guess: ")        if len(user_input)==0 or not user_input.isdigit():            print "Not a positive integer!" 
  4.         else: 
  5.             user_input = int(user_input)            if user_input > n:                print "Too big ! Try again!" 
  6.             elif user_input < n:                print "Too small ! Try again!" 
  7.             else:                print "You win!" 
  8.                 return Trueguess_my_number(42) 

神奇的Lambda,配合列表推導以及復雜一點的判斷語句,任何的python 代碼都可以轉換成一行代碼的。

例如,取一個列表里的隨機數(shù)

  1. import random as rnd 
  2. print rnd.choice([2,3, 5,7, 11,13,17]) 

轉換成Lambda 可以是:

  1. print (lambda rnd: rnd.choice([1, 2, 3, 10]))(__import__('random')) 

這些代碼出了覺得好玩,主要是可以幫助我們了解某些Python的特性技藝,尤其是神奇的Lambda 用法。

延展

當然,還有其他好玩的地方,輸入下面這一行

import antigravity

它打開了瀏覽器,展示網(wǎng)站上的漫畫和相關內(nèi)容:

python 打開瀏覽器

python 打開瀏覽器

我們可以把python的文件打包,做成庫的形式,然后import進來,是一種偷換概念和改變前提的一行代碼。例如,為了與windows 傳輸文件,在Mac上臨時搭個ftp服務器:

  1. $ python -m pyftpdlib 

這當然要依賴pyftpdlib 這個庫了,機器上沒有,pip install pyftpdlib 就可以了。

如果一行代碼中允許分號存在,那就只是犧牲可讀性而已了,基本上是無所不能。

在線的時候,獲取公網(wǎng)IP地址的一行代碼:

  1. python -c "import socket; sock=socket.create_connection(('ns1.dnspod.net',6666)); print sock.recv(16); sock.close()" 

一行代碼就可以輕易寫個小游戲了,來模擬一下golf擊球。

  1. python -c "import math as m;a,v=eval(input());[print('%03d'%x+' '*m.floor(0.5+x*m.tan(a)-x*x/(v*m.cos(a)))+'o') for x in range(102)]" 

輸入角度和力量大小如(0.8,80),就能得到一條字符描畫的拋物線了。

增加上while 等語句,畫一個沒完沒了的墻紙python -c "while 1:import random;print(random.choice('╱╲'), end='')"。

maze wall

maze wall

***, Zen of Python 以一行代碼來結束吧。

  1. $ python -c "import this"   
  2. The Zen of Python, by Tim Peters 
  3.  
  4. Beautiful is better than ugly. 
  5. Explicit is better than implicit. 
  6. Simple is better than complex. 
  7. Complex is better than complicated. 
  8. Flat is better than nested. 
  9. Sparse is better than dense. 
  10. Readability counts. 
  11. Special cases aren't special enough to break the rules. 
  12. Although practicality beats purity. 
  13. Errors should never pass silently. 
  14. Unless explicitly silenced. 
  15. In the face of ambiguity, refuse the temptation to guess. 
  16. There should be one-- and preferably only one --obvious way to do it. 
  17. Although that way may not be obvious at first unless you're Dutch. 
  18. Now is better than never. 
  19. Although never is often better than *right* now. 
  20. If the implementation is hard to explain, it's a bad idea. 
  21. If the implementation is easy to explain, it may be a good idea. 
  22. Namespaces are one honking great idea -- let's do more of those! 

【本文來自51CTO專欄作者老曹的原創(chuàng)文章,作者微信公眾號:喔家ArchiSelf,id:wrieless-com】

責任編輯:趙寧寧 來源: 喔家ArchiSelf
相關推薦

2022-04-09 09:11:33

Python

2017-04-13 19:20:18

Python代碼并行任務

2021-11-02 16:25:41

Python代碼技巧

2020-08-19 10:30:25

代碼Python多線程

2020-08-12 14:54:00

Python代碼開發(fā)

2020-09-28 12:34:38

Python代碼開發(fā)

2017-04-05 11:10:23

Javascript代碼前端

2014-02-12 13:43:50

代碼并行任務

2024-05-31 13:14:05

2020-01-10 22:56:56

Python圖像處理Linux

2022-09-28 10:12:50

Python代碼可視化

2020-09-09 16:00:22

Linux進程

2021-08-31 09:49:37

CPU執(zhí)行語言

2023-01-12 08:07:03

Python代碼版權

2021-01-25 09:36:00

Python代碼文件

2022-04-14 07:57:52

Python代碼熱力圖

2022-02-24 10:40:14

Python代碼

2020-02-14 12:26:55

Python愛心情人節(jié)

2021-04-19 10:38:06

代碼開發(fā)工具

2023-11-10 09:41:44

Python代碼
點贊
收藏

51CTO技術棧公眾號