Why is semicolon allowed in this python snippet?












128














Python does not warrant the use of semicolons to end statements.
So why is this (below) allowed?



import pdb; pdb.set_trace()









share|improve this question






















  • Note that such code is blatantly for debugging purposes only and would presumably be excised before it's "done". I use that snippet just as you have it so I can easily move it around.
    – Nick T
    Mar 3 '15 at 4:10
















128














Python does not warrant the use of semicolons to end statements.
So why is this (below) allowed?



import pdb; pdb.set_trace()









share|improve this question






















  • Note that such code is blatantly for debugging purposes only and would presumably be excised before it's "done". I use that snippet just as you have it so I can easily move it around.
    – Nick T
    Mar 3 '15 at 4:10














128












128








128


26





Python does not warrant the use of semicolons to end statements.
So why is this (below) allowed?



import pdb; pdb.set_trace()









share|improve this question













Python does not warrant the use of semicolons to end statements.
So why is this (below) allowed?



import pdb; pdb.set_trace()






python






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '11 at 1:48









canadadry

2,92243152




2,92243152












  • Note that such code is blatantly for debugging purposes only and would presumably be excised before it's "done". I use that snippet just as you have it so I can easily move it around.
    – Nick T
    Mar 3 '15 at 4:10


















  • Note that such code is blatantly for debugging purposes only and would presumably be excised before it's "done". I use that snippet just as you have it so I can easily move it around.
    – Nick T
    Mar 3 '15 at 4:10
















Note that such code is blatantly for debugging purposes only and would presumably be excised before it's "done". I use that snippet just as you have it so I can easily move it around.
– Nick T
Mar 3 '15 at 4:10




Note that such code is blatantly for debugging purposes only and would presumably be excised before it's "done". I use that snippet just as you have it so I can easily move it around.
– Nick T
Mar 3 '15 at 4:10












12 Answers
12






active

oldest

votes


















173














Python does not require semi-colons to terminate statements. Semi colons 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 semi-colon thing, but somebody thought it would be nice to have and added it to the language.






share|improve this answer

















  • 10




    It's useful for things like timeit a = 5; a*a
    – endolith
    May 23 '13 at 19:00






  • 8




    It seems useful for exec statements e.g. exec('for a in [1,2,3]:print a;print a+1')
    – Phil C
    Jun 30 '13 at 8:28








  • 52




    I've come to Python with a background of C, Obj-C, Javascript, PHP. I frequently put a (useless) terminator ; at the end of a line. But fortunately Python forgives me
    – Paolo
    Sep 28 '13 at 13:31






  • 30




    The semicolon is highly useful in the django shell, or the debugger, so you can write a small program on a single line and repeat it later.
    – Bryce
    Oct 2 '13 at 5:48






  • 3




    @endolith What does timeit mean? a = 2; a*a is useless since a is still 2; it would have to be a = 2; a *= a
    – Nearoo
    May 10 '17 at 2:02



















49














http://docs.python.org/reference/compound_stmts.html




Compound statements consist of one or more ‘clauses.’ A clause
consists of a header and a ‘suite.’ The clause headers of a particular
compound statement are all at the same indentation level. Each clause
header begins with a uniquely identifying keyword and ends with a
colon. 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
. Only the latter
form of suite can contain nested compound statements; the following is
illegal, mostly because it wouldn’t be clear to which if clause a
following else clause would belong:



if test1: if test2: print x


Also note that the semicolon binds tighter than the colon in this
context, so that in the following example, either all or none of the
print statements are executed:



if x < y < z: print x; print y; print z 



Summarizing:



compound_stmt ::=  if_stmt
| while_stmt
| for_stmt
| try_stmt
| with_stmt
| funcdef
| classdef
| decorated
suite ::= stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement ::= stmt_list NEWLINE | compound_stmt
stmt_list ::= simple_stmt (";" simple_stmt)* [";"]





share|improve this answer

















  • 4




    Didn't know about the priority of semicolon being less than colon after an if. Thank's!
    – gaborous
    Oct 13 '14 at 22:54






  • 2




    @gaborous I personally feel your comment ambiguous enough to make me thought you would mean the other way. So, to clarify, I would rather suggest other readers to stick with the official phrase: in that if cond: stmt1; stmt2; stmt3 example, either all or none of the statements are executed".
    – RayLuo
    Sep 30 '16 at 21:00












  • Yep that's what I meant, if the priority is lesser, then the lexer cannot leave the parent (colon) block until all semicolon statements are executed. That's really not an obvious thing.
    – gaborous
    Oct 2 '16 at 11:50










  • I think what you explained and meant was perfectly clear.
    – eSurfsnake
    Jul 28 '18 at 5:40



















33














Python uses the ; as a separator, not a terminator. You can also use them at the end of a line, which makes them look like a statement terminator, but this is legal only because blank statements are legal in Python -- a line that contains a semicolon at the end is two statements, the second one blank.






share|improve this answer

















  • 24




    If you put double semicolons at the end (or anywhere), you get a SyntaxError. So it seems blank statements are not entirely legal.
    – Cucu
    Apr 18 '13 at 11:07






  • 7




    Probably it has the same logic as lists, tuples, dicts with trailing commas are valid and the same as their trimmed counterparts (e.g. [1,2,3]==[1,2,3,]) but they cannot contain double commas. So basically Python has a "trailing blank remover". Which by the way is clear from this: stmt_list ::= simple_stmt (";" simple_stmt)* [";"]
    – Cucu
    Apr 19 '13 at 14:14





















21














Semicolon in the interpreter



Having read the answers, I still miss one important aspect of using semicolons, possibly the only one where it really makes a difference...



When you're working in an interpreter REPL (the Python interactive shell, IDLE, ipython) the value of the last expression is printed to the screen and usually this is the intended behavior.



Using an expression for side effects



But in some cases you want to evaluate an expression for its side effects only, e.g., to see the results of your simulation plotted by matplotlib.



In this cases you (probably) don't want to see the screenful of reprs of matplotlib objects that are sometimes returned by a call to a matplotlib function and one of the possibilities you have is to append a semicolon to the overly verbose statement, that immediately is composed by two expressions, the matplotlib invocation and a null statement, so that the value of the compound expression is None and nothing is printed to the screen by the interpreter
(the other possibility being assignment, as in _ = plot(...) but I find that a bit more intrusive).



Personal remark



IMHO, the use of the semicolon to suppress not desired output in the interpreter has become more relevant following the introduction of the IPyton notebook, that permits to save the input and the output, including graphical output, of an interpreter session for documentation and eventual reuse.






share|improve this answer





















  • I see, thank you for sharing! This just solved exactly my problem, where semicolon can prevent repr by matplotlib in ipython notebook
    – Napitupulu Jon
    Mar 17 '15 at 9:12



















10














As everyone else has noted, you can use semicolons to separate statements. You don't have to, and it's not the usual style.



As for why this is useful, some people like to put two or more really trivial short statements on a single line (personally I think this turns several trivial easily skimmed lines into one complex-looking line and makes it harder to see that it's trivial).



But it's almost a requirement when you're invoking Python one liners from the shell using python -c '<some python code>'. Here you can't use indentation to separate statements, so if your one-liner is really a two-liner, you'll need to use a semicolon. And if you want to use other arguments in your one-liner, you'll have to import sys to get at sys.argv, which requires a separate import statement. e.g.



python -c "import sys; print ' '.join(sorted(sys.argv[1:]))" 5 2 3 1 4
1 2 3 4 5





share|improve this answer

















  • 1




    You can use indentation and newlines when passing commands through python in the shell: Just span your quoted code over multiple lines or use a heredoc. Still, that fails if you want it to be a "one-liner". ;)
    – 00dani
    Jan 30 '14 at 1:26










  • includes traits for the rail answer with -c
    – n611x007
    Jul 6 '15 at 12:24





















5














A quote from "When Pythons Attack"




Don't terminate all of your statements with a semicolon. It's technically legal to do this in Python, but is totally useless unless you're placing more than one statement on a single line (e.g., x=1; y=2; z=3).







share|improve this answer

















  • 3




    Why not x,y,z = 1,2,3?
    – Anirban Nag 'tintinmj'
    Feb 17 '14 at 19:48






  • 2




    @AnirbanNag'tintinmj' In this particular case, I believe x, y, z = 1, 2, 3 is slower because it incurs an unnecessary round trip of building a tuple and then immediately desembles it. (By the way, I don't really think @communistpancake is suggesting the x=1; y=2; z=3 pattern though. I think he/she is merely giving an example for how semicolon is a separator rather than a terminator.)
    – RayLuo
    Sep 30 '16 at 21:12






  • 1




    @AnirbanNag - because that would have been a rubbish example of a multi-statement line.
    – c z
    Mar 2 '18 at 12:11



















3














Multiple statements on one line may include semicolons as separators. For example: http://docs.python.org/reference/compound_stmts.html In your case, it makes for an easy insertion of a point to break into the debugger.



Also, as mentioned by Mark Lutz in the Learning Python Book, it is technically legal (although unnecessary and annoying) to terminate all your statements with semicolons.






share|improve this answer





























    3














    Semicolons are part of valid syntax: 8. Compound statements (The Python Language Reference)






    share|improve this answer































      2














      Python does let you use a semi-colon to denote the end of a statement if you are including more than one statement on a line.






      share|improve this answer





























        2














        Semicolons can be used to one line two or more commands. They don't have to be used, but they aren't restricted.




        The semicolon ( ; ) allows multiple statements on the single line given that neither statement starts a new code block.




        http://www.tutorialspoint.com/python/python_basic_syntax.htm






        share|improve this answer





























          2














          Semicolons (like dots, commas and parentheses) tend to cause religious wars. Still, they (or some similar symbol) are useful in any programming language for various reasons.




          • Practical: the ability to put several short commands that belong conceptually together on the same line. A program text that looks like a narrow snake has the opposite effect of what is intended by newlines and indentation, which is highlighting structure.


          • Conceptual: separation of concerns between pure syntax (in this case, for a sequence of commands) from presentation (e.g. newline), in the old days called "pretty-printing".



          Observation: for highlighting structure, indentation could be augmented/replaced by vertical lines in the obvious way, serving as a "visual ruler" to see where an indentation begins and ends. Different colors (e.g. following the color code for resistors) may compensate for crowding.






          share|improve this answer





























            0














            I realize I am biased as an old C programmer, but there are times when the various Python conventions make things hard to follow. I find the indent convention a bit of an annoyance at times.



            Sometimes, clarity of when a statement or block ends is very useful. Standard C code will often read something like this:



            for(i=0; i<100; i++) {
            do something here;
            do another thing here;
            }

            continue doing things;


            where you use the whitespace for a lot of clarity - and it is easy to see where the loop ends.



            Python does let you terminate with an (optional) semicolon. As noted above, that does NOT mean that there is a statement to execute followed by a 'null' statement. SO, for example,



            print(x);
            print(y);


            Is the same as



            print(x)
            print(y)


            If you believe that the first one has a null statement at the end of each line, try - as suggested - doing this:



            print(x);;


            It will throw a syntax error.



            Personally, I find the semicolon to make code more readable when you have lots of nesting and functions with many arguments and/or long-named args. So, to my eye, this is a lot clearer than other choices:



            if some_boolean_is_true:
            call_function(
            long_named_arg_1,
            long_named_arg_2,
            long_named_arg_3,
            long_named_arg_4
            );


            since, to me, it lets you know that last ')' ends some 'block' that ran over many lines.



            I personally think there is much to much made of PEP style guidelines, IDEs that enforce them, and the belief there is 'only one Pythonic way to do things'. If you believe the latter, go look at how to format numbers: as of now, Python supports four different ways to do it.



            I am sure I will be flamed by some diehards, but the compiler/interpreter doesn't care if the arguments have long or short names, and - but for the indentation convention in Python - doesn't care about whitespace. The biggest problem with code is giving clarity to another human (and even yourself after months of work) to understand what is going on, where things start and end, etc.






            share|improve this answer





















              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f8236380%2fwhy-is-semicolon-allowed-in-this-python-snippet%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              12 Answers
              12






              active

              oldest

              votes








              12 Answers
              12






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              173














              Python does not require semi-colons to terminate statements. Semi colons 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 semi-colon thing, but somebody thought it would be nice to have and added it to the language.






              share|improve this answer

















              • 10




                It's useful for things like timeit a = 5; a*a
                – endolith
                May 23 '13 at 19:00






              • 8




                It seems useful for exec statements e.g. exec('for a in [1,2,3]:print a;print a+1')
                – Phil C
                Jun 30 '13 at 8:28








              • 52




                I've come to Python with a background of C, Obj-C, Javascript, PHP. I frequently put a (useless) terminator ; at the end of a line. But fortunately Python forgives me
                – Paolo
                Sep 28 '13 at 13:31






              • 30




                The semicolon is highly useful in the django shell, or the debugger, so you can write a small program on a single line and repeat it later.
                – Bryce
                Oct 2 '13 at 5:48






              • 3




                @endolith What does timeit mean? a = 2; a*a is useless since a is still 2; it would have to be a = 2; a *= a
                – Nearoo
                May 10 '17 at 2:02
















              173














              Python does not require semi-colons to terminate statements. Semi colons 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 semi-colon thing, but somebody thought it would be nice to have and added it to the language.






              share|improve this answer

















              • 10




                It's useful for things like timeit a = 5; a*a
                – endolith
                May 23 '13 at 19:00






              • 8




                It seems useful for exec statements e.g. exec('for a in [1,2,3]:print a;print a+1')
                – Phil C
                Jun 30 '13 at 8:28








              • 52




                I've come to Python with a background of C, Obj-C, Javascript, PHP. I frequently put a (useless) terminator ; at the end of a line. But fortunately Python forgives me
                – Paolo
                Sep 28 '13 at 13:31






              • 30




                The semicolon is highly useful in the django shell, or the debugger, so you can write a small program on a single line and repeat it later.
                – Bryce
                Oct 2 '13 at 5:48






              • 3




                @endolith What does timeit mean? a = 2; a*a is useless since a is still 2; it would have to be a = 2; a *= a
                – Nearoo
                May 10 '17 at 2:02














              173












              173








              173






              Python does not require semi-colons to terminate statements. Semi colons 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 semi-colon thing, but somebody thought it would be nice to have and added it to the language.






              share|improve this answer












              Python does not require semi-colons to terminate statements. Semi colons 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 semi-colon thing, but somebody thought it would be nice to have and added it to the language.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 23 '11 at 1:52









              André Caron

              34k848108




              34k848108








              • 10




                It's useful for things like timeit a = 5; a*a
                – endolith
                May 23 '13 at 19:00






              • 8




                It seems useful for exec statements e.g. exec('for a in [1,2,3]:print a;print a+1')
                – Phil C
                Jun 30 '13 at 8:28








              • 52




                I've come to Python with a background of C, Obj-C, Javascript, PHP. I frequently put a (useless) terminator ; at the end of a line. But fortunately Python forgives me
                – Paolo
                Sep 28 '13 at 13:31






              • 30




                The semicolon is highly useful in the django shell, or the debugger, so you can write a small program on a single line and repeat it later.
                – Bryce
                Oct 2 '13 at 5:48






              • 3




                @endolith What does timeit mean? a = 2; a*a is useless since a is still 2; it would have to be a = 2; a *= a
                – Nearoo
                May 10 '17 at 2:02














              • 10




                It's useful for things like timeit a = 5; a*a
                – endolith
                May 23 '13 at 19:00






              • 8




                It seems useful for exec statements e.g. exec('for a in [1,2,3]:print a;print a+1')
                – Phil C
                Jun 30 '13 at 8:28








              • 52




                I've come to Python with a background of C, Obj-C, Javascript, PHP. I frequently put a (useless) terminator ; at the end of a line. But fortunately Python forgives me
                – Paolo
                Sep 28 '13 at 13:31






              • 30




                The semicolon is highly useful in the django shell, or the debugger, so you can write a small program on a single line and repeat it later.
                – Bryce
                Oct 2 '13 at 5:48






              • 3




                @endolith What does timeit mean? a = 2; a*a is useless since a is still 2; it would have to be a = 2; a *= a
                – Nearoo
                May 10 '17 at 2:02








              10




              10




              It's useful for things like timeit a = 5; a*a
              – endolith
              May 23 '13 at 19:00




              It's useful for things like timeit a = 5; a*a
              – endolith
              May 23 '13 at 19:00




              8




              8




              It seems useful for exec statements e.g. exec('for a in [1,2,3]:print a;print a+1')
              – Phil C
              Jun 30 '13 at 8:28






              It seems useful for exec statements e.g. exec('for a in [1,2,3]:print a;print a+1')
              – Phil C
              Jun 30 '13 at 8:28






              52




              52




              I've come to Python with a background of C, Obj-C, Javascript, PHP. I frequently put a (useless) terminator ; at the end of a line. But fortunately Python forgives me
              – Paolo
              Sep 28 '13 at 13:31




              I've come to Python with a background of C, Obj-C, Javascript, PHP. I frequently put a (useless) terminator ; at the end of a line. But fortunately Python forgives me
              – Paolo
              Sep 28 '13 at 13:31




              30




              30




              The semicolon is highly useful in the django shell, or the debugger, so you can write a small program on a single line and repeat it later.
              – Bryce
              Oct 2 '13 at 5:48




              The semicolon is highly useful in the django shell, or the debugger, so you can write a small program on a single line and repeat it later.
              – Bryce
              Oct 2 '13 at 5:48




              3




              3




              @endolith What does timeit mean? a = 2; a*a is useless since a is still 2; it would have to be a = 2; a *= a
              – Nearoo
              May 10 '17 at 2:02




              @endolith What does timeit mean? a = 2; a*a is useless since a is still 2; it would have to be a = 2; a *= a
              – Nearoo
              May 10 '17 at 2:02













              49














              http://docs.python.org/reference/compound_stmts.html




              Compound statements consist of one or more ‘clauses.’ A clause
              consists of a header and a ‘suite.’ The clause headers of a particular
              compound statement are all at the same indentation level. Each clause
              header begins with a uniquely identifying keyword and ends with a
              colon. 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
              . Only the latter
              form of suite can contain nested compound statements; the following is
              illegal, mostly because it wouldn’t be clear to which if clause a
              following else clause would belong:



              if test1: if test2: print x


              Also note that the semicolon binds tighter than the colon in this
              context, so that in the following example, either all or none of the
              print statements are executed:



              if x < y < z: print x; print y; print z 



              Summarizing:



              compound_stmt ::=  if_stmt
              | while_stmt
              | for_stmt
              | try_stmt
              | with_stmt
              | funcdef
              | classdef
              | decorated
              suite ::= stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
              statement ::= stmt_list NEWLINE | compound_stmt
              stmt_list ::= simple_stmt (";" simple_stmt)* [";"]





              share|improve this answer

















              • 4




                Didn't know about the priority of semicolon being less than colon after an if. Thank's!
                – gaborous
                Oct 13 '14 at 22:54






              • 2




                @gaborous I personally feel your comment ambiguous enough to make me thought you would mean the other way. So, to clarify, I would rather suggest other readers to stick with the official phrase: in that if cond: stmt1; stmt2; stmt3 example, either all or none of the statements are executed".
                – RayLuo
                Sep 30 '16 at 21:00












              • Yep that's what I meant, if the priority is lesser, then the lexer cannot leave the parent (colon) block until all semicolon statements are executed. That's really not an obvious thing.
                – gaborous
                Oct 2 '16 at 11:50










              • I think what you explained and meant was perfectly clear.
                – eSurfsnake
                Jul 28 '18 at 5:40
















              49














              http://docs.python.org/reference/compound_stmts.html




              Compound statements consist of one or more ‘clauses.’ A clause
              consists of a header and a ‘suite.’ The clause headers of a particular
              compound statement are all at the same indentation level. Each clause
              header begins with a uniquely identifying keyword and ends with a
              colon. 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
              . Only the latter
              form of suite can contain nested compound statements; the following is
              illegal, mostly because it wouldn’t be clear to which if clause a
              following else clause would belong:



              if test1: if test2: print x


              Also note that the semicolon binds tighter than the colon in this
              context, so that in the following example, either all or none of the
              print statements are executed:



              if x < y < z: print x; print y; print z 



              Summarizing:



              compound_stmt ::=  if_stmt
              | while_stmt
              | for_stmt
              | try_stmt
              | with_stmt
              | funcdef
              | classdef
              | decorated
              suite ::= stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
              statement ::= stmt_list NEWLINE | compound_stmt
              stmt_list ::= simple_stmt (";" simple_stmt)* [";"]





              share|improve this answer

















              • 4




                Didn't know about the priority of semicolon being less than colon after an if. Thank's!
                – gaborous
                Oct 13 '14 at 22:54






              • 2




                @gaborous I personally feel your comment ambiguous enough to make me thought you would mean the other way. So, to clarify, I would rather suggest other readers to stick with the official phrase: in that if cond: stmt1; stmt2; stmt3 example, either all or none of the statements are executed".
                – RayLuo
                Sep 30 '16 at 21:00












              • Yep that's what I meant, if the priority is lesser, then the lexer cannot leave the parent (colon) block until all semicolon statements are executed. That's really not an obvious thing.
                – gaborous
                Oct 2 '16 at 11:50










              • I think what you explained and meant was perfectly clear.
                – eSurfsnake
                Jul 28 '18 at 5:40














              49












              49








              49






              http://docs.python.org/reference/compound_stmts.html




              Compound statements consist of one or more ‘clauses.’ A clause
              consists of a header and a ‘suite.’ The clause headers of a particular
              compound statement are all at the same indentation level. Each clause
              header begins with a uniquely identifying keyword and ends with a
              colon. 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
              . Only the latter
              form of suite can contain nested compound statements; the following is
              illegal, mostly because it wouldn’t be clear to which if clause a
              following else clause would belong:



              if test1: if test2: print x


              Also note that the semicolon binds tighter than the colon in this
              context, so that in the following example, either all or none of the
              print statements are executed:



              if x < y < z: print x; print y; print z 



              Summarizing:



              compound_stmt ::=  if_stmt
              | while_stmt
              | for_stmt
              | try_stmt
              | with_stmt
              | funcdef
              | classdef
              | decorated
              suite ::= stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
              statement ::= stmt_list NEWLINE | compound_stmt
              stmt_list ::= simple_stmt (";" simple_stmt)* [";"]





              share|improve this answer












              http://docs.python.org/reference/compound_stmts.html




              Compound statements consist of one or more ‘clauses.’ A clause
              consists of a header and a ‘suite.’ The clause headers of a particular
              compound statement are all at the same indentation level. Each clause
              header begins with a uniquely identifying keyword and ends with a
              colon. 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
              . Only the latter
              form of suite can contain nested compound statements; the following is
              illegal, mostly because it wouldn’t be clear to which if clause a
              following else clause would belong:



              if test1: if test2: print x


              Also note that the semicolon binds tighter than the colon in this
              context, so that in the following example, either all or none of the
              print statements are executed:



              if x < y < z: print x; print y; print z 



              Summarizing:



              compound_stmt ::=  if_stmt
              | while_stmt
              | for_stmt
              | try_stmt
              | with_stmt
              | funcdef
              | classdef
              | decorated
              suite ::= stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
              statement ::= stmt_list NEWLINE | compound_stmt
              stmt_list ::= simple_stmt (";" simple_stmt)* [";"]






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 23 '11 at 1:53









              chown

              41.3k16115158




              41.3k16115158








              • 4




                Didn't know about the priority of semicolon being less than colon after an if. Thank's!
                – gaborous
                Oct 13 '14 at 22:54






              • 2




                @gaborous I personally feel your comment ambiguous enough to make me thought you would mean the other way. So, to clarify, I would rather suggest other readers to stick with the official phrase: in that if cond: stmt1; stmt2; stmt3 example, either all or none of the statements are executed".
                – RayLuo
                Sep 30 '16 at 21:00












              • Yep that's what I meant, if the priority is lesser, then the lexer cannot leave the parent (colon) block until all semicolon statements are executed. That's really not an obvious thing.
                – gaborous
                Oct 2 '16 at 11:50










              • I think what you explained and meant was perfectly clear.
                – eSurfsnake
                Jul 28 '18 at 5:40














              • 4




                Didn't know about the priority of semicolon being less than colon after an if. Thank's!
                – gaborous
                Oct 13 '14 at 22:54






              • 2




                @gaborous I personally feel your comment ambiguous enough to make me thought you would mean the other way. So, to clarify, I would rather suggest other readers to stick with the official phrase: in that if cond: stmt1; stmt2; stmt3 example, either all or none of the statements are executed".
                – RayLuo
                Sep 30 '16 at 21:00












              • Yep that's what I meant, if the priority is lesser, then the lexer cannot leave the parent (colon) block until all semicolon statements are executed. That's really not an obvious thing.
                – gaborous
                Oct 2 '16 at 11:50










              • I think what you explained and meant was perfectly clear.
                – eSurfsnake
                Jul 28 '18 at 5:40








              4




              4




              Didn't know about the priority of semicolon being less than colon after an if. Thank's!
              – gaborous
              Oct 13 '14 at 22:54




              Didn't know about the priority of semicolon being less than colon after an if. Thank's!
              – gaborous
              Oct 13 '14 at 22:54




              2




              2




              @gaborous I personally feel your comment ambiguous enough to make me thought you would mean the other way. So, to clarify, I would rather suggest other readers to stick with the official phrase: in that if cond: stmt1; stmt2; stmt3 example, either all or none of the statements are executed".
              – RayLuo
              Sep 30 '16 at 21:00






              @gaborous I personally feel your comment ambiguous enough to make me thought you would mean the other way. So, to clarify, I would rather suggest other readers to stick with the official phrase: in that if cond: stmt1; stmt2; stmt3 example, either all or none of the statements are executed".
              – RayLuo
              Sep 30 '16 at 21:00














              Yep that's what I meant, if the priority is lesser, then the lexer cannot leave the parent (colon) block until all semicolon statements are executed. That's really not an obvious thing.
              – gaborous
              Oct 2 '16 at 11:50




              Yep that's what I meant, if the priority is lesser, then the lexer cannot leave the parent (colon) block until all semicolon statements are executed. That's really not an obvious thing.
              – gaborous
              Oct 2 '16 at 11:50












              I think what you explained and meant was perfectly clear.
              – eSurfsnake
              Jul 28 '18 at 5:40




              I think what you explained and meant was perfectly clear.
              – eSurfsnake
              Jul 28 '18 at 5:40











              33














              Python uses the ; as a separator, not a terminator. You can also use them at the end of a line, which makes them look like a statement terminator, but this is legal only because blank statements are legal in Python -- a line that contains a semicolon at the end is two statements, the second one blank.






              share|improve this answer

















              • 24




                If you put double semicolons at the end (or anywhere), you get a SyntaxError. So it seems blank statements are not entirely legal.
                – Cucu
                Apr 18 '13 at 11:07






              • 7




                Probably it has the same logic as lists, tuples, dicts with trailing commas are valid and the same as their trimmed counterparts (e.g. [1,2,3]==[1,2,3,]) but they cannot contain double commas. So basically Python has a "trailing blank remover". Which by the way is clear from this: stmt_list ::= simple_stmt (";" simple_stmt)* [";"]
                – Cucu
                Apr 19 '13 at 14:14


















              33














              Python uses the ; as a separator, not a terminator. You can also use them at the end of a line, which makes them look like a statement terminator, but this is legal only because blank statements are legal in Python -- a line that contains a semicolon at the end is two statements, the second one blank.






              share|improve this answer

















              • 24




                If you put double semicolons at the end (or anywhere), you get a SyntaxError. So it seems blank statements are not entirely legal.
                – Cucu
                Apr 18 '13 at 11:07






              • 7




                Probably it has the same logic as lists, tuples, dicts with trailing commas are valid and the same as their trimmed counterparts (e.g. [1,2,3]==[1,2,3,]) but they cannot contain double commas. So basically Python has a "trailing blank remover". Which by the way is clear from this: stmt_list ::= simple_stmt (";" simple_stmt)* [";"]
                – Cucu
                Apr 19 '13 at 14:14
















              33












              33








              33






              Python uses the ; as a separator, not a terminator. You can also use them at the end of a line, which makes them look like a statement terminator, but this is legal only because blank statements are legal in Python -- a line that contains a semicolon at the end is two statements, the second one blank.






              share|improve this answer












              Python uses the ; as a separator, not a terminator. You can also use them at the end of a line, which makes them look like a statement terminator, but this is legal only because blank statements are legal in Python -- a line that contains a semicolon at the end is two statements, the second one blank.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 23 '11 at 16:23









              kindall

              127k17192241




              127k17192241








              • 24




                If you put double semicolons at the end (or anywhere), you get a SyntaxError. So it seems blank statements are not entirely legal.
                – Cucu
                Apr 18 '13 at 11:07






              • 7




                Probably it has the same logic as lists, tuples, dicts with trailing commas are valid and the same as their trimmed counterparts (e.g. [1,2,3]==[1,2,3,]) but they cannot contain double commas. So basically Python has a "trailing blank remover". Which by the way is clear from this: stmt_list ::= simple_stmt (";" simple_stmt)* [";"]
                – Cucu
                Apr 19 '13 at 14:14
















              • 24




                If you put double semicolons at the end (or anywhere), you get a SyntaxError. So it seems blank statements are not entirely legal.
                – Cucu
                Apr 18 '13 at 11:07






              • 7




                Probably it has the same logic as lists, tuples, dicts with trailing commas are valid and the same as their trimmed counterparts (e.g. [1,2,3]==[1,2,3,]) but they cannot contain double commas. So basically Python has a "trailing blank remover". Which by the way is clear from this: stmt_list ::= simple_stmt (";" simple_stmt)* [";"]
                – Cucu
                Apr 19 '13 at 14:14










              24




              24




              If you put double semicolons at the end (or anywhere), you get a SyntaxError. So it seems blank statements are not entirely legal.
              – Cucu
              Apr 18 '13 at 11:07




              If you put double semicolons at the end (or anywhere), you get a SyntaxError. So it seems blank statements are not entirely legal.
              – Cucu
              Apr 18 '13 at 11:07




              7




              7




              Probably it has the same logic as lists, tuples, dicts with trailing commas are valid and the same as their trimmed counterparts (e.g. [1,2,3]==[1,2,3,]) but they cannot contain double commas. So basically Python has a "trailing blank remover". Which by the way is clear from this: stmt_list ::= simple_stmt (";" simple_stmt)* [";"]
              – Cucu
              Apr 19 '13 at 14:14






              Probably it has the same logic as lists, tuples, dicts with trailing commas are valid and the same as their trimmed counterparts (e.g. [1,2,3]==[1,2,3,]) but they cannot contain double commas. So basically Python has a "trailing blank remover". Which by the way is clear from this: stmt_list ::= simple_stmt (";" simple_stmt)* [";"]
              – Cucu
              Apr 19 '13 at 14:14













              21














              Semicolon in the interpreter



              Having read the answers, I still miss one important aspect of using semicolons, possibly the only one where it really makes a difference...



              When you're working in an interpreter REPL (the Python interactive shell, IDLE, ipython) the value of the last expression is printed to the screen and usually this is the intended behavior.



              Using an expression for side effects



              But in some cases you want to evaluate an expression for its side effects only, e.g., to see the results of your simulation plotted by matplotlib.



              In this cases you (probably) don't want to see the screenful of reprs of matplotlib objects that are sometimes returned by a call to a matplotlib function and one of the possibilities you have is to append a semicolon to the overly verbose statement, that immediately is composed by two expressions, the matplotlib invocation and a null statement, so that the value of the compound expression is None and nothing is printed to the screen by the interpreter
              (the other possibility being assignment, as in _ = plot(...) but I find that a bit more intrusive).



              Personal remark



              IMHO, the use of the semicolon to suppress not desired output in the interpreter has become more relevant following the introduction of the IPyton notebook, that permits to save the input and the output, including graphical output, of an interpreter session for documentation and eventual reuse.






              share|improve this answer





















              • I see, thank you for sharing! This just solved exactly my problem, where semicolon can prevent repr by matplotlib in ipython notebook
                – Napitupulu Jon
                Mar 17 '15 at 9:12
















              21














              Semicolon in the interpreter



              Having read the answers, I still miss one important aspect of using semicolons, possibly the only one where it really makes a difference...



              When you're working in an interpreter REPL (the Python interactive shell, IDLE, ipython) the value of the last expression is printed to the screen and usually this is the intended behavior.



              Using an expression for side effects



              But in some cases you want to evaluate an expression for its side effects only, e.g., to see the results of your simulation plotted by matplotlib.



              In this cases you (probably) don't want to see the screenful of reprs of matplotlib objects that are sometimes returned by a call to a matplotlib function and one of the possibilities you have is to append a semicolon to the overly verbose statement, that immediately is composed by two expressions, the matplotlib invocation and a null statement, so that the value of the compound expression is None and nothing is printed to the screen by the interpreter
              (the other possibility being assignment, as in _ = plot(...) but I find that a bit more intrusive).



              Personal remark



              IMHO, the use of the semicolon to suppress not desired output in the interpreter has become more relevant following the introduction of the IPyton notebook, that permits to save the input and the output, including graphical output, of an interpreter session for documentation and eventual reuse.






              share|improve this answer





















              • I see, thank you for sharing! This just solved exactly my problem, where semicolon can prevent repr by matplotlib in ipython notebook
                – Napitupulu Jon
                Mar 17 '15 at 9:12














              21












              21








              21






              Semicolon in the interpreter



              Having read the answers, I still miss one important aspect of using semicolons, possibly the only one where it really makes a difference...



              When you're working in an interpreter REPL (the Python interactive shell, IDLE, ipython) the value of the last expression is printed to the screen and usually this is the intended behavior.



              Using an expression for side effects



              But in some cases you want to evaluate an expression for its side effects only, e.g., to see the results of your simulation plotted by matplotlib.



              In this cases you (probably) don't want to see the screenful of reprs of matplotlib objects that are sometimes returned by a call to a matplotlib function and one of the possibilities you have is to append a semicolon to the overly verbose statement, that immediately is composed by two expressions, the matplotlib invocation and a null statement, so that the value of the compound expression is None and nothing is printed to the screen by the interpreter
              (the other possibility being assignment, as in _ = plot(...) but I find that a bit more intrusive).



              Personal remark



              IMHO, the use of the semicolon to suppress not desired output in the interpreter has become more relevant following the introduction of the IPyton notebook, that permits to save the input and the output, including graphical output, of an interpreter session for documentation and eventual reuse.






              share|improve this answer












              Semicolon in the interpreter



              Having read the answers, I still miss one important aspect of using semicolons, possibly the only one where it really makes a difference...



              When you're working in an interpreter REPL (the Python interactive shell, IDLE, ipython) the value of the last expression is printed to the screen and usually this is the intended behavior.



              Using an expression for side effects



              But in some cases you want to evaluate an expression for its side effects only, e.g., to see the results of your simulation plotted by matplotlib.



              In this cases you (probably) don't want to see the screenful of reprs of matplotlib objects that are sometimes returned by a call to a matplotlib function and one of the possibilities you have is to append a semicolon to the overly verbose statement, that immediately is composed by two expressions, the matplotlib invocation and a null statement, so that the value of the compound expression is None and nothing is printed to the screen by the interpreter
              (the other possibility being assignment, as in _ = plot(...) but I find that a bit more intrusive).



              Personal remark



              IMHO, the use of the semicolon to suppress not desired output in the interpreter has become more relevant following the introduction of the IPyton notebook, that permits to save the input and the output, including graphical output, of an interpreter session for documentation and eventual reuse.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Mar 17 '15 at 8:07









              gboffi

              8,84822454




              8,84822454












              • I see, thank you for sharing! This just solved exactly my problem, where semicolon can prevent repr by matplotlib in ipython notebook
                – Napitupulu Jon
                Mar 17 '15 at 9:12


















              • I see, thank you for sharing! This just solved exactly my problem, where semicolon can prevent repr by matplotlib in ipython notebook
                – Napitupulu Jon
                Mar 17 '15 at 9:12
















              I see, thank you for sharing! This just solved exactly my problem, where semicolon can prevent repr by matplotlib in ipython notebook
              – Napitupulu Jon
              Mar 17 '15 at 9:12




              I see, thank you for sharing! This just solved exactly my problem, where semicolon can prevent repr by matplotlib in ipython notebook
              – Napitupulu Jon
              Mar 17 '15 at 9:12











              10














              As everyone else has noted, you can use semicolons to separate statements. You don't have to, and it's not the usual style.



              As for why this is useful, some people like to put two or more really trivial short statements on a single line (personally I think this turns several trivial easily skimmed lines into one complex-looking line and makes it harder to see that it's trivial).



              But it's almost a requirement when you're invoking Python one liners from the shell using python -c '<some python code>'. Here you can't use indentation to separate statements, so if your one-liner is really a two-liner, you'll need to use a semicolon. And if you want to use other arguments in your one-liner, you'll have to import sys to get at sys.argv, which requires a separate import statement. e.g.



              python -c "import sys; print ' '.join(sorted(sys.argv[1:]))" 5 2 3 1 4
              1 2 3 4 5





              share|improve this answer

















              • 1




                You can use indentation and newlines when passing commands through python in the shell: Just span your quoted code over multiple lines or use a heredoc. Still, that fails if you want it to be a "one-liner". ;)
                – 00dani
                Jan 30 '14 at 1:26










              • includes traits for the rail answer with -c
                – n611x007
                Jul 6 '15 at 12:24


















              10














              As everyone else has noted, you can use semicolons to separate statements. You don't have to, and it's not the usual style.



              As for why this is useful, some people like to put two or more really trivial short statements on a single line (personally I think this turns several trivial easily skimmed lines into one complex-looking line and makes it harder to see that it's trivial).



              But it's almost a requirement when you're invoking Python one liners from the shell using python -c '<some python code>'. Here you can't use indentation to separate statements, so if your one-liner is really a two-liner, you'll need to use a semicolon. And if you want to use other arguments in your one-liner, you'll have to import sys to get at sys.argv, which requires a separate import statement. e.g.



              python -c "import sys; print ' '.join(sorted(sys.argv[1:]))" 5 2 3 1 4
              1 2 3 4 5





              share|improve this answer

















              • 1




                You can use indentation and newlines when passing commands through python in the shell: Just span your quoted code over multiple lines or use a heredoc. Still, that fails if you want it to be a "one-liner". ;)
                – 00dani
                Jan 30 '14 at 1:26










              • includes traits for the rail answer with -c
                – n611x007
                Jul 6 '15 at 12:24
















              10












              10








              10






              As everyone else has noted, you can use semicolons to separate statements. You don't have to, and it's not the usual style.



              As for why this is useful, some people like to put two or more really trivial short statements on a single line (personally I think this turns several trivial easily skimmed lines into one complex-looking line and makes it harder to see that it's trivial).



              But it's almost a requirement when you're invoking Python one liners from the shell using python -c '<some python code>'. Here you can't use indentation to separate statements, so if your one-liner is really a two-liner, you'll need to use a semicolon. And if you want to use other arguments in your one-liner, you'll have to import sys to get at sys.argv, which requires a separate import statement. e.g.



              python -c "import sys; print ' '.join(sorted(sys.argv[1:]))" 5 2 3 1 4
              1 2 3 4 5





              share|improve this answer












              As everyone else has noted, you can use semicolons to separate statements. You don't have to, and it's not the usual style.



              As for why this is useful, some people like to put two or more really trivial short statements on a single line (personally I think this turns several trivial easily skimmed lines into one complex-looking line and makes it harder to see that it's trivial).



              But it's almost a requirement when you're invoking Python one liners from the shell using python -c '<some python code>'. Here you can't use indentation to separate statements, so if your one-liner is really a two-liner, you'll need to use a semicolon. And if you want to use other arguments in your one-liner, you'll have to import sys to get at sys.argv, which requires a separate import statement. e.g.



              python -c "import sys; print ' '.join(sorted(sys.argv[1:]))" 5 2 3 1 4
              1 2 3 4 5






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 23 '11 at 3:24









              Ben

              45.8k1398138




              45.8k1398138








              • 1




                You can use indentation and newlines when passing commands through python in the shell: Just span your quoted code over multiple lines or use a heredoc. Still, that fails if you want it to be a "one-liner". ;)
                – 00dani
                Jan 30 '14 at 1:26










              • includes traits for the rail answer with -c
                – n611x007
                Jul 6 '15 at 12:24
















              • 1




                You can use indentation and newlines when passing commands through python in the shell: Just span your quoted code over multiple lines or use a heredoc. Still, that fails if you want it to be a "one-liner". ;)
                – 00dani
                Jan 30 '14 at 1:26










              • includes traits for the rail answer with -c
                – n611x007
                Jul 6 '15 at 12:24










              1




              1




              You can use indentation and newlines when passing commands through python in the shell: Just span your quoted code over multiple lines or use a heredoc. Still, that fails if you want it to be a "one-liner". ;)
              – 00dani
              Jan 30 '14 at 1:26




              You can use indentation and newlines when passing commands through python in the shell: Just span your quoted code over multiple lines or use a heredoc. Still, that fails if you want it to be a "one-liner". ;)
              – 00dani
              Jan 30 '14 at 1:26












              includes traits for the rail answer with -c
              – n611x007
              Jul 6 '15 at 12:24






              includes traits for the rail answer with -c
              – n611x007
              Jul 6 '15 at 12:24













              5














              A quote from "When Pythons Attack"




              Don't terminate all of your statements with a semicolon. It's technically legal to do this in Python, but is totally useless unless you're placing more than one statement on a single line (e.g., x=1; y=2; z=3).







              share|improve this answer

















              • 3




                Why not x,y,z = 1,2,3?
                – Anirban Nag 'tintinmj'
                Feb 17 '14 at 19:48






              • 2




                @AnirbanNag'tintinmj' In this particular case, I believe x, y, z = 1, 2, 3 is slower because it incurs an unnecessary round trip of building a tuple and then immediately desembles it. (By the way, I don't really think @communistpancake is suggesting the x=1; y=2; z=3 pattern though. I think he/she is merely giving an example for how semicolon is a separator rather than a terminator.)
                – RayLuo
                Sep 30 '16 at 21:12






              • 1




                @AnirbanNag - because that would have been a rubbish example of a multi-statement line.
                – c z
                Mar 2 '18 at 12:11
















              5














              A quote from "When Pythons Attack"




              Don't terminate all of your statements with a semicolon. It's technically legal to do this in Python, but is totally useless unless you're placing more than one statement on a single line (e.g., x=1; y=2; z=3).







              share|improve this answer

















              • 3




                Why not x,y,z = 1,2,3?
                – Anirban Nag 'tintinmj'
                Feb 17 '14 at 19:48






              • 2




                @AnirbanNag'tintinmj' In this particular case, I believe x, y, z = 1, 2, 3 is slower because it incurs an unnecessary round trip of building a tuple and then immediately desembles it. (By the way, I don't really think @communistpancake is suggesting the x=1; y=2; z=3 pattern though. I think he/she is merely giving an example for how semicolon is a separator rather than a terminator.)
                – RayLuo
                Sep 30 '16 at 21:12






              • 1




                @AnirbanNag - because that would have been a rubbish example of a multi-statement line.
                – c z
                Mar 2 '18 at 12:11














              5












              5








              5






              A quote from "When Pythons Attack"




              Don't terminate all of your statements with a semicolon. It's technically legal to do this in Python, but is totally useless unless you're placing more than one statement on a single line (e.g., x=1; y=2; z=3).







              share|improve this answer












              A quote from "When Pythons Attack"




              Don't terminate all of your statements with a semicolon. It's technically legal to do this in Python, but is totally useless unless you're placing more than one statement on a single line (e.g., x=1; y=2; z=3).








              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 23 '11 at 1:52









              CommunistPancake

              3941720




              3941720








              • 3




                Why not x,y,z = 1,2,3?
                – Anirban Nag 'tintinmj'
                Feb 17 '14 at 19:48






              • 2




                @AnirbanNag'tintinmj' In this particular case, I believe x, y, z = 1, 2, 3 is slower because it incurs an unnecessary round trip of building a tuple and then immediately desembles it. (By the way, I don't really think @communistpancake is suggesting the x=1; y=2; z=3 pattern though. I think he/she is merely giving an example for how semicolon is a separator rather than a terminator.)
                – RayLuo
                Sep 30 '16 at 21:12






              • 1




                @AnirbanNag - because that would have been a rubbish example of a multi-statement line.
                – c z
                Mar 2 '18 at 12:11














              • 3




                Why not x,y,z = 1,2,3?
                – Anirban Nag 'tintinmj'
                Feb 17 '14 at 19:48






              • 2




                @AnirbanNag'tintinmj' In this particular case, I believe x, y, z = 1, 2, 3 is slower because it incurs an unnecessary round trip of building a tuple and then immediately desembles it. (By the way, I don't really think @communistpancake is suggesting the x=1; y=2; z=3 pattern though. I think he/she is merely giving an example for how semicolon is a separator rather than a terminator.)
                – RayLuo
                Sep 30 '16 at 21:12






              • 1




                @AnirbanNag - because that would have been a rubbish example of a multi-statement line.
                – c z
                Mar 2 '18 at 12:11








              3




              3




              Why not x,y,z = 1,2,3?
              – Anirban Nag 'tintinmj'
              Feb 17 '14 at 19:48




              Why not x,y,z = 1,2,3?
              – Anirban Nag 'tintinmj'
              Feb 17 '14 at 19:48




              2




              2




              @AnirbanNag'tintinmj' In this particular case, I believe x, y, z = 1, 2, 3 is slower because it incurs an unnecessary round trip of building a tuple and then immediately desembles it. (By the way, I don't really think @communistpancake is suggesting the x=1; y=2; z=3 pattern though. I think he/she is merely giving an example for how semicolon is a separator rather than a terminator.)
              – RayLuo
              Sep 30 '16 at 21:12




              @AnirbanNag'tintinmj' In this particular case, I believe x, y, z = 1, 2, 3 is slower because it incurs an unnecessary round trip of building a tuple and then immediately desembles it. (By the way, I don't really think @communistpancake is suggesting the x=1; y=2; z=3 pattern though. I think he/she is merely giving an example for how semicolon is a separator rather than a terminator.)
              – RayLuo
              Sep 30 '16 at 21:12




              1




              1




              @AnirbanNag - because that would have been a rubbish example of a multi-statement line.
              – c z
              Mar 2 '18 at 12:11




              @AnirbanNag - because that would have been a rubbish example of a multi-statement line.
              – c z
              Mar 2 '18 at 12:11











              3














              Multiple statements on one line may include semicolons as separators. For example: http://docs.python.org/reference/compound_stmts.html In your case, it makes for an easy insertion of a point to break into the debugger.



              Also, as mentioned by Mark Lutz in the Learning Python Book, it is technically legal (although unnecessary and annoying) to terminate all your statements with semicolons.






              share|improve this answer


























                3














                Multiple statements on one line may include semicolons as separators. For example: http://docs.python.org/reference/compound_stmts.html In your case, it makes for an easy insertion of a point to break into the debugger.



                Also, as mentioned by Mark Lutz in the Learning Python Book, it is technically legal (although unnecessary and annoying) to terminate all your statements with semicolons.






                share|improve this answer
























                  3












                  3








                  3






                  Multiple statements on one line may include semicolons as separators. For example: http://docs.python.org/reference/compound_stmts.html In your case, it makes for an easy insertion of a point to break into the debugger.



                  Also, as mentioned by Mark Lutz in the Learning Python Book, it is technically legal (although unnecessary and annoying) to terminate all your statements with semicolons.






                  share|improve this answer












                  Multiple statements on one line may include semicolons as separators. For example: http://docs.python.org/reference/compound_stmts.html In your case, it makes for an easy insertion of a point to break into the debugger.



                  Also, as mentioned by Mark Lutz in the Learning Python Book, it is technically legal (although unnecessary and annoying) to terminate all your statements with semicolons.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 23 '11 at 1:58









                  mandarg

                  311




                  311























                      3














                      Semicolons are part of valid syntax: 8. Compound statements (The Python Language Reference)






                      share|improve this answer




























                        3














                        Semicolons are part of valid syntax: 8. Compound statements (The Python Language Reference)






                        share|improve this answer


























                          3












                          3








                          3






                          Semicolons are part of valid syntax: 8. Compound statements (The Python Language Reference)






                          share|improve this answer














                          Semicolons are part of valid syntax: 8. Compound statements (The Python Language Reference)







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Sep 10 '18 at 21:50









                          Peter Mortensen

                          13.5k1983111




                          13.5k1983111










                          answered Nov 23 '11 at 1:52









                          Dmitry B.

                          6,1922847




                          6,1922847























                              2














                              Python does let you use a semi-colon to denote the end of a statement if you are including more than one statement on a line.






                              share|improve this answer


























                                2














                                Python does let you use a semi-colon to denote the end of a statement if you are including more than one statement on a line.






                                share|improve this answer
























                                  2












                                  2








                                  2






                                  Python does let you use a semi-colon to denote the end of a statement if you are including more than one statement on a line.






                                  share|improve this answer












                                  Python does let you use a semi-colon to denote the end of a statement if you are including more than one statement on a line.







                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered Nov 23 '11 at 1:52









                                  Godwin

                                  7,01532647




                                  7,01532647























                                      2














                                      Semicolons can be used to one line two or more commands. They don't have to be used, but they aren't restricted.




                                      The semicolon ( ; ) allows multiple statements on the single line given that neither statement starts a new code block.




                                      http://www.tutorialspoint.com/python/python_basic_syntax.htm






                                      share|improve this answer


























                                        2














                                        Semicolons can be used to one line two or more commands. They don't have to be used, but they aren't restricted.




                                        The semicolon ( ; ) allows multiple statements on the single line given that neither statement starts a new code block.




                                        http://www.tutorialspoint.com/python/python_basic_syntax.htm






                                        share|improve this answer
























                                          2












                                          2








                                          2






                                          Semicolons can be used to one line two or more commands. They don't have to be used, but they aren't restricted.




                                          The semicolon ( ; ) allows multiple statements on the single line given that neither statement starts a new code block.




                                          http://www.tutorialspoint.com/python/python_basic_syntax.htm






                                          share|improve this answer












                                          Semicolons can be used to one line two or more commands. They don't have to be used, but they aren't restricted.




                                          The semicolon ( ; ) allows multiple statements on the single line given that neither statement starts a new code block.




                                          http://www.tutorialspoint.com/python/python_basic_syntax.htm







                                          share|improve this answer












                                          share|improve this answer



                                          share|improve this answer










                                          answered Nov 23 '11 at 1:54









                                          craniumonempty

                                          2,92911417




                                          2,92911417























                                              2














                                              Semicolons (like dots, commas and parentheses) tend to cause religious wars. Still, they (or some similar symbol) are useful in any programming language for various reasons.




                                              • Practical: the ability to put several short commands that belong conceptually together on the same line. A program text that looks like a narrow snake has the opposite effect of what is intended by newlines and indentation, which is highlighting structure.


                                              • Conceptual: separation of concerns between pure syntax (in this case, for a sequence of commands) from presentation (e.g. newline), in the old days called "pretty-printing".



                                              Observation: for highlighting structure, indentation could be augmented/replaced by vertical lines in the obvious way, serving as a "visual ruler" to see where an indentation begins and ends. Different colors (e.g. following the color code for resistors) may compensate for crowding.






                                              share|improve this answer


























                                                2














                                                Semicolons (like dots, commas and parentheses) tend to cause religious wars. Still, they (or some similar symbol) are useful in any programming language for various reasons.




                                                • Practical: the ability to put several short commands that belong conceptually together on the same line. A program text that looks like a narrow snake has the opposite effect of what is intended by newlines and indentation, which is highlighting structure.


                                                • Conceptual: separation of concerns between pure syntax (in this case, for a sequence of commands) from presentation (e.g. newline), in the old days called "pretty-printing".



                                                Observation: for highlighting structure, indentation could be augmented/replaced by vertical lines in the obvious way, serving as a "visual ruler" to see where an indentation begins and ends. Different colors (e.g. following the color code for resistors) may compensate for crowding.






                                                share|improve this answer
























                                                  2












                                                  2








                                                  2






                                                  Semicolons (like dots, commas and parentheses) tend to cause religious wars. Still, they (or some similar symbol) are useful in any programming language for various reasons.




                                                  • Practical: the ability to put several short commands that belong conceptually together on the same line. A program text that looks like a narrow snake has the opposite effect of what is intended by newlines and indentation, which is highlighting structure.


                                                  • Conceptual: separation of concerns between pure syntax (in this case, for a sequence of commands) from presentation (e.g. newline), in the old days called "pretty-printing".



                                                  Observation: for highlighting structure, indentation could be augmented/replaced by vertical lines in the obvious way, serving as a "visual ruler" to see where an indentation begins and ends. Different colors (e.g. following the color code for resistors) may compensate for crowding.






                                                  share|improve this answer












                                                  Semicolons (like dots, commas and parentheses) tend to cause religious wars. Still, they (or some similar symbol) are useful in any programming language for various reasons.




                                                  • Practical: the ability to put several short commands that belong conceptually together on the same line. A program text that looks like a narrow snake has the opposite effect of what is intended by newlines and indentation, which is highlighting structure.


                                                  • Conceptual: separation of concerns between pure syntax (in this case, for a sequence of commands) from presentation (e.g. newline), in the old days called "pretty-printing".



                                                  Observation: for highlighting structure, indentation could be augmented/replaced by vertical lines in the obvious way, serving as a "visual ruler" to see where an indentation begins and ends. Different colors (e.g. following the color code for resistors) may compensate for crowding.







                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered Apr 24 '13 at 5:49









                                                  Raymond

                                                  211




                                                  211























                                                      0














                                                      I realize I am biased as an old C programmer, but there are times when the various Python conventions make things hard to follow. I find the indent convention a bit of an annoyance at times.



                                                      Sometimes, clarity of when a statement or block ends is very useful. Standard C code will often read something like this:



                                                      for(i=0; i<100; i++) {
                                                      do something here;
                                                      do another thing here;
                                                      }

                                                      continue doing things;


                                                      where you use the whitespace for a lot of clarity - and it is easy to see where the loop ends.



                                                      Python does let you terminate with an (optional) semicolon. As noted above, that does NOT mean that there is a statement to execute followed by a 'null' statement. SO, for example,



                                                      print(x);
                                                      print(y);


                                                      Is the same as



                                                      print(x)
                                                      print(y)


                                                      If you believe that the first one has a null statement at the end of each line, try - as suggested - doing this:



                                                      print(x);;


                                                      It will throw a syntax error.



                                                      Personally, I find the semicolon to make code more readable when you have lots of nesting and functions with many arguments and/or long-named args. So, to my eye, this is a lot clearer than other choices:



                                                      if some_boolean_is_true:
                                                      call_function(
                                                      long_named_arg_1,
                                                      long_named_arg_2,
                                                      long_named_arg_3,
                                                      long_named_arg_4
                                                      );


                                                      since, to me, it lets you know that last ')' ends some 'block' that ran over many lines.



                                                      I personally think there is much to much made of PEP style guidelines, IDEs that enforce them, and the belief there is 'only one Pythonic way to do things'. If you believe the latter, go look at how to format numbers: as of now, Python supports four different ways to do it.



                                                      I am sure I will be flamed by some diehards, but the compiler/interpreter doesn't care if the arguments have long or short names, and - but for the indentation convention in Python - doesn't care about whitespace. The biggest problem with code is giving clarity to another human (and even yourself after months of work) to understand what is going on, where things start and end, etc.






                                                      share|improve this answer


























                                                        0














                                                        I realize I am biased as an old C programmer, but there are times when the various Python conventions make things hard to follow. I find the indent convention a bit of an annoyance at times.



                                                        Sometimes, clarity of when a statement or block ends is very useful. Standard C code will often read something like this:



                                                        for(i=0; i<100; i++) {
                                                        do something here;
                                                        do another thing here;
                                                        }

                                                        continue doing things;


                                                        where you use the whitespace for a lot of clarity - and it is easy to see where the loop ends.



                                                        Python does let you terminate with an (optional) semicolon. As noted above, that does NOT mean that there is a statement to execute followed by a 'null' statement. SO, for example,



                                                        print(x);
                                                        print(y);


                                                        Is the same as



                                                        print(x)
                                                        print(y)


                                                        If you believe that the first one has a null statement at the end of each line, try - as suggested - doing this:



                                                        print(x);;


                                                        It will throw a syntax error.



                                                        Personally, I find the semicolon to make code more readable when you have lots of nesting and functions with many arguments and/or long-named args. So, to my eye, this is a lot clearer than other choices:



                                                        if some_boolean_is_true:
                                                        call_function(
                                                        long_named_arg_1,
                                                        long_named_arg_2,
                                                        long_named_arg_3,
                                                        long_named_arg_4
                                                        );


                                                        since, to me, it lets you know that last ')' ends some 'block' that ran over many lines.



                                                        I personally think there is much to much made of PEP style guidelines, IDEs that enforce them, and the belief there is 'only one Pythonic way to do things'. If you believe the latter, go look at how to format numbers: as of now, Python supports four different ways to do it.



                                                        I am sure I will be flamed by some diehards, but the compiler/interpreter doesn't care if the arguments have long or short names, and - but for the indentation convention in Python - doesn't care about whitespace. The biggest problem with code is giving clarity to another human (and even yourself after months of work) to understand what is going on, where things start and end, etc.






                                                        share|improve this answer
























                                                          0












                                                          0








                                                          0






                                                          I realize I am biased as an old C programmer, but there are times when the various Python conventions make things hard to follow. I find the indent convention a bit of an annoyance at times.



                                                          Sometimes, clarity of when a statement or block ends is very useful. Standard C code will often read something like this:



                                                          for(i=0; i<100; i++) {
                                                          do something here;
                                                          do another thing here;
                                                          }

                                                          continue doing things;


                                                          where you use the whitespace for a lot of clarity - and it is easy to see where the loop ends.



                                                          Python does let you terminate with an (optional) semicolon. As noted above, that does NOT mean that there is a statement to execute followed by a 'null' statement. SO, for example,



                                                          print(x);
                                                          print(y);


                                                          Is the same as



                                                          print(x)
                                                          print(y)


                                                          If you believe that the first one has a null statement at the end of each line, try - as suggested - doing this:



                                                          print(x);;


                                                          It will throw a syntax error.



                                                          Personally, I find the semicolon to make code more readable when you have lots of nesting and functions with many arguments and/or long-named args. So, to my eye, this is a lot clearer than other choices:



                                                          if some_boolean_is_true:
                                                          call_function(
                                                          long_named_arg_1,
                                                          long_named_arg_2,
                                                          long_named_arg_3,
                                                          long_named_arg_4
                                                          );


                                                          since, to me, it lets you know that last ')' ends some 'block' that ran over many lines.



                                                          I personally think there is much to much made of PEP style guidelines, IDEs that enforce them, and the belief there is 'only one Pythonic way to do things'. If you believe the latter, go look at how to format numbers: as of now, Python supports four different ways to do it.



                                                          I am sure I will be flamed by some diehards, but the compiler/interpreter doesn't care if the arguments have long or short names, and - but for the indentation convention in Python - doesn't care about whitespace. The biggest problem with code is giving clarity to another human (and even yourself after months of work) to understand what is going on, where things start and end, etc.






                                                          share|improve this answer












                                                          I realize I am biased as an old C programmer, but there are times when the various Python conventions make things hard to follow. I find the indent convention a bit of an annoyance at times.



                                                          Sometimes, clarity of when a statement or block ends is very useful. Standard C code will often read something like this:



                                                          for(i=0; i<100; i++) {
                                                          do something here;
                                                          do another thing here;
                                                          }

                                                          continue doing things;


                                                          where you use the whitespace for a lot of clarity - and it is easy to see where the loop ends.



                                                          Python does let you terminate with an (optional) semicolon. As noted above, that does NOT mean that there is a statement to execute followed by a 'null' statement. SO, for example,



                                                          print(x);
                                                          print(y);


                                                          Is the same as



                                                          print(x)
                                                          print(y)


                                                          If you believe that the first one has a null statement at the end of each line, try - as suggested - doing this:



                                                          print(x);;


                                                          It will throw a syntax error.



                                                          Personally, I find the semicolon to make code more readable when you have lots of nesting and functions with many arguments and/or long-named args. So, to my eye, this is a lot clearer than other choices:



                                                          if some_boolean_is_true:
                                                          call_function(
                                                          long_named_arg_1,
                                                          long_named_arg_2,
                                                          long_named_arg_3,
                                                          long_named_arg_4
                                                          );


                                                          since, to me, it lets you know that last ')' ends some 'block' that ran over many lines.



                                                          I personally think there is much to much made of PEP style guidelines, IDEs that enforce them, and the belief there is 'only one Pythonic way to do things'. If you believe the latter, go look at how to format numbers: as of now, Python supports four different ways to do it.



                                                          I am sure I will be flamed by some diehards, but the compiler/interpreter doesn't care if the arguments have long or short names, and - but for the indentation convention in Python - doesn't care about whitespace. The biggest problem with code is giving clarity to another human (and even yourself after months of work) to understand what is going on, where things start and end, etc.







                                                          share|improve this answer












                                                          share|improve this answer



                                                          share|improve this answer










                                                          answered Jul 28 '18 at 5:58









                                                          eSurfsnake

                                                          277110




                                                          277110






























                                                              draft saved

                                                              draft discarded




















































                                                              Thanks for contributing an answer to Stack Overflow!


                                                              • Please be sure to answer the question. Provide details and share your research!

                                                              But avoid



                                                              • Asking for help, clarification, or responding to other answers.

                                                              • Making statements based on opinion; back them up with references or personal experience.


                                                              To learn more, see our tips on writing great answers.





                                                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                                              Please pay close attention to the following guidance:


                                                              • Please be sure to answer the question. Provide details and share your research!

                                                              But avoid



                                                              • Asking for help, clarification, or responding to other answers.

                                                              • Making statements based on opinion; back them up with references or personal experience.


                                                              To learn more, see our tips on writing great answers.




                                                              draft saved


                                                              draft discarded














                                                              StackExchange.ready(
                                                              function () {
                                                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f8236380%2fwhy-is-semicolon-allowed-in-this-python-snippet%23new-answer', 'question_page');
                                                              }
                                                              );

                                                              Post as a guest















                                                              Required, but never shown





















































                                                              Required, but never shown














                                                              Required, but never shown












                                                              Required, but never shown







                                                              Required, but never shown

































                                                              Required, but never shown














                                                              Required, but never shown












                                                              Required, but never shown







                                                              Required, but never shown







                                                              這個網誌中的熱門文章

                                                              Tangent Lines Diagram Along Smooth Curve

                                                              Yusuf al-Mu'taman ibn Hud

                                                              Zucchini