21

I can join lines in Python using semi-colon, e.g.

a=5; b=10

But why can't I do the same with for

x=['a','b']; for i,j in enumerate(x): print(i,":", j)
vaultah
  • 44,105
  • 12
  • 114
  • 143
Neil Walker
  • 6,400
  • 14
  • 57
  • 86

6 Answers6

24

Because the Python grammar disallows it. See the documentation:

stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]

Semicolons can only be used to separate simple statements (not compound statements like for). And, really, there's almost no reason to ever use them even for that. Just use separate lines. Python isn't designed to make it convenient to jam lots of code onto one line.

BrenBarn
  • 242,874
  • 37
  • 412
  • 384
  • Thanks, I'm writing some notes for students and space was short so decided to join them as I was mentioning ; at the same time anyway :) – Neil Walker Jun 18 '14 at 19:12
13

The short (yet valid) answer is simply "because the language grammar isn't defined to allow it". As for why that's the case, it's hard if not impossible to be sure unless you ask whoever came up with that portion of the grammar, but I imagine it's due to readability, which is one of the goals of Python1.

Why would you ever want to write something obscure like that? Just split it up into multiple lines:

x = ['a','b']
for i,j in enumerate(x):
    print(i, ":", j)

I would argue that this variant is much clearer.


1 From import this: Readability counts.

arshajii
  • 127,459
  • 24
  • 238
  • 287
  • 8
    One use case would be to use in command line: `python -c 'import something; for ...'` – xOneca Dec 20 '18 at 12:20
  • 1
    For use case from @xOneca we can use list comprehension and run multiple list comprehension for each line inside the required for loop. E.g.: l = [1, 2, 3]; [print(x) for x in l]; [print(x*2) for x in l]; – vishal Jan 30 '23 at 13:47
5

Because Guido van Rossum, the creator of Python, and other developers, don't actually like the "semicolons and braces" style of code formatting.

For a brief look at how they feel about these things, try:

from __future__ import braces

Statements in Python are supposed to be separated by blank lines, and compound statements in Python are supposed to be bounded by indentation.

The existence of ; and one-line compound statements (e.g. for i in x: print i) are meant to be only very limited concessions... and you can't combine them.

Dan Lenski
  • 76,929
  • 13
  • 76
  • 124
2

The grammar of Python does not allow this. It's a good answer, but what's the reason for it?

I think the logic behind the decision is the following: body of a for loop must be indented in order to be recognized. So, if we allow not a simple_stmt there, it would require a complex and easy-to-break indentation.

Dmytro Sirenko
  • 5,003
  • 21
  • 26
  • 1
    Good point. I think that's probably the best rationale for this particular limitation (beyond just saying "GvR hates semicolons and braces") – Dan Lenski Jun 18 '14 at 19:59
1

Try this.

x=['a','b']; [print(i,":", j) for i,j in enumerate(x)]
hojin
  • 1,221
  • 14
  • 16
0

A compound statement consists 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.

x=['a','b'];

This does not justify the clause definition and thus cannot be used as a part of a compound statement. Therefore you encounter error.