Does Python support semicolons?
So what about Python?
The short answer is that Python supports semicolons, but there is no need to use them, and they are generally considered bad style.
Python accepts semicolons to end statements, such as:
x = 1; y = 2;
But this is unnecessary, as a new line, outside of brackets, is sufficient to end a statement.
Python only added semicolons to support more than one statement per line, such as:
x = 1; y = 2
Using this capability is considered bad style though, as it reduces both readability and debuggability. The popular code checker Flake8 complains about any use of semicolons:
$ flake8 example.py example.py:1:6: E702 multiple statements on one line (semicolon)
Additionally, the code formatter Black will reformat code to one statement per line, removing all semicolons:
x = 1 y = 2
Therefore we should avoid using semicolons in production code. But there are occasional uses for them in other places.
Sometimes it’s useful to run Python in a command line pipe. Here the semicolon comes in useful to write a command in a single line.
Por exemplo, imagine we wanted to call Python’s
re.escape() on output from another program. We can do so in a single line like so:
$ echo "21 ^ 2" | python -c 'import re; print(re.escape(input()))' 21\ \^\ 2
Thanks to shell string rules, multiple lines are possible without semicolons, but it can look odd:
$ echo "21 ^ 2" | python -c 'import re print(re.escape(input()))' 21\ \^\ 2
I’ve used semicolons in the past for invoking Python’s debugger, pdb. It’s convenient to import and start pdb in one line, which I inserted with a text editor snippet whenever required:
import pdb; pdb.set_trace()
Since I didn’t want debugging code to get committed, it was also useful that Flake8 flagged warnings on such lines.
Invoking pdb in this manner is slightly outdated though. Since Python 3.7, it’s possible to start pdb with the
breakpoint() builtin, no import required:
Thus I no longer need this snippet. It might still be useful to use semicolons in other snippets though.
The improved Python shell IPython gives special meaning to semicolons at the end of lines. When we run an expression in IPython, it outputs the result:
In : 2*2 Out: 4
If there are multiple expressions (from semicolons, newlines, etc.), IPython will only output the final result:
In : 2*2; 3*3 Out: 9
This is normally useful, but occasionally we don’t want to see the return value. We can do this by ending the line with a semicolon:
In : 2*2;
This is particularly useful in Jupyter Notebooks when output would distract the reader. For example, calling matplotlib’s
title() returns a
Title() object, but we don’t care about seeing it in
Out [...] when the title is visible on the plot. We can suppress the output from the cell by ending it with a semicolon:
In : import matplotlib.pyplot as plt In : plt.plot([1, 2, 3]) plt.title("My Plot"); <plot would be visible here>
Learn how to make your tests run quickly in my book Speed Up Your Django Tests.
One summary email a week, no spam, I pinky promise.