2

So far I have coded mainly in Java. So I am more used to the fact that a statement ends with a semicolon. If I continue the same habit here in Python (which by the way helps me to keep a nice continuity), how will that be perceived as far as programming practices are concerned?

anirban.at.web
  • 349
  • 2
  • 11
  • 1
    It's bad. Don't do it. Write Python in Python. – TigerhawkT3 Dec 13 '15 at 12:28
  • Definitely not standard practice. If no-one else is going to read your code, sure, go for it. But if you plan on others reading your code, don't, or you'll receive more comments about the semicolons than about the actual code. – Prashanth Chandra Dec 13 '15 at 12:29
  • The only time I see people use semicolons in Python is if they have multiple statements on a single line, in which case they're needed as statement separators (and in that case, there is no semicolon after the final statement). I personally avoid this. – Tom Karzes Dec 13 '15 at 12:31
  • 1
    I'd still say don't do it, even if no one is watching. – TigerhawkT3 Dec 13 '15 at 12:31
  • When I see Python code littered with semicolons: `≖_≖`... `ಠ_ಠ`. – TigerhawkT3 Dec 13 '15 at 12:33
  • Also, did you try a basic Google search like "python semicolon"? – TigerhawkT3 Dec 13 '15 at 12:35

2 Answers2

3

Semicolons are generally not used to terminate a statement in Python because they are not necessary.

You can use them for compound statements such as

x = 1; y = 2

However, the PEP8 style guide discourages these statements.

Compound statements (multiple statements on the same line) are generally discouraged.

Personally, I use semicolons only when I do quick and dirty stuff on the command line, e.g.:

$ python -c "print('hi'); print('SO')"
hi
SO
timgeb
  • 76,762
  • 20
  • 123
  • 145
2

Normally the semicolon is only used in Python if you want to have multiple commands in one line. So you should omit the semicolons where they are not necessary.

Randrian
  • 1,055
  • 12
  • 25