We have the answers to your questions! - Don't miss our next open house about the data universe!

Avoid These Top 5 Common Python Coding Mistakes

- Reading Time: 3 minutes

Learn to code in Python. You've been thinking about it for a while, and now you're ready to get started! You download anaconda and the famous Titanic dataset, install jupyter and off you go. As with all beginnings, you may change your mind at the first hurdle, and even if you're willing, some of them can make getting started a bit tricky.

1. Do not read error messages.

Yes, at first seeing a lot of red on your screen is normal.

Python gives you the opportunity to understand your errors by generating an error message and indicating on which lines of code they occur.

You can then run the cell of code over and over again, hoping that one day the message will disappear, but I’d rather tell you right now that it’s a waste of time.

Now that you know this, remember that you should always read error messages and try to understand them using the documentation available or by searching forums such as stack overflow. Always remember that if you’ve made a mistake, someone else must have made it before you, and don’t hesitate to ask for help on a forum.

Of course, at first you’ll find it time-consuming and demotivating, but that’s the way to improve. With experience, you’ll get quicker at understanding these error messages.

2. Do not comment on your code

Here’s another very common mistake: Coding without comment. After a while, you’ll end up with a block of code that you can’t remember why you coded it the way you did, or why you used the template you did. Also, if people come up behind you and read your code without comment, you can be sure that they won’t understand everything.

We each have our own way of coding certain variables, but also of approaching a problem and solving it.

#So it’s always a good idea to provide a minimum of commentary on your code.

3. Do not read documentation

“But how does this operator work?”
“What are the parameters of this model?” “What are the attributes?”

If you’re in any doubt about how a tool works or how to set it up, always read the documentation. This will help you understand what you’re doing, what to use and how. You can use the help function without moderation, which will give you all the information you need. On the Internet, sites such as Scikit-Learn offer very detailed documentation on the models used.

4. Think that Python indexes in 1

Getting started in Python usually means starting to manipulate lists, and you’re likely to come across this error message: “IndexError: list index out of range”. Like any beginner, you’ll think that python indexes from 1. Well, it doesn’t! Always remember that python indexes to 0. This will save you many hours of wasted time trying to understand the cascading IndexError.

5. Do not allocate a result

The easiest way to explain this type of error is with an example.

You create a variable b = 2 and want to add 1 to it. You go to b + 1 and execute the code, but is it really equal to 3? You’ll quickly realize that no, b is always equal to 2. Incomprehensible! Can’t Python do simple addition? In our example, we’ve executed a line of code adding 1 to b, but if we don’t tell python to store this new value of b in b itself, it won’t do it for us (in other words, we write b=b+1 or b+=1).

This may seem silly, but it’s a common mistake when you’re just starting out.

Want to get started with Python or improve your skills? Take a look at our Data Analyst, Data Scientist and Data Engineer courses.

Need more information? Contact us directly!

You are not available?

Leave us your e-mail, so that we can send you your new articles when they are published!
icon newsletter


Get monthly insider insights from experts directly in your mailbox