59

I got a function online to help me with my current project and it had semicolons on some of the lines. I was wondering why? Is it to break the function?

def containsAny(self, strings=[]):
    alphabet = 'abcdefghijklmnopqrstuvwxyz0123456789'
    for string in strings:
        for char in string:
            if char in alphabet: return 1;
    return 0;

The function I got online with little modification:

for string in strings:
    for char in string:
        if char in alphabet: return 1;

Is the above saying the following?

if char in alphabet:
    return 1
    break
dreftymac
  • 31,404
  • 26
  • 119
  • 182
Brandon Nadeau
  • 3,568
  • 13
  • 42
  • 65
  • Related post - [Why is semicolon allowed in this python snippet?](https://stackoverflow.com/q/8236380/465053) – RBT Jul 19 '18 at 07:32
  • Related: *[When is semicolon use in Python considered "good" or "acceptable"?](https://stackoverflow.com/questions/19365508/)* – Peter Mortensen Apr 10 '22 at 16:19

5 Answers5

105

The semicolon does nothing in the code you show.

I suspect this is someone who programs in another language (C, Java, ...) that requires semicolons at the end of statements and it's just a habit (happens to me sometimes too).

If you want to put several Python statements on the same line, you can use a semi-colon to separate them, see this Python Doc:

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

Levon
  • 138,105
  • 33
  • 200
  • 191
  • Semicolons can [suppress output in IPython](https://stackoverflow.com/questions/56572156/does-a-semicolon-at-the-end-of-a-python-statement-suppress-output#comment99722886_56572156). – Peter Mortensen Nov 22 '22 at 21:36
19

The semicolon here does not do anything. People who come from C/C++/Java/(many other language) backgrounds tend to use the semicolon out of habit.

arshajii
  • 127,459
  • 24
  • 238
  • 287
2

In general the semicolon does nothing. But if you are using the Jupyter Notebook (depending on your version), you might get a figure plotted twice. The semicolon at the end of your plot command prevents this:

df.plot();
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
christianbauer1
  • 467
  • 5
  • 16
1

Programmers of C, C++, and Java are habituated of using a semicolon to tell the compiler that this is the end of a statement, but for Python this is not the case.

The reason is that in Python, newlines are an unambiguous way of separating code lines; this is by design, and the way this works has been thoroughly thought through. As a result, Python code is perfectly readable and unambiguous without any special end-of-statement markers (apart from the newline).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Abrar Ahmad
  • 109
  • 1
  • 3
  • 3
    "has been thoroughly thought through" -- It's not everyday that you can see those 3 words back to back in a normal conversation – CLOVIS Mar 12 '19 at 22:34
0

As other answers point out, the semicolon does nothing there. It's a separator (e.g. print 1;print 2). But it does not work like this: def func():print 1;print 2;;print'Defined!' (;; is a syntax error). Out of habit, people tend to use it (as it is required in languages such as C/Java...).

EKons
  • 887
  • 2
  • 20
  • 27