The use of :

Paul Robson autismuk at autismuk.muralichucks.freeserve.co.uk
Fri Nov 26 09:33:40 CET 2004


On Thu, 25 Nov 2004 18:58:16 -0800, Chang LI wrote:

> Some statements use : in the tail such as while x > 0: and def func():
> What is the meaning and the usage of : in Python?

It, together with the indent, identifies a block of code.  Most languages
use either a terminating keyword like "WEND" "ENDIF" "NEXT" to identify
the end of a group of instructions, or alternatively a bracketing syntax
such as {} begin..end or $( $)

So these are roughly equivalent. The difference with the Python version
(the last one) is without the indentation it won't work - the indentation
does the job of WEND, { }  and begin .. end

WHILE I > 0
 PRINT I 
 I = I-1
WEND

while (i > 0)
{
	printf("%d\n",i);
	i = i-1;
}

while i > 0 do
  begin
	writeln i;
	i := i - 1;
  end;

while i > 0:
	print i
	i = i - 1

this code, unlike most other languages is different to the one above.
Changing the indentation actually changes what the code does.

while i > 0:
	print i
i = i - 1

it is equivalent to C

while (i > 0)
{
	printf("%d\n",i);
}
i = i-1;





More information about the Python-list mailing list