You Are Here: Home » Uncategorized

Easter eggs in Python

By on May 2nd, 2010 
Advertisement

Easter eggs are always a sweet surprise. And Python also has some. Heres a list of Python's easter eggs. Try them out and be surprised!

# Fire up the Python console and type in ' import __hello__ ' or ' import __phello__ ' and this is what you will get. It happens only on the first time you import the module, if you want to display ' Hello world... ' again, use ' reload(__phello__) ' or ' reload(__hello__) '. Read more on this here.

>>>import __hello__
Hello world...
>>>reload(__hello__)
Hello world...
<module '__hello__' from '<frozen>'>

# Enter ' from __future__ import braces ' and you will get a  ' not a chance' syntax error. Read more on this here.

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

# Type in ' import this ' and you will see a hidden message - The Zen of Python, a summary of Python philosophy , written by pythoneer Tim Peters. Theres also a ROT13 version of the same message available and it can viewed by typing ' print this.s ' and the look up table for ROT13 can be viewed by typing ' print this.d '. Read more about this here.

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

# This is one works  in Python 3.  Fire up Python 3 shell and type in ' import antigravity ' (antigravity is a module added to Python 3.x ). Importing this module opens a web browser and displays a xkcd comic page featuring the antigravity module. Read more about this here.

>>> import antigravity

# This is not exactly an easter egg. Its a joke generated using Python interpreter (courtesy : Python mailing list):

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

Try them out. And if you have managed to discover some more easter eggs do share them.

Advertisement



Related Posts by Tags: ,





Easter eggs in Python was originally published on Digitizor.com on May 2, 2010 - 9:02 am (Indian Standard Time)