Writing code is a constructive process in which we tend to make mistakes and sometimes these mistakes throw out specific errors.
Errors are nothing but mistakes that are identified by the compiler or the interpreter at run time. Various errors occur in Python, which we will study in depth as ‘End of Statement Expected Error’.
Introduction to Python
Python is an established trend in the industry. It is gaining popularity due to its easy and user-friendly syntax, endless libraries that make working on it easy, and Python is extremely easy to understand.
Python has a very robust error-handling mechanism that identifies that something is going wrong whenever we write a wrong code, and then it throws a relevant error which is very self-explanatory and understandable.
To know more about Python programming follow along with the linked article.
Importance of Understanding and Handling Errors in Python
Consider a situation where you have an error in your code, and whenever an error is found, the code just stops execution abruptly, and the process fails, leaving the program in an unstable state. This situation is very dangerous when working on a big project.
Error handling allows us to give an alternate way to the interpreter so that if an error occurs, there should be an alternate way to execute and the code should not execute abruptly, and the program should be in a stable state.
To add an error-handling mechanism, we need to understand what error we are handling, as we know that to solve a problem, it is very important to first understand that problem and then be better equipped to solve it. In this case, the error is the problem, and then error handling is the solution to that problem.
Hence it is important to understand the error and how these errors occur and then move ahead to understand how we can resolve these errors.
‘End of Statement Expected’
The ‘end of statement Expected’ is a common syntax error in Python, which occurs due to various reasons we will discuss extensively in the sections ahead.
This error mainly triggers when we do not follow the programming language’s syntax. Let’s understand and figure out how this ‘end of statement expected’ error occurs and likewise take a look at measures to avoid these errors.
The syntax is nothing but the way or the structure in which a certain language needs to be written. It is pre-determined by the programming language developers and cannot be changed after that. Syntax defines a set of rules to be followed when writing code in that specific language.
Python has certain specific rules with regard to syntax, which are pre-defined and cannot be changed. Python syntax is relatively easy to understand as it usually reads as normal English.
Some Important rule is as follow:
When using conditional statements, loops, functions, and classes, python expects indentation so that it identifies which piece of code belongs to which block.
Most programming languages that are being used require a semi-colon(;) so that the end of the statement is understood, but Python doesn’t require any sem-colon.
Comments in Python are shown in two methods: one is a single line which uses a hash(#), and the other is a multi-line, which is done using six double quotation marks (“”” ”””).
To get a better idea of Python syntax, you can check out The Python Tutorial.
Significance of Indentation and White-Spacing
Python follows a standard called PEP 8 which we will be discussing later, but for now, it is just a format in which Python code needs to be written.
Writing clean and understandable code was one of the major reasons behind the development of the Python programming language; indentations and white spacing play a very crucial role in maintaining readability and keeping the code clean. Indentation and white-spacing form an important part of Python syntax.
Statements in Python
Statements are the basic building blocks of Python code; everything from printing output to displaying output and something very basic, from assigning value to a variable to writing functions, can be considered a statement.
Some examples of statements are given below:
name = "George"
These small units come together and form a Python program, as shown below.
name = "George"
What is the ‘End of Statement Expected’ error?
This is a common error that is triggered when there are some mistakes in following the syntax. When the Python interpreter expects some specific symbols or characters at the end of a certain statement, it will throw this error.
We will see the different situations where this error can be triggered and see how to fix this error.
Causes of ‘End of Statement Expected’
Python’s basic syntax expects semi-colons at the end of every condition statement and loop
Below is an example of this error:
if 10 == 10
The above code will give the following error.
To fix this, we will simply add a semi-colon, and the error will be fixed.
if 10 == 10:
As we have discussed that indentations are important while writing Python code, if we are unable to indent code properly, it will error out.
Below is an example of this error:
if 10 == 10:
The above code will throw the following error.
To fix this, either press the space bar 4 times or just press the tab once before the statement, which is not indented properly, as shown below:
if 10 == 10:
Parenthesis always exists in pair; forgetting to write one of the two will lead to an error, as shown in the below example:
An easy fix to this problem will be putting in the missing parenthesis.
Mismatch quotes in strings
We know that Python accepts strings via both double and single quotations, but it should either be single quotes or double quotes, not both in combination, do this will lead to the following error:
To resolve this error, just identify the mismatch and choose either of the quotes and replace it with the matching quote as shown below:
What is a code block in Python?
A set or a group of statements that can be interacted with using a single name is called a code block. Conditional statements, loops, functions, and classes can be treated as the entry point of this code block. Every code block has an entry point, actual body, and exit point. We shall understand it better with the help of an example.
def calc(a,b, op):
if op == '+':
if op == '-':
if op == '*':
if op == '/':
I have implemented a simple function, this function takes two integers and one character as input parameters.
The above code is 10 lines long, but in practice, it can be accessed using a single line and a single name, as shown below:
As we can see, the above 10 lines of code are being treated as a single unit of code, which is nothing but a code block.
How can blocks be identified with indentation?
Python follows a formatting procedure in which a set of four spaces form an indent, and an element with the same number of indents belongs to the same block.
When a new block is encountered, the indentation level increases, and after successful execution, the block is exited, and the indentation level decreases.
In this way, you can identify which statement belongs to which code block with the help of indentation. Code blocks are determined with the help of indents.
Importance of Code-Blocks
Having well-maintained and properly indented code blocks is always a good practice to follow. As well organized code is easy to read and understand and looks very pleasant to see.
It becomes easy to track the flow of control and determine and identify bugs and errors, if at all there are any, in our program.
Best Practices to avoid the ‘End of Statement expected’ error
Using code editor with automatic indentation
Modern code editors or IDEs are well equipped with tools and extensions that assist you while writing code.
They become very handy as often they automatically add indents to places where required and give the code a proper format as per the language’s syntax. A code editor with excellent tools and extensions is an absolute life save for any developer.
Following the PEP 8 Guidelines
PEP 8 stands for Python Enhancement Proposal 8; it is a set of guidelines laid down by the Python community to set some standard practices for writing code to maintain the readability of code and keep the code clean
Some important guidelines in context to our point of concern are:
Four spaces are identified as an indentation
Binary operators need to be padded by single spaces from both sides, i.e., before and after
The top level of functions needs to have two blank lines.
Consistent Indentation Practice
Inconsistent tabs, spaces, and incorrect indentation will lead to an ‘End to Statement Expected’ error. So it is important to follow a consistent format of using indents and stick to the guideline mentioned in PEP 8 to avoid these syntax errors.
Importance of Clean Code
As developers, it is important to understand the necessity of well-readable and understandable code as it is easy to work ahead and maintain such code rather than shabby and untidy code, which is not appreciated by anybody.
Hence it is important to have a clean and tidy code so that even when we work in a team environment others will be able to handle and work on our code and make things easier.
To conclude this article, let’s summarize whatever we’ve been through in this article. To begin with, we brushed up our knowledge about Python and its basic syntax. We also understood the importance of using indentations effectively in depth.
Later on, we took a deep dive into the ‘End of Statement Expected’ error, we saw the different reasons why the error occurs, and we also resolved those issues with examples.
We took an overview of what a code block is, then we came on to the best practices to be followed to avoid syntax errors, understood what PEP 8 is, and also the importance of maintaining clean code.