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

前端 未结 5 453
执念已碎
执念已碎 2020-11-30 11:32

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          


        
5条回答
  •  情歌与酒
    2020-11-30 11:47

    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.

提交回复
热议问题