loading...
Cover image for Common Mistakes Junior Pythonistas make

Common Mistakes Junior Pythonistas make

amigosmaker profile image amigos-maker ・3 min read

What Are The Most Common Mistakes Junior Python Developers Should Avoid?

We all make mistakes, but sometimes these mistakes can be easily avoided if you've been given some worthwhile advice by someone whos been there before. Luckily, when it comes to Python, plenty of people have taken note of the issues they had while learning the language.

As with all coding languages, its normal to make mistakes here and there. There are a few mistakes, however, that would be easy to avoid if you know what to look for! We've compiled a list of things to watch out for while you're learning Python.

Incorrect Indentation

Whereas most languages use indentation to improve their readability but dont depend on the practice, Python has woven indentation right into the fabric of their language. This means you cant afford to make any errors when it comes to formatting your code.

For Python, the rule is to have 4 spaces for indention. Don't mix with 2,3,5 or a different amount of spaces as Python will simply not run your program.

Youll need to learn how to use Python to separate your blocks of code so everything flows nicely. Its important to make sure you get it right the first time. Not indenting properly can lead to a bug in your code if youre not careful. Debugging is tiresome enough on its own, but looking for a missing space or extra space will drive you crazy!

Avoid this mistake by practicing the fundamentals of indenting before you get too far into other aspects of Python. This will decrease your likelihood of making an error later on.

Error Handling

Speaking of errors, if you're new to programming error handling itself may prove to be a little tricky when you're starting out. All errors will seem a bit cryptic starting out - make sure you understand the basics of every error handling message you encounter by looking for information from other coders on StackOverflow or GitHub. This will help you avoid making the same mistake as you continue to learn.

Errors in Python are handled using a traceback. You'll get most of your errors from making a mistake in your syntax. This means you've likely forgotten a colon, parentheses, or have indentation issues.

Avoid spending too much time on this mistake by going through your code line by line and reading it out loud. Make sure you work through the purpose of each line of code in your head as you do this. Paying attention to the small details will help you pick up on your syntax errors quickly so you can move on to more important issues.

The LEGB Rule

Figuring out scope can be a bit of a pain for a newcomer in any language, but it's especially true in Python. Whereas other languages usually just have two scopes to work with (local and global variable scope) Python requires you to work with four scopes. These are commonly referred to as the LEGB Rule.

The LEGB Rule refers to the way the variables are defined - Local, Enclosing, Global, and Built-in. You'll need to be careful about your decisions to assign variables. Once you make a decision about where to put your variables Python will consider them to be local to that particular scope - which may create problems later on if you're not intentional with your assignments.

Related links

Posted on by:

Discussion

markdown guide