This is a list of hidden Eggs and jokes in Python. If you know of any other funny Easter eggs and jokes, feel free to create a PR addition on GitHub.

The address of the project: https://github.com/greyli/shedan

Python is more fun than you think.




A little girl goes into a pet shop and asks for a wabbit. The shop keeper looks down at her, smiles and says:

“Would you like a lovely fluffy little white rabbit, or a cutesy wootesly little brown rabbit?”

“Actually”, says the little girl, “I don’t think my python would notice.”

— Nick Leaton, Wed, 04 Dec 1996, Python Humor

1. Hello World

>>> import __hello__
Hello World!Copy the code

Note: Print “Hello, World!” in Python. The simplest way.

2. The zen of Python

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

Note: Python Design Philosophy & A Guide to Python Code Style.

3. Missing line of Zen Python

Zen of Python, introduced in PEP 20, was supposed to be 20 maxims, but only 19 were written.

Note: Maybe just to show that you should always leave a blank line at the end of a file.

4. A simple life lesson

>>> import this
...
>>> love = this
>>> this is love
True
>>> love is True
False
>>> love is False
False
>>> love is not True or False
True
>>> love is not True or False; love is love  # FML
TrueCopy the code

Note: Not an Easter egg, just an interpreter joke.

5. Anti-gravity comics

>>> import antigravityCopy the code

Note: This will open this
XKCD cartoonThe cartoon shows how easy Python is to learn.

It’s not a choice. It defines who we are

>>> from __future__ import braces
  File "<stdin>", line 1
SyntaxError: not a chanceCopy the code

Note: This is a quick way to end any discussion of introducing braces in Python — no way!

Origin of 7.

The name “Python” has nothing to do with the type of snake.

Note: The actual source is Guido Van Rossum’s favorite TV show,
Monty Python’s Flying Circus”.

8. Contrast

Here is the contents of the this.py module printed out from Zen Python:

s = """Gur Mra bs Clguba, ol Gvz Crgref Ornhgvshy vf orggre guna htyl. Rkcyvpvg vf orggre guna vzcyvpvg. Fvzcyr vf orggre guna pbzcyrk. Pbzcyrk vf  orggre guna pbzcyvpngrq. Syng vf orggre guna arfgrq. Fcnefr vf orggre guna qrafr. Ernqnovyvgl pbhagf. Fcrpvny pnfrf nera'g fcrpvny rabhtu gb oernx gur ehyrf. Nygubhtu cenpgvpnyvgl orngf chevgl. Reebef fubhyq arire cnff fvyragyl. Hayrff rkcyvpvgyl fvyraprq. Va gur snpr bs nzovthvgl, ershfr gur grzcgngvba gb thrff. Gurer fubhyq or bar-- naq cersrenoyl bayl bar --boivbhf jnl gb qb vg. Nygubhtu gung jnl znl abg or boivbhf ng svefg hayrff lbh'er Qhgpu. Abj vf orggre guna arire. Nygubhtu arire vf bsgra orggre guna *evtug* abj. Vs gur vzcyrzragngvba vf uneq gb rkcynva, vg'f n onq vqrn. Vs gur vzcyrzragngvba vf rnfl gb rkcynva, vg znl or n tbbq vqrn. Anzrfcnprf ner bar ubaxvat terng vqrn -- yrg'f qb zber bs gubfr!" "" d = {} for c in (65, 97): for i in range(26): d[chr(i+c)] = chr((i+13) % 26 + c) print("".join([d.get(c, c) for c in s]))Copy the code

The code that generates Zen Python itself goes against the style advice it professes. It is ugly rather than beautiful, implicit rather than intuitive.

Note: We use one called
ROT13The permutation encryption method.

9. Are there any C/C++ users?

To quote again from Zen Python:

There should be one-- and preferably only one --obvious way to do it.Copy the code

Note: In many languages there are often two ways of doing the same thing, i.e
--no
no--. This line contains a hidden example.

10. Named identifiers can be pretty cool

>>> From math import PI >>> PI >>> area = π * r**2 >>> Resume = 'knows Python' >>> 'Python' in resume True >>> my English name = 'Grey Li' >>> import webbrowser >>> my website = 'http://greyli.com' >>> webbrowser.open(my website)Copy the code

Note: Python 3 supports using Unicode characters as variable names. Still, using non-English characters for variable names may not be a good idea, but it does make working with scientific formulas more interesting.

Pick a place to meet

>>> from antigravity import geohash >>> # Your location, A date and that date's (or most recent) DJIA opening. >>> # Your location, a date and the date (or most recent) corresponding to the Dow Jones Industrial Average. >>> GeoHash (37.421542, -122.085589, B '2005-05-26-10458.68') 37.857713-122.544543Copy the code

This can be used to generate a GPS location in an area 1 longitude long and 1 latitude wide based on your location.

Note: The source code for this function is available in
hereAn explanation of how it works can be found in this
XKCD cartoonSee, maybe that’s why this function is there, right
antigravityIn the module.

12. The FLUFL – Friendly Language Uncle For Life from PEP 401 — BDFL Retirement

>>> from __future__ import barry_as_FLUFL >>> 1 <> 2 True >>> 1 ! = 2 File "<stdin>", line 1 1 ! = 2 ^ SyntaxError: invalid syntaxCopy the code

After realizing Python 3.0’s inequality operators (! FLUFL reinstated the diamond operator (<>) as the only unequal operator after the error was caused by a sore finger.

Note:
PEP 401It’s an April Fool’s joke (judging by the number). This PEP says Guido Van Rossum is retiring. He’ll get a new title, BDEVIL, Benevolent Emeritus Vacationing Indefinitely from the Language, His successor will be Barry Warsaw (Uncle Barry), whose official title is “FLUFL” (Friendly Language Uncle For Life).

13. InPynite?

>>> infinity = float('infinity')
>>> hash(infinity)
314159
>>> hash(float('-inf'))
-314159Copy the code

A hash value is a fixed-size integer that identifies a particular value. If you look closely, you’ll see that the infinity of the hash value is 10^5 x π. Interestingly, in Python3, hash(float(‘ -INF ‘)) will generate -10^5 x π, whereas in Python 2 it will generate -271828 (that is, 10^5 x e).

Note:
source.

14. types.CodeType – Not for the faint of heart

If you start digging into Python, you’ll see a warning in the help output about types.codeType: Not for the faint of heart.

>>> import types
>>> help(types.CodeType)
...
Help on class code in module builtins:                                                    
                                                                                          
class code(object)                                                                        
 |  code(argcount, kwonlyargcount, nlocals, stacksize, flags, codestring,                 
 |        constants, names, varnames, filename, name, firstlineno,                        
 |        lnotab[, freevars[, cellvars]])                                                 
 |                                                                                        
 |  Create a code object.  Not for the faint of heart.                                    
 |                                                                                        
 |  Methods defined here:                                                                 
 |                                                                                                                                       
 ...Copy the code

The original version translated from https://github.com/OrkoHunter/python-easter-eggs, the original project without the license agreement, has applied for translation and publishing permission via email.