
Introduction:
Python is a remarkable programming language. It is simple to use and provides efficient results. There is a wide range of options available for the integrated development environments (IDE) such as Pycharm, visual studio code, Jupyter notebooks, etc. Even the simple Python IDLE is a phenomenal interpretable environment for beginners. No matter which development you choose, there are some errors you might encounter. These errors and issues occur mainly due to misjudgments, lack of in-depth knowledge on the particular topic, or just a silly blunder that could happen to anybody.
Today, let us discuss the five most common errors that anyone ranging from a beginner programmer to an expert can make due to lack of focus, knowledge, or precautionary steps. We will also discuss the type of error that is printed. And then understand the reasoning and how to avoid the particular blunder. So without further ado, let us get started with the five most common python errors and how to prevent them.
5 Common errors and their respective fixes:
1. Indentation –
Indentation refers to the number of spaces at the beginning of a code line or code block. This indentation spaces could occur after a conditional statement like an if-else statement or after a function. The usual amount of indentation is either four spaces with the space bar button or pressing the Tab button once. Indentation is exceptionally paramount in python. The indentation acts as an alternative for something like the curly braces "{}" in other programming languages like C, C++, or Java.
You can get the following indentation errors –
- This error occurs when you forget to give an indentation in the starting line of a function or code block.
IndentationError: expected an indented block
- This error occurs when your indentation does not occur within the code block or the program. This mistake commonly happens when you give two spaces within the desired code block, and the description of that particular line does not fit in the entire code.
IndentationError: unindent does not match any outer indentation level
- One of the lines of the code is not aligned with respect to all the other indentations.
IndentationError: unexpected indent
These errors can also occur when you are trying to blindly copy and paste code from a website into your working environment. The chief solution to this issue is to make sure you have consistency in spacing and fix each of these issues by looking at the description for each of the error occurring.
2. Saving filename same as module name –
This error often occurs when you save the filename similar to a module name, and you are utilizing that particular module to call a function in your respective program. This mistake results in an import error as your python file replaces the actual existing library module. So, when you try to call the function while running the module, it simply does not work. An example of this error can be noticed in the code block given below. Here, I have saved the file as gtts.py, and this is what I got –
ImportError: cannot import name 'gTTS' from 'gtts'
This error has a simple fix. Just don’t save the filename as the module name, which will be used. If you want to retain the particular module name, just add a simple underscore or a number at the end of the filename, and that should fix your error. 😃
To understand the gTTS module in a more concise manner, check out one of my previous articles.
3. Messing up the brackets –
There are basically three types of brackets, each serving a particular purpose. The square brackets "[]" for listing as well as indexing, the curly braces "{}" for dictionary or sets, and the parenthesis "()" for tuple operations. There is a chance that beginners are often confused with the right bracket to use, leading to a few errors that could happen.
- One way to declare a list with a variable is a = []. The alternative way to declare a list is as follows: a = list(). However, when you mess up the brackets here as a = list[], you end up with the following syntax error.
SyntaxError: invalid syntax
- While addressing the index of a list or a tuple, we always use the square bracket ‘[]’. Upon using a list like x = [1, 2, 3] or tuple like x = (1,2,3), and writing to index it is as x(1) will result in the following error.
TypeError: 'list' object is not callable
or
TypeError: 'tuple' object is not callable
The simple fix to this to make sure you always use the square bracket for obtaining value on a particular index.
This issue can be solved by using x[1], and you will receive the value 2.

4. Trying to use parenthesis while printing a numpy shape –
Elaborating and diving slightly deeper into the next error quite similar to the previous one. This next issue is slightly more of a beginner level or rookie mistake while you are trying to utilize the numpy module for Machine Learning. It is important to note that shape in numpy is an attribute and not a function. It returns a tuple that consists of the shape of the numpy array. The shape values in the tuple can be accessed by addressing the respective index of the desired context. An example to understand this error is as follows.
This results in the following error.
TypeError: 'tuple' object is not callable
The easy solution to this problem is by just using the a.shape without the parenthesis. This fix will provide the exact result that is required for achieving the perfect solution.

5. Local and Global Variables –
A local variable is one which is defined inside a specific code block or code statement. This local variable is only accessible within that specific code block and nowhere else. Formal argument identifiers also behave as local variables. These are also the variables that are defined as dummy variables in the function.
A global variable is one which is defined in the main program and not inside a function or specific code. This variable is accessible throughout the entire program. The name error occurs when trying to access a local variable in the main program.
NameError: name 'x1' is not defined
The best idea is to stick to the local variables within the code block and if you want to utilize the variable throughout the entire program, then use the global variable format.

Conclusion:
These are the five most common errors I made when I got started with Python and machine learning. I have no shame in admitting that I occasionally mess up these silly things sometimes even today. However, I hope we all learned the reasons why these errors can occur, and how exactly can they be prevented. Hopefully, we can all avoid these mistakes in the future. At the very least, not be stuck and hop over to stack overflow to find a solution to the problem. Let me know if I missed out on any other common mistakes made from beginners to experts, and I will make sure to cover it in the next article.
I would highly recommend all the viewers check out the below two articles to start your journey to master machine learning. They will be updated consistently, and this series will cover every topic and algorithm related to machine learning with python from scratch.
Starting Your Journey to Master Machine Learning with Python
Basics of Python and its Library Modules Required for Machine Learning
Thank you all for reading this article. I wish you all a wonderful day!