Why Is Semicolon Allowed in This Python Snippet

Why is semicolon allowed in this Python snippet?

Python does not require semicolons to terminate statements. Semicolons can be used to delimit statements if you wish to put multiple statements on the same line.

Now, why is this allowed? It's a simple design decision. I don't think Python needs this semicolon thing, but somebody thought it would be nice to have and added it to the language.

Why is a semicolon working in Python and does not result in an error?

First of all, don’t use semicolons to end a statement in Python!

But look here to see why it’s allowed: Why is semicolon allowed in this Python snippet?

Or here: What does a semicolon do?

They are used to put multiple statements on a single line, so the interpreter is just ignoring it since there is not a second statement.

What does a semicolon do?

The semicolon does nothing in the code you show.

I suspect this is someone who programs in another language (C, Java, ...) that requires semicolons at the end of statements and it's just a habit (happens to me sometimes too).

If you want to put several Python statements on the same line, you can use a semi-colon to separate them, see this Python Doc:

A suite is a group of statements controlled by a clause. A suite can
be one or more semicolon-separated simple statements on the same line
as the header, following the header’s colon, or it can be one or more
indented statements on subsequent lines

When is semicolon use in Python considered good or acceptable?

PEP 8 is the official style guide and says:

Compound statements (multiple statements on the same line) are generally discouraged.

(See also the examples just after this in the PEP.)

While I don't agree with everything PEP 8 says, if you're looking for an authoritative source, that's it. You should use multi-statement lines only as a last resort. (python -c is a good example of such a last resort, because you have no way to use actual linebreaks in that case.)

What does the ' ; ' symbol mean in python?

Semi-colons are used in Python to separate statements in the same line.

print 1; print 2

Common use example:

import pdb; pdb.set_trace()

Is Using Semi-Colons in python looked down upon?

It's normally discouraged as it detracts from readability, from PEP8:

Compound statements (multiple statements on the same line) are
generally discouraged.

Yes:

if foo == 'blah':
do_blah_thing()
do_one()
do_two()
do_three()

Rather not:

if foo == 'blah': do_blah_thing()
do_one(); do_two(); do_three()

While sometimes it's okay to put an if/for/while with a small body on
the same line, never do this for multi-clause statements. Also avoid
folding such long lines!

Rather not:

if foo == 'blah': do_blah_thing()
for x in lst: total += x
while t < 10: t = delay()

Definitely not:

if foo == 'blah': do_blah_thing()
else: do_non_blah_thing()

try: something()
finally: cleanup()

do_one(); do_two(); do_three(long, argument,
list, like, this)

if foo == 'blah': one(); two(); three()

what is the use of ';' in python. I don't see a document that explains this

As your question states as, you want a document:

Here is Compound statements:

A suite can be one or more semicolon-separated simple statements on the same line as the header, following the header’s colon, or it can be one or more indented statements on subsequent lines.

What's the difference with Python statements ending with ;?

There is really no difference. Python ends a line of code at the end of the logical line, or when it encounters ;

The only advantage to using ; is that you can stack multiple logical lines into one physical line. For example (in python3):

import sys

for i in range(10):
print(i, end=' '); sys.stdout.flush()

That said, this is terrible coding style, so don't ever do it

Why does break not need a semicolon when ending a loop?

A shorter example:

let _: i32 = loop {
if true {
break 3; // ()
}
};

That is just another example where the semi-colon does not interfere with the intended outcome. For one, the insertion of a semi-colon introduces an expression statement, which evaluates to the unit type (). As the loops and if expressions continue to admit a code block evaluating to the same type (), then all types are in conformance.

let _: i32 = loop {
if true {
break 3 // !
}
};

If the semi-colon is taken away, the break is evaluated to the never type !, which coerces to any other type. This means that it will fulfill any type expected by the outer scope. So all is well all the same, so long as you don't try to append any other statement before the end of the if block.

Both break and return evaluate to !, as their side effects imply that the program will not go through with the natural workflow.

See also:

  • Why do return expressions use semicolons when they're unnecessary?
  • What's the difference between using the return statement and omitting the semicolon in Rust?
  • How to statically assert the end of a function is unreachable


Related Topics



Leave a reply



Submit