What wonderful line of Python code feature can be achieved?

If only allows you to write a line of code, you can realize what kind of function? Today we take a look at this 16-line funeral (gan) Heart (de) disease (piao) mad (liang) code.

Mandelbrot image output

mandelbrot image Chinese man named Mandelbrot images. Mandelbrot each position in the image corresponds to the formula N = in a complex number x + y * i. In fact, part number is x, the imaginary part of y, i is the square root of -1. Image x and y coordinates corresponding to the respective positions x and y of the imaginary part of the number. Graphics Baidu Encyclopedia gives is this.

can not read it? Never mind, we look at the direct effect. (Sliding around to see all the 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 == 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)]))

Multiplication table

We still remember the multiplication table lies at the heart of it? , We have to consolidate under, we print out a single line of code. (Sliding around to see all the code)

print('\n'.join([' '.join(['%s*%s=%-2s' % (y, x, x*y) for y in range(1, x+1)]) for x in range(1, 10)]))

Line of printing pattern Heart

Do not tell the truth? One line of code to tell you!

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

Beautiful spiral

Do you think the above is not enough romantic heart-shaped pattern? If so, try the following beautiful spiral? Enter the following code in the Python compiler. (Sliding around to see all the code) 

exec("""\nfrom turtle import *\nfor i in range(500): \n    forward(i)\n    left(91)\n""")

FIG portion of the dynamic look during execution.

And then shot a look at big picture is not very pretty, very cool!

The painting gallery with a turtle library.

Calculate the number of prime between 1-100

Also known prime number prime number. Is a natural number greater than 1, except 1 and itself, but not a natural number divisible other known prime number;

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

The results are:

Line of code output Fibonacci number

Fibonacci number (Fibonacci sequence), also known as golden columns, because mathematician Leonardo Fibonacci (Leonardoda Fibonacci) as an example to the rabbit breeding and the introduction, it is also known as "rabbit number of columns", this the number of columns from the beginning of paragraph 3, each of which is equal to the sum of the first two.

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

Look at the results:

A line of code to achieve fast algorithm row

Sorting is a classic fast and efficient sorting algorithms.

quickSort = lambda array: array if len(array) <= 1 else quickSort([item for item in array[1:] if item <= array[0]]) + [array[0]] + quickSort([item for item in array[1:] if item > array[0]])
array = [9, 11, 88, 32, 8]
print(quickSort(array))

Print Zen of Python

Zen of Python you know what? Know "Life is short, I used Python 'classic phrase of the source it? With the following line of code to print Python Zen. (Sliding around to see all the code)

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

Give you a reference translation:

Beautiful is better than ugly.
Implicit rather than explicit.
Simple is better than complex.
Better than complex and difficult.
Flat rather than nesting.
Sparse is better than closer.
Readability should be valued.
Although practicality will defeat purity, exceptions can not be above the rules.
Do not ignore any error, unless you are sure you want to do this
face is not clearly defined, refuse the temptation to guess.
To find a best only a way to solve the problem.
Although at first this method is not obvious, because you are not the parent of Python.
Do not do too well, but did not think to do might as well do
if the implementation is difficult to explain, that it is a bad idea.
If the implementation is easy to explain, that it might be a good idea.
Namespace is a wonderful idea, always close by.

read comics

Importing a package will be able to read comics, the system will automatically open the comics pages after executing code.

import antigravity

Give you a reference translation:

Above:
"how do you fly!?"
"Python"!

Below left:

"Last night, I learned Python, everything is so simple."

"HELLO WORLD run only need to print" Hello World! ""

Under the:
"I still do not understand ...... dynamically typed, or space?"

"Come join us, and once again become interesting program up, Python is a whole new world."

"But how do you do together?"

At right:

"I just entered the import antigravity"

"that's it?"

"I have all the stuff in the medicine cabinet were sampled more" (alluding to his comparison of various programming languages ​​too, but still feel the simplest Python)

"But I think this is Python."

Single line maze

cmd command input single-wire maze under the following code.

python -c "while 1:import random;print(random.choice('|| __'), end='')"

One line of code to open web services

Use this command to build a WEB service network you can be quickly accessed.

python -m http.server 8080

Enter http: // localhost: 8080 / WEB site can access the service.

Mini slot machine

Out random sampling of unicode characters, perfectly simulate the effect of slot machine, press the Enter key to stop. Consider the following dynamic FIG.

python -c "import random;p=lambda:random.choice('7♪♫♣♠♦♥◄☼☽');[print('|'.join([p(),p(),p()]),end='\r') for i in range(8**5)]”

One line of code to solve the eight queens problem

What is the eight queens problem? We look at Baidu Encyclopedia explained: Eight queens problem is an old and well-known problems, is a typical case of backtracking algorithms. The problem is an international chess player Max Bethel presented in 1848: placed on the international chess 8 × 8 grid of eight Queens, it can not attack each other, that is, any two queens can not be in the same line, on the same row or the same slash and asked how many pendulum method. Gauss think there are 96 kinds of programs. 1854 in Berlin on different chess magazine published 40 kinds of different solutions, then someone solve 92 kinds of results using graph theory. After the invention of the computer, there are a variety of computer languages ​​can solve this problem.

List all the results with the following code, a total of 92 kinds.

[__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)))]

The multidimensional array into a one-dimensional

The line of code multidimensional array into one-dimensional array.

print([i for item in ab for i in item])

It gives a multidimensional value ab, into a one-dimensional array by this method.

ab = [[1, 2, 3], [5, 8], [7, 8, 9]]
print([i for item in ab for i in item])

# 结果
[1, 2, 3, 5, 8, 7, 8, 9]

Solving 1000-th power of 2 and the sum of all of you

2 of the 1000 value of great power, the results add up how much each digit is equal to it?

Print (SUM (Map (int, STR (2 ** 1000)))) 

# Results 
1366

blessing

Finally, to give you: love you three thousand times!

print("I Love You!\n" * 3000)

What do you think of fun, interesting line of code to achieve functionality, welcome message!

Published 151 original articles · won praise 4 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_46089319/article/details/105385642