DEV Community

Subham Behera
Subham Behera

Posted on

Exploring the Uncharted: Fun and Weird Stuff in Python

Python is widely celebrated for its simplicity and readability, making it a favourite among developers and data scientists. But beyond its practical applications, Python has a playful and quirky side that's worth exploring. In this post, I'll take you on a journey through some lesser-known and weird features of Python that can add a bit of fun and surprise to your coding experience.

1. The Zen of Python

Before we dive into the weird stuff, let's start with a hidden gem that provides a philosophical foundation for Python: The Zen of Python. You can access it by importing this:

import this
Enter fullscreen mode Exit fullscreen mode

This will print out a set of aphorisms written by Tim Peters, which serve as guiding principles for writing Python code. It's a great reminder of why Python is such a delightful language.

2. Python's Easter Eggs

Python has several Easter eggs hidden within its standard library. One of the most famous is the "import antigravity" module, which opens a webcomic by XKCD:

import antigravity
Enter fullscreen mode Exit fullscreen mode

Another fun Easter egg is the import hello:

import __hello__
Enter fullscreen mode Exit fullscreen mode

This will print "Hello world!" to the console. These Easter eggs are just for fun and don't serve any practical purpose, but they showcase the playful nature of the Python community.

3. The Walrus Operator

Introduced in Python 3.8, the walrus operator (:=) allows you to assign values to variables as part of an expression. It can make your code more concise and readable in certain situations:

# Traditional way
data = input("Enter something: ")
while data != "quit":
    print(f"You entered: {data}")
    data = input("Enter something: ")

# Using the walrus operator
while (data := input("Enter something: ")) != "quit":
    print(f"You entered: {data}")
Enter fullscreen mode Exit fullscreen mode

4. The else Clause in Loops

Did you know that loops in Python can have an else clause? The else clause executes only if the loop completes normally (i.e., it doesn't encounter a break statement):

for i in range(5):
    if i == 3:
        break
    print(i)
else:
    print("Loop completed without break")

for i in range(5):
    print(i)
else:
    print("Loop completed without break")
Enter fullscreen mode Exit fullscreen mode

5. String Formatting

Python offers several ways to format strings, from the older % operator to the format() method and the more recent f-strings (formatted string literals):

name = "Alice"
age = 30

# Using % operator
print("Hello, %s! You are %d years old." % (name, age))

# Using format() method
print("Hello, {}! You are {} years old.".format(name, age))

# Using f-strings
print(f"Hello, {name}! You are {age} years old.")
Enter fullscreen mode Exit fullscreen mode

Conclusion

Python is not just a tool for getting things done; it's also a playground for exploring new ideas and having fun. By delving into these weird and lesser-known features, you can not only enhance your Python skills but also discover the joy and creativity that Python can bring to your coding experience. Happy coding!

Top comments (0)