Ever since I got into Python in 2008, I have had a love affair with It, and have gradually, somewhat unwillingly, distanced myself from Perl and Shell programming because of the elegance of Python? B: Not really, mainly because it can be developed efficiently.

What does that line of code do?

## interesting

My kid’s English name is Andy, and maybe if I had shown him this line of code when I was teaching him how to write a program, he might have been more interested in it.

``> > > print '\ n'. Join (['. Join ([(' AndyLove [8] (x-y) % if ((0.05) x * * * 2 + 0.1) (y * * * 2-1) 3 - (0.05) x * * * * * 2 * * * 3 (y * 0.1) < = 0 else ") for x In range(-30,30)] for y in range(15,-15,-1)]Copy the code``

When executed in Python, the output will be a heart-shaped character.

Character graphics are interesting, there’s a famous image called Mandelbrot. Each position in the Mandelbrot diagram corresponds to a complex number in the formula N=x+y * I, which should be impressive to anyone who learned complex numbers in high school. Each position is represented by the parameter N, which is the square root of x * x+y * y. If this value is greater than or equal to 2, then the corresponding position value of this number is 0. If the value of N is less than 2, change the value of N to N*N-

N (N= (xx-yy-x) + (2xy-y) * I)), and test the new value of N again. Wikipedia gives an image like this:

Let’s draw a Mandelbrot with one line of code:

``>>> 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 0.05 j * * x + y, 40)) 2 else 'for x in the range (80, 20 -)]) for y in range (20, 20 -)])Copy the code``

## efficient

Python is especially good for handy gadgets.

One line of code prints the multiplication table:

``Print '\ n'. Join (['. Join ([= '% s * % s % 2 s' % (y, x, x * y) for y in range (1, x + 1)]) for x in range (1, 10)])Copy the code``

Output:

One line of code calculates primes between 1 and 1000

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

One line of code outputs the first 100 Fibonacci numbers:

``Print [x [0] for x in [(a [I] [0], a.a ppend ((a [I] [1], a [I] a [I] [0] + [1]))) for a in ([[1, 1]], for I in xrange (100)]]Copy the code``

One line of code implements factorial, but also with interaction:

``````>>> reduce ( lambda x,y:x*y, range(1,input()+1))
10
3628800
Copy the code``````

One line of code to convert between degrees Celsius and Fahrenheit:

``>>> 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 Fahrenheit 2,Fahrenheit to Celsius Please input 1 or 2 1 Please Input a Celsius Temperature :28 82.4 >>>Copy the code``

String sorting and quicksort are handy.

``"". Join ((lambda x:(x.ort (),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 arrCopy the code``

## The connotation of the

Take a look at the following line of Python code and you might be confused:

This is originally designed to stimulate children’s interest in programming, so that children can practice the code, it looks like this:

``````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 True
guess_my_number(42)
Copy the code``````

In fact, any Python code can be converted to a single line of code with an understanding of functional programming, using the magic of Lambda, list derivations, and slightly more complex judgment statements.

For example, take a random number from a list

``Import random as RND print rnd.choice([2,3, 5,7, 11,13,17])Copy the code``

Lambda can be converted to:

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

The code is fun to read, mainly because it helps us understand some of Python’s gnomes, especially the magic of Lambda usage.

## stretch

Of course, there are other fun things to do, so type in the following line

``````import antigravity
Copy the code``````

It opens a browser and displays comics and related content from the site:

We can package Python files, make them into libraries, and import them, which is a line of code that steals concepts and premises. For example, to transfer files with Windows, temporarily set up FTP on Mac:

``````\$ python -m pyftpdlib
Copy the code``````

PIP install Pyftpdlib is not available on the machine.

If you allow semicolons in a line of code, you’re just sacrificing readability, and you’re basically omnipotent.

Obtain a public IP address if the network is connected

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

One line of code can easily write a small game, to simulate golf shot.

``python -c "import math as m; a,v=eval(input()); [print (' % '03 d % + x "' * m. loor (0.5 + x x m. the an (a) - x/x * * m.c OS (a) (v)) + 'o') for x in the range (102)]"Copy the code``

Input angles and forces such as (0.8,80) to get a character-drawn parabola.

Add while and so on, draw an endless

``python -c "while 1:import random; Print (the random choice (' ╱ ╲ '), end = ' ')"Copy the code``

Finally, a line of code ends with a python philosophy.

``\$ 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. 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!Copy the code``