Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does a semicolon do?

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 
like image 382
Brandon Nadeau Avatar asked Sep 08 '12 23:09

Brandon Nadeau


People also ask

When should a semicolon be used examples?

A semicolon may be used between independent clauses joined by a connector, such as and, but, or, nor, etc., when one or more commas appear in the first clause. Example: When I finish here, and I will soon, I'll be glad to help you; and that is a promise I will keep.

What is a semicolon example?

When to Use a Semicolon. A semicolon (;) is a punctuation mark that has two main functions: Semicolons separate items in a complex list. For example, The Council is comprised of ten members: three from Sydney, Australia; four from Auckland, New Zealand; two from Suva, Fiji; and one from Honiara, Solomon Islands.


2 Answers

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

like image 93
Levon Avatar answered Oct 15 '22 12:10

Levon


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.

like image 34
arshajii Avatar answered Oct 15 '22 10:10

arshajii