10 Python Tricks You Wish You Knew Earlier

Alan Constantino
4 min readSep 19, 2024

1. Follow the Zen of Python

Python has a built-in philosophy that can guide your coding journey. Simply type import this in your Python shell, and you’ll see the Zen of Python—19 aphorisms that promote clarity, simplicity, and beauty in code. These principles will help you make decisions on how to structure your code. The key takeaway: readability counts.

import this

Learn More: To dive deeper into the Zen of Python and its guiding principles, check out the official Python documentation.

2. Use List Comprehensions for Concise Code

List comprehensions allow you to generate lists in a single, readable line of code. Instead of using loops, try this shorter approach:

# Traditional way
squares = []
for i in range(10):
squares.append(i**2)

# List comprehension way
squares = [i**2 for i in range(10)]

Learn More: If you want to master list comprehensions and even expand to set and dictionary comprehensions, check out the Python docs.

3. Master the F-Strings for String Formatting

Python 3.6 introduced f-strings, which make string interpolation cleaner and faster. It’s a more efficient way to format…

--

--