It takes about 6.6 minutes to read the text.

When a programming language is open source, something funny and interesting happens. Often, this means community contributors add interesting and special Easter eggs and hidden features to the language (as long as it doesn’t increase the risk of use in a production environment, of course).

Python is a good example. As an open source language, its community has contributed something humorous to it.

Side note: If you want to observe eggs, please note that they only work the first time. If you want to run them again, you need to restart the Python shell.

0. Hello World

Programmers are familiar with the concept of Hello World. In most cases, it refers to the smallest program written in the programming language that prints “Hello World” to the screen. This is probably the first thing to do when learning a new programming language.

Python has a cool hidden library that does something a little different:

>>> import __hello__
Hello World... 
Copy the code

1. April Fool

The following April Fool joke was put forward by Barry Warsaw in connection with his retirement. He is a famous Python developer, and when he announced his retirement, this egg was born:

>>> from __future__ import barry_as_FLUFL
>>> 1 <> 2
True 
Copy the code

2. Convert from C++ to Python

The ‘braces’ library is also a programmer’s joke, noting in its documentation that it provides the ability to use C++ curly braces when writing Python code. But when you try it out, you’ll see what the community thinks about it:

>>> from __future__ import braces
SyntaxError: not a chance 
Copy the code

Hahaha, thought import braces could be used to close a code block? The Python answer is: no way!

3. Classic Python Zen

import this 
Copy the code

Running this command will display “Zen Of Python “by Tim Peters. This is said to be the “eight honors and eight disgraces” of Python, something every aspiring Python programmer should keep in mind.

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

Translation:

Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than difficult. Flat is better than nesting. Dispersion is better than density. Readability should be valued. While practicality trumps purity, exceptions do not override the rules. Mistakes should never slip away unless they are explicitly silenced. In the face of unclear definitions, resist the temptation to guess. One way, preferably only one way to do one thing. This may not be obvious at first, but you’re not the father of Python. Doing is better than never, but doing right now is sometimes worse than not doing at all. If the implementation is hard to explain, it’s a bad idea. If the implementation is easy to explain, it’s probably a good idea. Namespaces are a great idea, let’s use them more!

4. “Anti-gravity” comics

import antigravity 
Copy the code

Typing this line of code will take you directly to the comic page XKCD. The cartoon is very interesting, a man is floating in the sky, one of his friends is curious to ask him how he flies.

image

Translation of the dialogue:

Above:

“You’re flying! How do you do that?”

“Python!”

Key levels:

“I learned Python last night and everything was so simple”

“Run HELLO WORLD just print” HELLO WORLD!” “

Under the:

“I still don’t understand… Dynamic type, or space?”

“Come and join us, programming is fun again, Python is a whole new world.”

“But how do you fly?”

Right:

“I just typed import Antigravity.”

“That’s it?”

“I also sampled and compared everything in the medicine cabinet.”

“But I guess that’s Python.”

Article reprinted from: Open Source China Community [http://www.oschina.net]

This paper address: https://www.oschina.net/news/85139/top-5-python-easter-eggs-and-hidden-features

Please follow my wechat official account “Crazy Sea” and go back to “1024” to get the latest Python tutorial.