7 Easter Eggs in Python. Countless Ways to Entertain Yourself at... | by Eden Au | Towards Data Science

Most of us are working from home amid the coronavirus outbreak. Many of you might already be so bored staying at home all day. And I feel you.
Python might only be a tool for you to build projects, simulation, and automation, and it could be really fun.
Thanks to the amazing Python community, we can find many hidden features and easter eggs in this open-source language. Here are 7 of them.
The first thing to do when learning a programming language is to print out ‘Hello World’ on the screen. How can you do Hello World in Python? print('Hello World!')?
Turns out Python developers hid a module that could do Hello World simply by importing this module! Try this:
>>> import __hello__Hello World!
That line of code consists of 16 characters only, including the spacebar! This is arguably one of the ‘easiest’ Hello World programs.
Note that you can’t reimport a module in a Python program, so you could print the message once in a single run. But I guess that might mean something deep...
This was proposed in PEP 20. PEP stands for Python enhancement proposals.
Is Python your favourite language? What do you like about Python? Is it the design? The guiding principles for Python’s design can be described by 20 aphorisms, and you can find 19 of them by:
>>> import this
What about the missing one? I guess you can’t know everything in the world. Here are the first 3 aphorisms:
Beautiful is better than ugly.Explicit is better than implicit.Simple is better than complex.
You have got to appreciate the effort the developers have invested in making such an elegant and human-readable programming language.
This is also by far the only ‘official’ easter egg that is stated as an ‘easter egg’ in Python Developer’s Guide.
Remember the first 3 lines in the Zen of Python?
When you dig deeper and find the module file this.py, it is the most beautiful, explicit, and simple code I have ever seen.
Check out the file here. Well done.
You can actually experience antigravity using one line of code in Python!
import antigravity
Really! Try it out! Bear in mind that you might be stuck in outer space and spend hours browsing xkcd webcomic.
Unlike many other programming languages, Python doesn’t really utilise curly braces {} when constructing statements, functions, and loops. But they might change in the future.
The __future__ module includes incompatible changes that will be mandatorily enforced in the foreseeable future. For instance, importing print_function from __future__ in Python 2.6 or 2.7 allows you to make ‘print’ a function that takes arguments print(), just like in Python 3.
Let’s see how braces would work from __future__:
>>> from __future__ import bracesSyntaxError: not a chance
Well played.
The hash of infinity and NaN.
>>> hash(float('inf'))314159>>> hash(float('nan'))0
And I somehow found this easter egg on Reddit.
I was expecting the answer to life, the universe and everything to be honest.
The debate of operator choice has gone on four two long. Let’s settle this.
This was proposed in PEP 401. You might sense what is coming up.
A well-known Python developer Barry Warsaw (aka Uncle Barry) was ‘chosen’ to become the Friendly Language Uncle For Life, which is FLUFL in short. Great acronym.
He enacted a few ‘modifications’, which enabled the replacement inequality operator != by diamond operator<>.
If you agree with Uncle Barry’s vision, you could import this interesting library, and the <> syntax would become valid, whereas the != one would result in a syntax error.
>>> from __future__ import barry_as_FLUFL>>> 0 != 1SyntaxError: with Barry as BDFL, use '<>' instead of '!='>>> 0 <> 1True>>> 1 <> 1False
April Fool! Poor Barry... I assure you there is no typo in the example above.
The list goes on and on, but I will stop here and let you explore them yourself.
Thanks for reading! If you want to receive updates on my new articles, you can subscribe to my newsletter using the slider below:
And if you are still reading this, you are probably interested in Python. The following articles might be useful: