First you need to understand The Zen of Python. One line of code prints “The Zen of Python” :

Copy the code
  1. python -c “import this”  
  2. “” “
  3. The Zen of Python, by Tim Peters  
  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!  
  23. “” “

As you can see from “The Zen of Python”, Python advocates The principles of Beautiful, Explicit, Simple, etc. Of course, The next line of Python can do some fun things that might not be Explicit.

If you have any other small examples of this, feel free to comment and I will add them to the post, which may be updated in the long run.

(1) One line of code starts a Web service

Copy the code
  1. python -m SimpleHTTPServer 8080 # python2  
  2. python3 -m http.server 8080 # python3 

(2) a line of code to achieve variable value interchange

Copy the code
  1. a, b = 1, 2; a, b = b, a 

(3) One line of code to solve FizzBuzz:

FizzBuzz Problem: Print the numbers 1 to 100, multiples of 3 print “Fizz”, multiples of 5 print “Buzz”, multiples of both 3 and 5 print “FizzBuzz”

Copy the code
  1. for x in range(1, 101): print(“fizz”[x % 3 * 4:]+”buzz”[x % 5 * 4:] or x) 

(4) One line of code outputs a heart shaped by the specific character “Love”

Copy the code
  1. Print (‘ \ n ‘. Join ([(‘ Love ‘ ‘ ‘. Join ([[(x-y) % len (‘ Love ‘)] the if ((0.05) x * * * 2 + 0.1) (y * * * 2-1) 3 – (0.05) x * * * * * 2 * * * 3 (y * 0.1) < = 0 the else ‘ ‘) for x in range(-30, 30)]) for y in range(30, -30, -1)]))

(5) One line of code output Mandelbrot image

Mandelbrot image: Each position in the image corresponds to a complex number in the formula N=x+y* I

Copy the code
  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 == 0 else 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)]))

(6) A line of code to print the multiplication table

Copy the code
  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)])) 

(7) One line of code to calculate prime numbers between 1 and 100 (both versions)

Copy the code
  1. print(‘ ‘.join([str(item) for item in filter(lambda x: not [x % i for i in range(2, x) if x % i == 0], range(2, 101))]))  
  2. print(‘ ‘.join([str(item) for item in filter(lambda x: all(map(lambda p: x % p != 0, range(2, x))), range(2, 101))])) 

(8) One line of code to output the Fibonacci sequence

Copy the code
  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 range(30)]]) 

(9) one line of code to achieve fast sorting algorithm

Copy the code
  1. qsort = lambda arr: len(arr) > 1 and qsort(list(filter(lambda x: x <= arr[0], arr[1:]))) + arr[0:1] + qsort(list(filter(lambda x: x > arr[0], arr[1:]))) or arr 

(10) One line of code to solve the eight queens problem

Copy the code
  1. [__import__(‘sys’).stdout.write(‘\n’.join(‘.’ * i + ‘Q’ + ‘.’ * (8-i-1) for i in vec) + “\n========\n”) for vec in __import__(‘itertools’).permutations(range(8)) if 8 == len(set(vec[i]+i for i in range(8))) == len(set(vec[i]-i for i in range(8)))]

(11) One line of code to achieve the flatten function of the array: transform the multidimensional array into one dimension

Copy the code
  1. flatten = lambda x: [y for l in x for y in flatten(l)] if isinstance(x, list) else [x] 

(12) One line of code implements list, somewhat similar to the reverse of the previous function

Copy the code
  1. array = lambda x: [x[i:i+3] for i in range(0, len(x), 3)] 

(13) a line of code to solve the sum of the digits of 2 to the 1000th power

Copy the code
  1. print(sum(map(int, str(2**1000)))) 



Author: Anonymous

Source: 51 cto