They are seldom useful and don’t look pretty
Published in · 3 min read · May 28, 2020
--
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-bullsh*t 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 = 10
a = 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: