Crispy Crispy - 4 years ago 169
Python Question

Python: What Does a Semi Colon Do?

I got a function online to help me with my current project and it had semi colons 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;


Function I got online with little modification:

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


Is this ^ saying

if char in alphabet:
return 1
break


Thanks for any effort to help.

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download