What does a semicolon do in Python?


I found some code that had variables inside classes, ending in a semicolon. I didn't understand why, and I didn't find anything about it on the internet either.

    class exemplo():

      self.variavel_teste = 0;


It is only needed if you want to put an extra command on the same line, so it works as a command separator. Obviously it's allowed to put it in and then leave nothing, which makes it look like an equal terminator for C and its descendants. It's actually a terminator, but optional in almost all situations. And in this specific case it seems abuse of the resource.

This is called compound statements .

x = 5; print(x);
if x > 0: print("maior que zero"); print(x);

It is the same as

x = 5
if x > 0:
    print("maior que zero")

See working on ideone . And on repl.it. Also posted on GitHub for future reference .

Scroll to Top