Have you heard about Python Easter eggs? In this article, we will discuss the 10 best Easter eggs in Python.
1.Simple Hello World
This Easter egg will print a hello world message. Open the python terminal and try this code.
>>> import __hello__
Hello world!
Hello world can be printed using importing __phello__.
>>> import __phello__
Hello world!
2. The Zen of Python
As per the PEP20 , “The zen of Python” is written by Tim Peters it has 20 lines describing the guiding principles of Python’s design.
import this
Output
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!
3. The comics, Antigravity!
Experience the antigravity using single line of code!
import antigravity
Try it out, it will open xkcd web comic page.
4. The braces.
Official python does not support curly braces but it might change in the future. Let’s try to import braces from __future__ .
>>> from __future__ import braces
File "<stdin>", line 1
SyntaxError: not a chance
5. Hash of Infinity and NaN
Python provides a hash function. The hash of infinity and NaN(Not a Number) :
>>> hash(float('inf'))
314159
>>> hash(float('nan'))
0
The original reference of this Easter egg from Reddit.
6. Uncle Barry
A well-known Python developer Barry Warsaw (aka Uncle Barry) was ‘chosen’ to become the Friendly Language Uncle For Life (FLUFL).
He introduced some modifications PEP 401, replacement of inequality operator (!=) by diamond operator(<>). If you want to use this feature or Uncle Barry’s vision just import it from __future__.
from __future__ import barry_as_FLUFL
>>> 10 != 50
File "<stdin>", line 1
10 != 50
^
SyntaxError: with Barry as BDFL, use '<>' instead of '!='
But if we use diamond operator it will not show error.
>>> from __future__ import barry_as_FLUFL
>>> 10 <> 50
True
7.Python 3.9 PEG parser
The __peg_parser__ is a keyword introduced in Python 3.9 . It will throw error if we use it.
>>> __peg_parser__
File "<stdin>", line 1
__peg_parser__
^
SyntaxError: You found it!
8. Warning – Not for the faint of heart !
If we check the help for types.CodeType then it will show text “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:
9. Picking a place for meetup?
This is based on Geohashing , geohash function will compute a destination coordinates. For calculating meeting location, we have to give location coordinates and date.
>>> from antigravity import geohash
>>> # Your location, a date and that date's (or most recent) DJIA opening.
>>> geohash(37.421542, -122.085589, b'2005-05-26-10458.68')
37.857713 -122.544543
Help for geohash function
>>> help(geohash)
Help on function geohash in module antigravity:
geohash(latitude, longitude, datedow)
Compute geohash() using the Munroe algorithm.
10. The this.py
Let ‘s explore the source of The Zen of Python , this.py.
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]))
Yes, the string “s” is encrypted, it is ROT13 “rotate by 13 places” encrypted. It is special case of Caesar cipher.
Conclusion
There you have it,10 best Easter eggs in Python. Hope you enjoyed our article feel free to share it.