Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why this way of executing python isn't working

Tags:

python

python -c 'for i in range(10): print i'

works, and

python -c 'a=3;'

works, but

python -c 'a=3;for i in range(10): print i'

gave an error

  File "<string>", line 1
    a=3;for i in range(10): print i
          ^
SyntaxError: invalid syntax

Could anyone help point out why? Thank you.

like image 925
Qiang Li Avatar asked Apr 20 '26 17:04

Qiang Li


1 Answers

Because that's how Python syntax works. for, like all other statements that introduce an indent level, does not like having anything in front of it.

You need a newline in this case, not a semicolon. These are legal (assuming bash):

python -c 'a=3
for i in range(10)]: print(i)'

python -c $'a=3\nfor i in range(10): print(i)'
like image 76
Amadan Avatar answered Apr 22 '26 06:04

Amadan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!