Python Tips and Tricks for Better Code
Python is known for its readability and simplicity, but there are many features and techniques that can make your code even more elegant and efficient. Here are some of my favorite Python tips and tricks.
1. List Comprehensions
Instead of using loops to create lists, use list comprehensions:
# Traditional way
squares = []
for i in range(10):
squares.append(i ** 2)
# Pythonic way
squares = [i ** 2 for i in range(10)]2. Dictionary Comprehensions
Similar to list comprehensions, but for dictionaries:
# Create a dictionary of squares
squares_dict = {i: i ** 2 for i in range(10)}3. The Zip Function
Combine multiple iterables:
names = ['Alice', 'Bob', 'Charlie']
ages = [25, 30, 35]
for name, age in zip(names, ages):
print(f"{name} is {age} years old")4. Enumerate for Index and Value
Get both index and value when iterating:
fruits = ['apple', 'banana', 'orange']
for index, fruit in enumerate(fruits):
print(f"Index {index}: {fruit}")5. Context Managers for Resource Management
Use with statements for automatic resource cleanup:
# Reading files
with open('file.txt', 'r') as f:
content = f.read()
# Working with databases
import sqlite3
with sqlite3.connect('database.db') as conn:
cursor = conn.cursor()
cursor.execute("SELECT * FROM users")6. F-Strings for String Formatting
Clean and readable string formatting:
name = "Alice"
age = 25
# Modern f-string
message = f"{name} is {age} years old"
# With expressions
result = f"The sum is {2 + 3}"7. The Underscore Placeholder
Use underscore as a placeholder for unused variables:
# Unpacking with unused values
name, _, age = person_data
# In loops (when you don't need the index)
for _ in range(5):
print("Hello!")8. Generator Expressions
Memory-efficient iteration:
# List comprehension (creates full list in memory)
squares = [i ** 2 for i in range(1000000)]
# Generator expression (lazy evaluation)
squares_gen = (i ** 2 for i in range(1000000))9. Multiple Assignment
Swap variables and assign multiple values at once:
# Swap variables
a, b = b, a
# Multiple assignment
x, y, z = 1, 2, 3
# Extended unpacking
first, *middle, last = [1, 2, 3, 4, 5]10. The Walrus Operator (Python 3.8+)
Assignment expressions for cleaner code:
# Before
import re
pattern = re.compile(r'\d+')
match = pattern.search(text)
if match:
print(f"Found: {match.group()}")
# With walrus operator
if (match := re.search(r'\d+', text)):
print(f"Found: {match.group()}")Conclusion
These Python tips can help you write more concise, readable, and efficient code. The key is to balance cleverness with clarity - your code should be easy to understand for others (and your future self!).
What are your favorite Python tips? Share them in the comments!