5 Python Easter Eggs That Make Learning Programming More Fun
Programming
Did you know Python has several hidden easter eggs? Turns out, your favorite programming language isn’t just helpful for building applications, but also some quality humor. Let’s find some of the best ones.
5
Hello World
If you’ve ever written code in any language, it’s highly likely your first program was to print “Hello World” on the console. You can do that in Python with a line of code.
print("Hello World")
However, there’s a more sophisticated way to do this. You can import a module called __hello__ to print it.
import __hello__
However, since Python 3.11, you need to call its main method to actually print the text.
import __hello__
__hello__.main()
Similar to the __hello__ module, there’s also a __phello__ module that does the same thing.
import __phello__
__phello__.main()
The __phello__ module even has a spam attribute you can call to print it twice. This works in versions that are older than 3.11.
import __phello__.spam
In reality, these modules were added to Python to test if frozen modules worked as intended, as mentioned in the Cpython source code.
In order to test the support for frozen modules, by default we define some simple frozen modules: __hello__, __phello__ (a package), and __phello__.spam. Loading any will print some famous words...
So the next time you want to print ‘Hello, World!’, try this trick to have a chuckle or impress others.
4
The Zen of Python
Every programming language has some rules and regulations, philosophies, and best practices. Python is no exception. Tim Peters, who was a major contributor to the Python programming language, wrote a set of principles for writing code in Python. This is commonly known as the “Zen of Python.” This literature piece was even incorporated into this language itself. To read it, all you have to do is run:
import this
You’ll see the Zen of Python being printed on the screen.
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!
If you look at the actual code of the file, you’ll find something interesting. The printed text is encrypted originally.
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!"""
There’s another piece of code that is transforming the given text.
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]))
So, what’s happening is that the original text has been encrypted using a substitution algorithm known as ROT13. It’s a reversible algorithm. When you import the this
module, that encrypted text is decrypted back to its original form and printed on the screen.
3
Braces or No Braces
If you’ve even remotely used Python, you know that Python rarely uses curly braces, one of the most common syntax in many popular languages. Curly braces are usually used to define the scope of a code block, such as conditionals, loops, etc. Instead of braces, Python uses indentation. But will there ever be braces in Python? Not likely. Because the developers have already answered in the __future__ module.
from __future__ import braces
>>> SyntaxError: not a chance
This is a special syntax error that you won’t find in any other case, implying that braces are never coming to Python. The __future__ module in Python is used for implementing features in the current Python version that will be incorporated in a future version. This is done so that you can adapt to the new feature. It was a clever choice to fit this easter egg into this module.
2
The FLUFL
The __future__ module holds another interesting easter egg. If you’ve used logical operators in programming before, you know that in most languages, the symbol for inequality is != (exclamation mark followed by equal sign.) However, one of the core developers of Python, Barry Warsaw, also known as Uncle Barry, preferred to use diamond operators (<>) for inequality. Here’s the code bit.
from __future__ import barry_as_FLUFL
0 != 1
>>> SyntaxError: with Barry as BDFL, use '<>' instead of '!='
0 <> 1
>>> True
1 <> 1
>>> False
FLUFL stands for Friendly Language Uncle For Life, which is apparently Uncle Barry’s title.
1
antigravity
Another fun module to play with. I won’t spoil it for you but just run this code and experience it yourself.
import antigravity
This module also has a geohash() function. This function is used for geohashing using the Munroe algorithm.
Now, this may look totally out of place. However, this function is closely related to the previous easter egg you saw in the antigravity module. Pretty neat stuff.
It’s always interesting to find behind-the-scenes stuff about programming languages, especially if it puts a smile on your face. If this interested you, then you may consider learning Python and do more fun stuff with it.