Magic python, what kind of cool things can one line of code do?

Source: Internet
Author: User

You may have seen a similar Python line of code that can be implemented online, such as:

99 Multiplication Table:

Print (". Join" (['%s*%s=%-2s '% (y,x,x*y) for Y in range (1,x+1)]) "For X in Range (1,10)])

or output the value of the first 100 Fibonacci sequences:

Print ([x[0] for x in [(A[i][0], A.append ((A[i][1], a[i][0]+a[i][1]))))-A In ([[[[]],) ' For I in range (100)]])

Well, these can be done, then the following cool code, more worthy of a look!

Single-line maze (input under cmd):

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

Emmmm, this may be more abstract, then try to change the characters inside, such as the following code python-c "while 1:import random;print (Random.choice (' ╱╲ '), end= ')"

This maze effect is OK!

Guess numbers

A lot of tutorials have this small case, then you use a few lines? Try the following.

Python-c "Import random;n=random.randint (1,99); [(Lambda a:print (' Y ' if a==n Else ' H ' if a>n Else ' L '))) (int (input ()))) for I in range (6)] "

Yes, you have not read wrong, a line of code can achieve this function!

100 Foot Golf

Referring to the Angry Birds, the goal is to get the golf to just 100 feet, not much more!

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

You need to enter 2 parameters, which is the angle and speed of golf!

At last

Most of the techniques used by these codes should look familiar:

List parsing, lambda expressions and random integers, with a combination of several functions, can write cool code!

Magic python, what kind of cool things can one line of code do?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.