Skip to main content

5 Fun Python Easter Eggs

Welcome to this comprehensive tutorial on five fun and famous Python Easter eggs that every Python developer should know. Whether you're new to Python or have been using it for a while, these Easter eggs are sure to amuse and surprise you. Let's dive in!

1. Hello World with a Twist​

The Traditional Way​

Usually, when you start a new program, you test it out by creating a simple "Hello, World!" program:

print("Hello, World!")

The Easter Egg Way​

To save time and avoid common mistakes like missing commas or incorrect capitalization, you can use a built-in Easter egg. Python has a hidden __hello__ module that you can import to print "Hello, World!" consistently.

import __hello__

When you run this code, it prints "Hello world!" to the console. Note the absence of the comma here, but it's a fun and consistent way to print the classic message.

2. The Zen of Python​

Discovering the Zen​

The Zen of Python is a collection of guiding principles for writing computer programs in Python. You can read it online, but there's a more Pythonic way to access it:

import this

This prints the entire Zen of Python, a beautiful poem by Tim Peters, directly in your console.

The Secret Challenge​

There’s an additional fun challenge within the Zen of Python module. Try this:

print(this.d)

This prints a seemingly random string of letters. It's actually a ciphered message. Here's the dictionary that might help you decode it:

print(this.s)

This provides a mapping that can be used to decipher the message.

3. The Friendly Language Uncle For Life (FLUFL)​

The Diamond Operator​

Barry Warsaw, a core Python developer, humorously proposed using the diamond operator (<>) for "not equal to" comparisons instead of the usual !=. You can explore this joke proposal using the __future__ module.

from __future__ import barry_as_FLUFL

# Now try:
print(10 <> 20) # This will return True

This only works in the interactive Python console, not in scripts directly. In a script, you would wrap it in a string and evaluate it.

code = '10 <> 20'
print(eval(code)) # This will return True

4. Braces Are Not Allowed​

The Humor in Future Imports​

Python emphasizes readability, and part of this ethos includes avoiding the use of braces {} for code blocks. There's a humorous Easter egg reflecting this philosophy.

from __future__ import braces

Running this code will immediately raise a SyntaxError with the message "not a chance," underscoring Python's stance on code readability.

5. The Anti-Gravity Comic​

The Fun of Importing Anti-Gravity​

This is probably the most famous Python Easter egg. Importing the antigravity module opens a web browser with an xkcd comic that humorously celebrates Python's ease of use.

import antigravity

Exploring Geohashes​

The antigravity module also includes a geohash function that computes a geohash from given latitude, longitude, and date.

import antigravity

# Example coordinates for the Falkland Islands and a specific date
latitude = -51.7963
longitude = -59.5236
date = b'2024-05-17'

antigravity.geohash(latitude, longitude, date)

This opens the comic and prints a geohash string to the console. The geohash is a way to encode geographic locations into short strings, which is useful for spatial indexing.