# 一行Python代码

Life is short, just use Python.

## 有趣

`>>> 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)])`

LoveAndy

Mandelbrot

`>>> 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)])`

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

`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) ]]`

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

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

```"".join((lambda x:(x.sort(),x)[1])(list(‘string’)))

qsort = lambda arr: len(arr) > 1 and  qsort(filter(lambda x: x<=arr[0], arr[1:] )) + arr[0:1] + qsort(filter(lambda x: x>arr[0], arr[1:] )) or arr```

## 内涵

```def guess_my_number(n):
while True:
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!"
else:
user_input = int(user_input)            if user_input > n:                print "Too big ! Try again!"
elif user_input < n:                print "Too small ! Try again!"
else:                print "You win!"
return Trueguess_my_number(42)```

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

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

## 延展

`import antigravity`

python 打开浏览器

`\$ python -m pyftpdlib`

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

`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)]"`

maze wall

```\$ python -c "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.
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!```

136 篇文章37 人订阅

0 条评论

## 相关文章

### javascript ES6 初次相见

JS的ES6网上也热炒了好久了， 我一直也没怎么太细看， 今天想起来就写个东西， 也为分享，也为学习。 我喜欢接地气一点，所以网上的那些新名词我就不写了， 就写...

1877

### HLS Lesson19-函数层面优化

1.Coding Style on function 主要是数据类型，应该定义为HLS专用的数据类型ap_int<W> 2.inline:去除了函数的层次化，...

2309

3609

752

33711

3982

731

3808

### Go 语言的演化历程

9、hello.c，标准C89 #include <stdio.h> main(void) //译注：与上面hello.c相比，多了个void { pr...

3318

4047