<p>Python does not usually use explicit line-ending characters; the parser can almost always figure out where a statement ends from the positioning of newlines. However, Python's syntax allows you to use a semicolon to end a statement instead, which allows you to place multiple statements on a single line. I have (<a href="https://codegolf.stackexchange.com/a/245322/108218">almost</a>) never used this feature, and I can't ever recall seeing it in production code. Does the ability to end a statement with a semicolon allow you to do anything that cannot be done simply by using newlines?</p>

<syntax><parsing><python>
Does Python's semicolon statement ending feature have any unique use?
CC BY-SA 4.0
2023-10-17T14:34:56.907Z
I don't know about Python in specific, but in Swift the only time I've seen this used is `lock(); defer { unlock() }`.
Bbrk24
CC BY-SA 4.0
2023-10-17T14:36:58.787Z
Also, for the purposes of code golf, `;` and newline are both 1 byte :P
Bbrk24
CC BY-SA 4.0
2023-10-17T16:03:41.967Z
In Koltin I've used it in short lambdas, e.x. `{ consume(IF); parseIf() }`
Seggan
CC BY-SA 4.0
2023-10-17T16:49:37.913Z
@Bbrk24 That's not true for indented blocks; `;` saves on indentation characters.
kaya3
CC BY-SA 4.0
2023-10-18T11:44:50.617Z
Semicolons are extremely useful when writing python code in a comment on stackexchange!
Stef
<p>I used this feature when embedding Python code in an editor for a Jupyter-notebook-like experience. The code is a recipe for baking bread. It is also a template, meant to be loaded into the current context, then lightly edited to change the amounts in the first paragraph:</p>
<pre class="lang-python prettyprint-override"><code>flour_grams = 1000
whole_wheat_percent = 45
honey_grams = 30
sourdough_starter_percent = 15
starter_is_watery = False
</code></pre>
<p>The rest of the script was math and boilerplate. For the code to be embedded nicely in the page, the beginning needed to be written as above, but the rest needed be minified to not take up the whole page vertically. (Eventually I had to use <code>exec(code_str)</code> because standard minification was insufficient.)</p>
<p>This is a niche example (embedding the script in where it must be vertically compact), but I suspect a lot of different niche examples end up justifying the feature.</p>

CC BY-SA 4.0
2023-10-20T14:22:24.440Z
2023-10-20T14:24:27.777Z
2023-10-20T14:24:27.777Z
piojo
<p>There have always been embedded scripting languages, and although I have never seen that use case, it was certainly a consideration even in the early days of python 2.</p>
<p>Other posters have mentioned bash, snippets, and GUI python, but the class is larger than that.  Consider python embedded in HTML: HTML specifically ignores whitespace, and you cannot ensure that whitespace is retained in that environment.</p>
<p>Generally, when you embed python script inside something that is not just a text file in a file system, you have to conform to the constraints of that system.</p>
<p>I've always thought that the semi-colons were just included as a concession to c programmers: that's certainly the way I saw them used 20 years ago.  But embedded scripting with the cpython engine was certainly an active consideration then, much more than it is today.</p>

CC BY-SA 4.0
2023-10-20T23:12:06.887Z
2023-10-20T23:12:06.887Z
david
CC BY-SA 4.0
2023-10-21T00:09:14.683Z
Semicolons aren't enough to make Python work in an environment where whitespace isn't preserved, because semicolons are only for sequencing, they cannot indicate nesting depth. Consider `if a: if b: foo() else: bar()` ─ you can't use semicolons to disambiguate this.
kaya3
CC BY-SA 4.0
2023-10-21T00:13:07.357Z
Also isn't not really accurate to say HTML doesn't preserve whitespace; otherwise the CSS property `white-space: pre;` would be useless. Some transformations of HTML might not preserve whitespace, but those transformations are only correct when the whitespace indeed doesn't matter.
kaya3
CC BY-SA 4.0
2023-10-21T00:36:45.467Z
@kaya3, Complex if/else statements can be trivially decomposed into repeated If statements.  HTML is just an example: I'm glad you understood the example.
david
CC BY-SA 4.0
2023-10-21T14:51:24.873Z
If you have any reference showing that either the Python designers intended semicolons to be used in environments where newlines weren't preserved, or examples of Python being embedded in HTML without whitespace-preservation in real projects, please add them to your answer. Otherwise as far as I can tell this is just false; I am aware of projects which embed Python in HTML (e.g. [PyScript example](https://pyscript.net/examples/simple_clock.html)) but in reality these rely on HTML *preserving* whitespace.
kaya3
<p>I've never used this feature in Python, but I am familiar with other languages that support semicolons: Algol, C, Java, etc. When I used place two or more statements on the same line, I was sending anyone reading the code a message: these two statements are tightly bound, and should be considered together. I have got out of the habit of using the idiom, perhaps because I'd tend to bind the two statements together by putting then into a single method of an object.</p>
<p>E.g. x=1;y=1,z=1;  // They are coordinates in 3D, so they hang out together.</p>
<p>Compare: p = Point(x=1,y=1,z=1)</p>

CC BY-SA 4.0
2023-10-19T22:02:54.750Z
2023-11-26T22:06:45.147Z
2023-11-26T22:06:45.147Z
Simon Crase
CC BY-SA 4.0
2023-10-20T01:49:02.347Z
In Python I'd write `x,y,z = 1,1,1` to convey that they are bound at the same time, no need for a semicolon
Pete Kirkham
<p>In Python specifically, the main use case is to eliminate newlines in a bit of code you are running.</p>
<p>The common case for this is using the <code>-c</code> argument for the Python interpreter to have it run a bit of code you specify on the command line (instead of having to load it from a file). I’ve also seen it used on rare occasion with <code>exec()</code> or <code>eval()</code> when the code being passed in to either is relatively short, but would need multiple lines if written in ‘full form’.</p>
<p>The other big use for this is code golf. As an example, this:</p>
<pre class="lang-python prettyprint-override"><code>for a in range(0, 10):b(a);c(a);d(a)
</code></pre>
<p>is semantically equivalent to this, but twelve bytes shorter:</p>
<pre class="lang-python prettyprint-override"><code>for a in range(0, 10):
    b(a)
    c(a)
    d(a)
</code></pre>
<p>Twelve bytes does not sound like much, but that’s a 24% savings in this case, and even for a longer program it’s pretty darn significant when golfing competitively.</p>
<hr />
<p>There are a couple of interesting caveats to this though. The two big ones are that:</p>
<ol>
<li>Using a <code>;</code> like this in Python only works with compound statements (if/elif/else, for/in, while, try/except/finally/else, with, and possibly match/case, though I’ve never tried with that one). In particular, this means you can’t use a <code>;</code> in Python to define a lambda expression with multiple statements in it’s body, or to have multiple statements evaluated as part of a ternary operator.</li>
<li>If there is no newline between the <code>:</code> in a compound statement and the first statement of the block that follows that part of the compound statement, the body of that block will continue until the next newline. This means that in my first example above, the only ways to denote the end of the loop’s body are to either insert a newline after the final statement, or have the end of the file be immediately after the final statement. This is a bit annoying for code golf, but it actually has some interesting uses, such as making it possible to write functions which have only one statement in their body all on one line (which is especially useful when writing <a href="https://typing.readthedocs.io/en/latest/source/stubs.html" rel="noreferrer">type stubs</a>).</li>
</ol>

CC BY-SA 4.0
2023-10-18T02:06:08.557Z
2023-10-18T02:06:08.557Z
Austin Hemmelgarn
CC BY-SA 4.0
2023-10-18T14:35:56.770Z
If you're willing to write the block on just 1 line, you're surely also willing to forego PEP-8's recommendation of 4-space indentation. If you change the block indent to just 1 space then you only save 3 bytes. But if you're golfing, I guess every byte counts.
Barmar
CC BY-SA 4.0
2023-10-18T14:58:35.897Z
@Barmar or even one tab, which looks nicer
Chris H
<p>Similar to what <a href="https://langdev.stackexchange.com/a/3031/2753">abel1502 said</a> about executing code from bash, I find myself using semicolons when using <code>pdb</code> and trying to do anything involving multiple lines. It's a lot easier to deal with for one-off exploration of the state after <code>pdb.set_trace()</code> and doesn't require doing <code>interact</code></p>
<p>Ex:</p>
<pre class="lang-python prettyprint-override"><code>import pdb
import random

def function_to_debug():
    result = []
    for a in range(100):
        b = random.randint(1, a)
        result.append((a, b))
        pdb.set_trace()
    # rest of the function
</code></pre>
<p>Then when it pauses, I might want to do some sort of exploration of stuff</p>
<pre class="lang-python prettyprint-override"><code>(pdb) test=[]
(pdb) for i, j in result:print(i);test.append(j)
</code></pre>
<p>This isn't exactly a representative example, but a very simplified example of how I might want to mess around with the currently available variables when debugging.</p>

CC BY-SA 4.0
2023-10-18T08:35:00.063Z
2023-10-18T09:18:34.150Z
2023-10-18T09:18:34.150Z
fyrepenguin
CC BY-SA 4.0
2023-10-18T09:06:45.540Z
Could you add an example to how you use it with pdb?
I myself use it a lot like this: `import pdb; pdb.set_trace()`. But your answer makes it sound like you can do more with it? I'm curious about that.
Opifex
CC BY-SA 4.0
2023-10-18T09:21:59.153Z
@Opifex a more concrete/useful example doesn't come to mind right this second, but this is a very generic example of where I might want to both print one part of a tuple and save out the second element, maybe to do something else with.  Personally I use `pdb.set_trace()` when exploring less-than-well-documented code I don't understand to see what the contents of variables & such actually are in practice. Hopefully this is of use though
fyrepenguin
CC BY-SA 4.0
2023-10-18T13:19:35.483Z
Ahh, I understand now. You use it *inside* of pdb. Not in the python code. That makes sense, thanks!
Opifex
<p>When executing a python snippet via <code>python -c</code> from bash, I find using semicolons more convenient than having to inject newlines. (In particular when the snippet has <code>import</code>s, and thus cannot be done in a single statement)</p>

CC BY-SA 4.0
2023-10-17T15:19:56.180Z
2023-10-17T15:19:56.180Z
abel1502
CC BY-SA 4.0
2023-10-17T23:35:37.310Z
The same is also true of `timeit` statements (and its `setup` argument)
Dillon Davis
# Does Python's semicolon statement ending feature have any unique use?
Posted by: username_0 on 2023-10-17
Python does not usually use explicit line-ending characters; the parser can almost always figure out where a statement ends from the positioning of newlines. However, Python's syntax allows you to use a semicolon to end a statement instead, which allows you to place multiple statements on a single line. I have ([almost](https://codegolf.stackexchange.com/a/245322/108218)) never used this feature, and I can't ever recall seeing it in production code. Does the ability to end a statement with a semicolon allow you to do anything that cannot be done simply by using newlines?
**Tags:** syntax parsing python
> **Comments:**
> **username_1**: I don't know about Python in specific, but in Swift the only time I've seen this used is `lock(); defer { unlock() }`.  
> **username_1**: Also, for the purposes of code golf, `;` and newline are both 1 byte :P  
> **username_2**: In Koltin I've used it in short lambdas, e.x. `{ consume(IF); parseIf() }`  
> **username_3**: @Bbrk24 That's not true for indented blocks; `;` saves on indentation characters.  
> **username_4**: Semicolons are extremely useful when writing python code in a comment on stackexchange!  
---
## 6 answers
### Answer by username_14 on 2023-10-17. Score: 43
When executing a python snippet via `python -c` from bash, I find using semicolons more convenient than having to inject newlines. (In particular when the snippet has `import`s, and thus cannot be done in a single statement)
> **Comments:**
> **username_15** (2023-10-17): The same is also true of `timeit` statements (and its `setup` argument)  
---
### Answer by username_9 on 2023-10-18. Score: 12
In Python specifically, the main use case is to eliminate newlines in a bit of code you are running.
The common case for this is using the -c argument for the Python interpreter to have it run a bit of code you specify on the command line (instead of having to load it from a file). I’ve also seen it used on rare occasion with exec() or eval() when the code being passed in to either is relatively short, but would need multiple lines if written in ‘full form’.
The other big use for this is code golf. As an example, this:
for a in range(0, 10):b(a);c(a);d(a)

is semantically equivalent to this, but twelve bytes shorter:
for a in range(0, 10):
    b(a)
    c(a)
    d(a)

Twelve bytes does not sound like much, but that’s a 24% savings in this case, and even for a longer program it’s pretty darn significant when golfing competitively.

There are a couple of interesting caveats to this though. The two big ones are that:

Using a ; like this in Python only works with compound statements (if/elif/else, for/in, while, try/except/finally/else, with, and possibly match/case, though I’ve never tried with that one). In particular, this means you can’t use a ; in Python to define a lambda expression with multiple statements in it’s body, or to have multiple statements evaluated as part of a ternary operator.
If there is no newline between the : in a compound statement and the first statement of the block that follows that part of the compound statement, the body of that block will continue until the next newline. This means that in my first example above, the only ways to denote the end of the loop’s body are to either insert a newline after the final statement, or have the end of the file be immediately after the final statement. This is a bit annoying for code golf, but it actually has some interesting uses, such as making it possible to write functions which have only one statement in their body all on one line (which is especially useful when writing type stubs).
> **Comments:**
> **username_10** (2023-10-18): If you're willing to write the block on just 1 line, you're surely also willing to forego PEP-8's recommendation of 4-space indentation. If you change the block indent to just 1 space then you only save 3 bytes. But if you're golfing, I guess every byte counts.  
> **username_11** (2023-10-18): @Barmar or even one tab, which looks nicer  
---
### Answer by username_12 on 2023-10-18. Score: 9
Similar to what abel1502 said about executing code from bash, I find myself using semicolons when using pdb and trying to do anything involving multiple lines. It's a lot easier to deal with for one-off exploration of the state after pdb.set_trace() and doesn't require doing interact
Ex:
import pdb
import random

def function_to_debug():
    result = []
    for a in range(100):
        b = random.randint(1, a)
        result.append((a, b))
        pdb.set_trace()
    # rest of the function

Then when it pauses, I might want to do some sort of exploration of stuff
(pdb) test=[]
(pdb) for i, j in result:print(i);test.append(j)

This isn't exactly a representative example, but a very simplified example of how I might want to mess around with the currently available variables when debugging.
> **Comments:**
> **username_13** (2023-10-18): Could you add an example to how you use it with pdb?
I myself use it a lot like this: `import pdb; pdb.set_trace()`. But your answer makes it sound like you can do more with it? I'm curious about that.  
> **username_12** (2023-10-18): @Opifex a more concrete/useful example doesn't come to mind right this second, but this is a very generic example of where I might want to both print one part of a tuple and save out the second element, maybe to do something else with.  Personally I use `pdb.set_trace()` when exploring less-than-well-documented code I don't understand to see what the contents of variables & such actually are in practice. Hopefully this is of use though  
> **username_13** (2023-10-18): Ahh, I understand now. You use it *inside* of pdb. Not in the python code. That makes sense, thanks!  
---
### Answer by username_5 on 2023-10-20. Score: 1
I used this feature when embedding Python code in an editor for a Jupyter-notebook-like experience. The code is a recipe for baking bread. It is also a template, meant to be loaded into the current context, then lightly edited to change the amounts in the first paragraph:
flour_grams = 1000
whole_wheat_percent = 45
honey_grams = 30
sourdough_starter_percent = 15
starter_is_watery = False

The rest of the script was math and boilerplate. For the code to be embedded nicely in the page, the beginning needed to be written as above, but the rest needed be minified to not take up the whole page vertically. (Eventually I had to use exec(code_str) because standard minification was insufficient.)
This is a niche example (embedding the script in where it must be vertically compact), but I suspect a lot of different niche examples end up justifying the feature.
---
### Answer by username_6 on 2023-10-20. Score: -2
There have always been embedded scripting languages, and although I have never seen that use case, it was certainly a consideration even in the early days of python 2.

Other posters have mentioned bash, snippets, and GUI python, but the class is larger than that. Consider python embedded in HTML: HTML specifically ignores whitespace, and you cannot ensure that whitespace is retained in that environment.

Generally, when you embed python script inside something that is not just a text file in a file system, you have to conform to the constraints of that system.

I've always thought that the semi-colons were just included as a concession to c programmers: that's certainly the way I saw them used 20 years ago. But embedded scripting with the cpython engine was certainly an active consideration then, much more than it is today.
> **Comments:**
> **username_3** (2023-10-21): Semicolons aren't enough to make Python work in an environment where whitespace isn't preserved, because semicolons are only for sequencing, they cannot indicate nesting depth. Consider `if a: if b: foo() else: bar()` ─ you can't use semicolons to disambiguate this.  
> **username_3** (2023-10-21): Also isn't not really accurate to say HTML doesn't preserve whitespace; otherwise the CSS property `white-space: pre;` would be useless. Some transformations of HTML might not preserve whitespace, but those transformations are only correct when the whitespace indeed doesn't matter.  
> **username_6** (2023-10-21): @kaya3, Complex if/else statements can be trivially decomposed into repeated If statements.  HTML is just an example: I'm glad you understood the example.  
> **username_3** (2023-10-21): If you have any reference showing that either the Python designers intended semicolons to be used in environments where newlines weren't preserved, or examples of Python being embedded in HTML without whitespace-preservation in real projects, please add them to your answer. Otherwise as far as I can tell this is just false; I am aware of projects which embed Python in HTML (e.g. [PyScript example](https://pyscript.net/examples/simple_clock.html)) but in reality these rely on HTML *preserving* whitespace.  
---
### Answer by username_7 on 2023-10-19. Score: -3
I've never used this feature in Python, but I am familiar with other languages that support semicolons: Algol, C, Java, etc. When I used place two or more statements on the same line, I was sending anyone reading the code a message: these two statements are tightly bound, and should be considered together. I have got out of the habit of using the idiom, perhaps because I'd tend to bind the two statements together by putting then into a single method of an object.

E.g. x=1;y=1,z=1; // They are coordinates in 3D, so they hang out together.

Compare: p = Point(x=1,y=1,z=1)
> **Comments:**
> **username_8** (2023-10-20): In Python I'd write `x,y,z = 1,1,1` to convey that they are bound at the same time, no need for a semicolon  
---
