After doing all these coding interview problems on the blog, I think it's time we relaxed a bit and looked at something a little less serious. Python is after all named after Monty Python, a comedy group, rather than a family of snakes, so humour and good fun should be core to the language.
Python actually has a number of neat hidden features. I'm only going to be showing off a few here, but I'd encourage you to go looking for more on your own!
First up, we have
import this, which prints the Zen of Python to the console: a list of aphorisms that sort of sum up the design philosophy of Python.
If you were to look at the source code for
this.py, you'd actually find a garbled mess like this:
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. ... snip ...
You can see for yourself here.
The content is actually encrypted using a rotation cipher. Why? Probably so as not to spoil the surprise! Try
import this for yourself to see the real Zen of Python.
Ever wanted Python to greet you without having to write your own greeting app?
import __hello__ is your answer.
import __hello__ is really quite simple: it just prints
Hello world! to the console. If you're on Python 2, you get a slightly less enthusiastic,
Hello world... — probably a sign that it's time to move onto Python 3.
You can also use
import __phello__ which does the same thing.
This is one of my favourite Python Easter eggs, so I won't spoil the surprise. Just run this line in your terminal and enjoy.
If you find any other Python Easter eggs, we'd love it if you could share them with us on Twitter.
If you're eager to get back to improving your Python, check back on Thursday for another full length post. You might also be interested in our freshly updated Complete Python Course, with new sections on testing, Selenium, and Tkinter!