Neil Walker
Neil Walker

Reputation: 6858

Why can't use semi-colon before for loop in Python?

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)

Upvotes: 23

Views: 3436

Answers (6)

hojin
hojin

Reputation: 1405

Try this.

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

Upvotes: 0

Mayank Dixit
Mayank Dixit

Reputation: 1

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.

Upvotes: -1

arshajii
arshajii

Reputation: 129537

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.

Upvotes: 14

BrenBarn
BrenBarn

Reputation: 251458

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.

Upvotes: 25

Dmytro Sirenko
Dmytro Sirenko

Reputation: 5083

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.

Upvotes: 2

Dan Lenski
Dan Lenski

Reputation: 79792

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.

Upvotes: 5

Related Questions