![](https://crypto4nerd.com/wp-content/uploads/2023/07/0bavWiPOiKcycPFJ0-1024x1536.jpeg)
Hey there, fellow Python enthusiasts! I’m Gabe A, and today, I’m excited to share with you some of the game-changing lessons I’ve learned in my journey as a Python developer and data analyst.
Over the years, I’ve had the privilege of learning from some of the best minds in the Python community, and these insights have transformed the way I write Python code.
In this blog post, I’ll be revealing ten practices I’ve stopped doing in Python after learning from the experts. So, grab your favorite beverage, and let’s dive in!
1. Stop Using “print” for Debugging
Back in the early days of my Python journey, I often resorted to the “print” statement for debugging purposes. However, I soon discovered more effective methods. Now, I’m a big fan of Python’s built-in “logging” module. It allows for more control over the debugging process, and you can even customize the log level and output format. Check out this practical example:
import logginglogging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
def complex_function(arg1, arg2):
logging.debug(f"arg1: {arg1}, arg2: {arg2}")
# Rest of the function code...
complex_function(42, 'hello')
2. Avoid Using len() Repeatedly in Loops
In the past, I often used “len()” inside loops, not realizing that it’s an expensive operation, especially for large collections. Thankfully, I learned about the Pythonic way to avoid this by using “enumerate.” Let me show you an example:
# Non-Pythonic way
my_list = [1, 2, 3, 4, 5]
for i in range(len(my_list)):
print(f"Index: {i}, Value: {my_list[i]}")# Pythonic way
for i, value in enumerate(my_list):
print(f"Index: {i}, Value: {value}")
3. Ditching Old-Style String Formatting
I used to rely on old-style string formatting with the “%” operator, but now I’ve fully embraced the power of f-strings. They are more readable, concise, and allow evaluating expressions within the curly braces:
name = "Gabe"
age = 30
print(f"Hi, I'm {name} and I'm {age} years old.")