Python – Sytax errors
Syntax errors in Python
Syntax errors are also called as parser errors are the most common errors. They arise when the python parser is unable to understand a line of code in the program. These errors are almost fatal which means there is no way to successfully execute the program if the program contains syntax errors.
The error is displayed at the piece of line with a little arrow pointing at the earliest point in the line where the error is detected. The error is least detected at the token preceding the arrow. Output displayed is the file name and line number of the code where the error is present. So that, it is easy to detect the error in the respective program.
Whereas, IDLE highlights the syntax error in the program. Almost all the syntax errors are caused due to typos, incorrect or unmatched indentations, incorrect arguments.
In order to avoid some common syntax errors in python, there are some ways to follow. They are:
- Python keyword can not be used as a variable name.
- Verify the indentation whether it is consistent or not. It is better to use a tab for the indentation in the program. Each level of the code should be nested with the same amount of indentation.
- We have to place a colon at the end of the header of compound statements like for, while, if and def statements.
- Strings in the code should always be in matching quotation marks.
- Terminate the string properly if code contains any multiline strings. Because it may cause an invalid token error.
- Close all the brackets accordingly.
- Check for the classic (=) instead of (==) inside the conditional statement.