What is a Syntax Error in Python
Answer to this question “ what is a syntax error in python “ required few things to consider, let us have an idea about that.
Usually when user writes a program in python, he uses his full knowledge about logic and programming constructs.
Writing an error free program is not quite easy for a programmer. Like other programming languages, python also have commands, statements and rules of using python constructs. As python is new widely accepted language these days, it provides a lot of things which its predecessor languages were not offering, There are new features , new syntax rules, python also altered general syntax of few popular statements.
for example if statement in C , C++ and JAVA have syntax :
if ( boolean expression)
{
block of if
}
but in python the general syntax of if is changed as
if (boolean expression) :
block of if
Here you can see the change in syntax , python is uisng a colon symbol with if statement and for the block of if-statement python uses the concept of indentation.
Now the program shifted from C or JAVA to python can face difficulty in using if statement in Python, this could result in syntax error in case of python.
Python uses indentation for the purpose of blocks, and in C , JAVA , we use pair of curl;y braces. The new user of python will get syntax error messages when he will not use proper indentation.
There are many more things which are changed in python.
What is
Syntax error
We can say a syntax error is an error which comes when language users don't follow the proper syntax of the language constructs.
One can easily fix syntax errors , usually language compiler helps in this task. One can also follow some basic rules, while writing programs in python.
Here we are suggesting few things to be considered to avoid syntax errors in python
Don't use a keyword of python for the purpose of identifiers.
Use a colon (: ) after a for , while and if statements in python.
Use proper indentation, as it plays a vital role in python programming .
While using strings , check the proper use of quotes.
use braces , brackets with proper combination of opening and closing.
Always use a == for conditional statements instead of =, new programmer usually use them interchangeably. Here == is an equality operator and = is an assignment operator.
If you consider few things given above , there might be few syntax errors in the program. Moreover if your program length is short, you can go through the code carefully, this will also help in locating errors in python program.
After completing the code , you can execute your python program , if program is error free, you will get the desired results and if the program have syntax errors, compiler will flash errors. Now you can easily locate them and can correct them.
You can ask your queries in comments of this article.
Here I am submitting a python playlist link for new python learners. It will be helpful for them.
You are also requested to join this channel.