Thursday, May 21, 2020

What can a single line of Python code do?

Python has a lot of elegant and interesting code writing, and it is also very short, so that when I first started to contact this programming language, I couldn't put it down. In the list of programming languages a few days ago, python also surpassed java and became the first, quite happy for python.

How interesting is Python? One line of code tells you!

1) Draw love

print(' '.join([''.join([('sergiojune'[(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)]))

This can be described as a confession artifact. You can show it off every holiday, brother dei, understand me?

2) Draw a Mandelbrot
print(' '.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)]))

3) Print 99 multiplication table
print(' '.join([' '.join(['%s*%s=%-2s' % (y, x, x*y) for y in range(1, x+1)]) for x in range(1, 10)]))

4) One line of code for quick sorting
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]

5) Two variables can be exchanged
a, b = b, a

This should be simple. Also pythonic style.

6) Print the Zen of python
import this

Many people do not know this estimate. You often say "life is short, I use python". The beauty of python is often reflected in it. Good Python programmers should also follow it.

7) Watch comics
import antigravity

No comments:

Post a Comment