Sharing These Python Tips
Despite having programmed in Python for many years, I’m still amazed by how clean the code can be and how well it adheres to the DRY (Don’t Repeat Yourself) programming principle. My experience over the years has taught me many small tricks and pieces of knowledge, mostly gained from reading popular open-source software like Django, Flask, and Requests.
Here are a few tips I’ve picked out that are often overlooked, but can genuinely help us in daily programming.
1. Dictionary Comprehensions and Set Comprehensions
Most Python programmers know and use list comprehensions. If you’re not familiar with the concept of list comprehensions, it’s a shorter, more concise way to create a list.
>>> some_list = [1, 2, 3, 4]
>>> another_list = [ x + 1 for x in some_list ]
>>> another_list
[2, 3, 4, 5]
Since Python 3, we can use the same syntax to create sets and dictionaries:
……

