Stop Using Semicolons in Python. They are seldom useful and don’t look... | by Chaitanya Baweja | Towards Data Science

Coming from a C/C++ background, I am used to seeing a lot of semi-colons ; in code. They are used to represent statement termination.
But, Python does not mandate the use of semi-colons for delimiting statements. Yet, I often come across Python code littered with semi-colons.
Most recently, I was going through a Data Science with Python Course. The instructor was introducing conditional statements and wrote the following piece of code:
temp = 10;if temp <12: print('It is cold'); cold = True;
My reaction: Pure Horror!
Semi-colons are used only in atypical situations in Python. So, I prepared a small guide explaining why you shouldn’t use semi-colons in Python and point out the handful exceptions.
In many popular programming languages, you need to add a semi-colon at the end of every statement. For example, in C++:
int c = 10;int a = 5;printf('In C++, semicolon at the end is must');
But this is not true for Python.
Python is a no-bullshit programming language. It stays away from unnecessary characters and syntax.
In Python, a statement ends at the end of a line (exception for open brackets, quotes or parentheses). For example:
c = 10a = 5print('No semicolons in Python')
A semi-colon in Python denotes separation, rather than termination. It allows you to write multiple statements on the same line.
print('Statement 1'); print('Statement 2'); print('Statement 3')
This syntax also makes it legal to put a semicolon at the end of a single statement:
print('Why God? WHY?');
This statement means print(‘...’) and then do nothing. So, it’s actually two statements where the second one is empty.
Even though the language allows a semi-colon for delimiting statements, most Python programmers would have never used it in their code.
Why is that? It is NOT PYTHONIC.
Python is supposed to be clean and readable. Syntactic characters like semi-colons add unnecessary clutter.
If you send a code like this to an experienced Python programmer, you will never hear the end of it.
Forcing multiple statements onto one line makes a trivial code harder to read.
So, a logical question here would be: Why are Semi-colons allowed in Python?
I believe that it was done to make a transition from other programming languages slightly easier. Programmers with a background in Java, C++, and PHP habitually put a (useless) terminator at the end of every line.
Nonetheless, there are certain situations where semicolons come in handy.
The most commonly encountered one would be to run a short script from the shell using python -c '<script>'.
We need to type many expressions separated with semi-colon because we can’t use indentation here. Take a look:
python -c 'import math; t = math.pi;print(int(t))’
Another use case would be the exec statement:
exec('for i in [100,200,300]: print(i);print(i*2)')
When working in an interactive environment like Jupyter Notebook, the value returned by the last expression is printed out as a string.
This might be annoying in cases where we evaluate a function for its side effects. Let’s look at this issue in matplotlib where the side effect is a plot.
I will create a simple histogram in my Jupyter notebook.
As depicted in the figure below, running this script adds an extra unwanted line in the output: Text(0.5, 1.0, ‘Histogram’).
This is actually the return value of the call to .set_title(...).
To suppress the output, we append a semi-colon to the end of the last statement:
axis.set_title('Histogram', size=10);
Like many Python programmers, I believe that we should avoid adding any fluff/extra characters when writing code.
The semicolon is legal. But that doesn’t mean that you should use it.
Use it only when you have to.
I hope that you found this story informative and useful! You can read more about the Philosophy of Python here: