Some statements use : in the tail such as while x > 0: and def func():
What is the meaning and the usage of : in Python? 18 1328
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 means something else follows in an indented block as part of the same
statement.
--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
To perceive is to suffer.
-- Aristotle
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;
Paul Robson <au******@autismuk.muralichucks.freeserve.co.uk> wrote in message news:<pa****************************@autismuk.mura lichucks.freeserve.co.uk>... while i > 0 do begin writeln i; i := i - 1; end;
while i > 0: print i i = i - 1
So the : is similar to "begin" and the last space line is similar to
"end", right? How about
while i > 0 :
print i
i = i-1
this is answered in the python faq: http://www.python.org/doc/faq/genera...ass-statements The colon is required primarily to enhance readability
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?
Chang LI wrote: Paul Robson <au******@autismuk.muralichucks.freeserve.co.uk> wrote in message news:<pa****************************@autismuk.mura lichucks.freeserve.co.uk>... So the : is similar to "begin"
Correct
and the last space line is similar to "end", right?
Nope. You don't need a space line (you mean an empty line by that, right?),
you can just outdent one level and continue without any empty lines
(although in the interactive interpreter you need the empty line to end the
block on the first indentation level). So you could do:
i = 5
while i > 0:
print i
i = i-1
print "That's it"
How about while i > 0 : print i i = i-1
Works just fine. Other than the indentation, spacing is pretty much
irrelevant in Python.
--
Timo Virkkala
Dave Anderson wrote: this is answered in the python faq:
http://www.python.org/doc/faq/genera...ass-statements
The colon is required primarily to enhance readability
It also allows 'suiteless' versions of some statements:
if x > 0: print "Greater that 0"
while x > 0: x -= 1
def f(): pass
class c(object): pass
for x in range(4): print x
Actually using that feature outside sample code is generally bad karma, though ;)
Cheers,
Nick.
Timo Virkkala a écrit : Chang LI wrote:
Paul Robson <au******@autismuk.muralichucks.freeserve.co.uk> wrote in message news:<pa****************************@autismuk.mura lichucks.freeserve.co.uk>...
So the : is similar to "begin"
Correct
Not correct. You need both the ':' and the extra level of indentation on
next line, ie:
Python 2.3.3 (#2, Feb 17 2004, 11:45:40) while True:
.... print "aha"
File "<stdin>", line 2
print "aha"
^
IndentationError: expected an indented block
and the last space line is similar to "end", right?
Nope. You don't need a space line (you mean an empty line by that, right?), you can just outdent one level and continue without any empty lines (although in the interactive interpreter you need the empty line to end the block on the first indentation level). So you could do:
i = 5 while i > 0: print i i = i-1 print "That's it"
<meta> Is this a pb when typing of copying ? It's just plain wonrg code
anyway : while i > 0:
.... print i
.... i -= 1
File "<stdin>", line 3
i = i-1
^
SyntaxError: invalid syntax while i > 0:
.... print i
.... i -= 1
....
5
4
3
2
1
The level of indentation has to be the same within a block, and it has
to be *1* (*one*) extra-or-less level of indentation than the previous
block.
<please-someone-correct-me-if-i-am-wrong>
In fact, from a purely technical POV, the ':' could have been omitted
from the Python syntax, since indentation does the whole job of defining
blocks. It's only here for readability AFAIK.
</please-someone-correct-me-if-i-am-wrong>
Bruno
bruno modulix wrote: <please-someone-correct-me-if-i-am-wrong> In fact, from a purely technical POV, the ':' could have been omitted from the Python syntax, since indentation does the whole job of defining blocks. It's only here for readability AFAIK. </please-someone-correct-me-if-i-am-wrong>
Without the ':', single-line suites are impossible. If you allow only
multi-line suites, you're right.
Reinhold
--
[Windows ist wie] die Bahn: Man muss sich um nichts kuemmern, zahlt fuer
jede Kleinigkeit einen Aufpreis, der Service ist mies, Fremde koennen
jederzeit einsteigen, es ist unflexibel und zu allen anderen Verkehrs-
mitteln inkompatibel. -- Florian Diesch in dcoulm
Reinhold Birkenfeld ha scritto: Without the ':', single-line suites are impossible. If you allow only multi-line suites, you're right.
are they really?
if <expression> <expressions>
and the likes would invho parse just fine
On Sun, 28 Nov 2004 19:17:52 +0000, gabriele renzi wrote: are they really? if <expression> <expressions> and the likes would invho parse just fine
if "" "a" print "Hi"
Does that print Hi or not?
Dig deeper into Python grammar; make sure you know that statements and
expressions are different, and the counter-example above is based on
Python's string concatenation rules:
Python 2.3.4 (#1, Oct 26 2004, 20:13:42)
[GCC 3.4.2 (Gentoo Linux 3.4.2-r2, ssp-3.4.1-1, pie-8.7.6.5)] on linux2
Type "help", "copyright", "credits" or "license" for more information. "a" "b"
'ab'
The question is, what is the "<expression>", "" or "" "a"? The first is
false, the second true.
(At this point the natural response of many people is to start adding
rules and exceptions and "well, obviously"s... but none of them will beat
if <expression>:, or justify the removal of a colon if you compare back
to the original; that's the *real* competition. This is just a pre-emptive
point 'cause I've seen people do this sort of thing too often, it isn't
targetted directly at you, gabriele.)
A completely new grammar could certainly do away with it and there are
languages that can do that, but you almost certainly won't be able to get
there from here via incremental changes. (Forth is close but it is spelled
differently. Lisp natually encapsulates all expressions; I don't think it
quite captures the spirit of what I think you're getting at, but for the
Lisp definition of "expression", it works.)
In the faq, http://www.python.org/doc/faq/genera...ass-statements,
it is stated that the colon is there "primarily to enhance readability
(one of the results of the experimental ABC language)." But can that
statement really be backed up? Has someone made a study or something?
I always thought the rule was "the less useless symbols, the higher
the readability." I.e:
if (a == b)
{
print a
}
is less readable than:
if a == b:
print a
Because it contains more non-significant symbols (, ), { and } that
"steal" the programmers attention. But consider
def f(x, y, z)
print x, y, z
to
def f(x, y, z):
print x, y, z
IMHO, the colon-less variant is more readable than the one with the colon.
--
mvh Björn
On Sun, 28 Nov 2004 22:10:31 +0100, BJörn Lindqvist wrote: I always thought the rule was "the less useless symbols, the higher the readability." I.e:
There's a bit of circularity there, in that a symbol's use can *be* to
enhance readability. I'd certainly agree that instances where this holds
true is an exception rather than the rule, but it seems plausible to me
that Python is symbol-free enough for this to be a potential
justification.
The only punctuation you *need* is whitespace. See Forth (I don't know
if this is perfect but I'd bet the transform is simple), the HP RPN
calculator programming languages (IIRC, the only other punctuation is a
block delimiter, which would also be easy to transform away), or some
hacks I've cooked up for my own use where I minimized the token count
because I had to write the parser by hand and I was willing to trade a
little extra work to write the parsed stuff for an easier translation.
(Usually it is data, not programs, so while saying that's a "compilation"
is technically true it isn't what most people think of.) Take it to those
extremes and you'd see what I mean; extra symbols can help.
(Or, God help you, see the whitespace language for a demonstration that
all you need is whitespace, period. http://compsoc.dur.ac.uk/whitespace/ )
Jeremy Bowers wrote: The only punctuation you *need* is whitespace. See Forth
You don't even need that... see FORTRAN. :-)
DOI=1TO10-ly,
--
Greg Ewing, Computer Science Dept,
University of Canterbury,
Christchurch, New Zealand http://www.cosc.canterbury.ac.nz/~greg
>>>>> "Greg" == Greg Ewing <gr**@cosc.canterbury.ac.nz> writes: The only punctuation you *need* is whitespace. See Forth
Greg> You don't even need that... see FORTRAN. :-)
And you don't need everything else either... see this. http://compsoc.dur.ac.uk/whitespace/
:-)
Regards,
Isaac.
A two-fer.
On Mon, 29 Nov 2004 10:28:42 +0800, Isaac To wrote: >> "Greg" == Greg Ewing <gr**@cosc.canterbury.ac.nz> writes: >> The only punctuation you *need* is whitespace. See Forth Greg> You don't even need that... see FORTRAN. :-)
Well, I for one don't like reading large programs with no line feeds :-)
And you don't need everything else either... see this.
http://compsoc.dur.ac.uk/whitespace/
I'm pretty sure if you remove all the whitespace and you remove all the
non-whitespace that you have indeed gotten a little too minimal :-)
On Mon, 29 Nov 2004 01:12:16 -0500, Jeremy Bowers wrote: The only punctuation you *need* is whitespace. See Forth (I don't know if this is perfect but I'd bet the transform is simple),
: Announce ." Forth has a fair bit of punctuation" ;
In <41***********************@news.free.fr>, bruno modulix wrote: <please-someone-correct-me-if-i-am-wrong> In fact, from a purely technical POV, the ':' could have been omitted from the Python syntax, since indentation does the whole job of defining blocks. It's only here for readability AFAIK. </please-someone-correct-me-if-i-am-wrong>
The ':' serves as a very good hint to "python aware" text editors to
automagically indent the next line after hitting return.
Ciao,
Marc 'BlackJack' Rintsch
BJörn Lindqvist wrote: Because it contains more non-significant symbols (, ), { and } that "steal" the programmers attention. But consider
def f(x, y, z) print x, y, z
to
def f(x, y, z): print x, y, z
IMHO, the colon-less variant is more readable than the one with the colon.
Except that it is quite acceptable to do the following:
def f(x, y, z,
long_func_arg_name):
long_func_arg_name(x, y, z)
def f(x, y, z,
long_func_arg_name)
long_func_arg_name(x, y, z)
The colons do a decent job of flagging the beginning of suites, mainly because
of Python general lack of *other* punctuation (e.g. the colon would be entirely
ineffective at improving readability if every line ended with a semi-colon).
Cheers,
Nick. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: William C. White |
last post by:
Does anyone know of a way to use PHP /w Authorize.net AIM without using
cURL? Our website is hosted on a shared drive and the webhost company
doesn't installed additional software (such as cURL)...
|
by: Albert Ahtenberg |
last post by:
Hello,
I don't know if it is only me but I was sure that header("Location:url")
redirects the browser instantly to URL, or at least stops the execution of
the code. But appearantely it continues...
|
by: James |
last post by:
Hi,
I have a form with 2 fields.
'A'
'B'
The user completes one of the fields and the form is submitted.
On the results page I want to run a query, but this will change
subject to which...
|
by: Ollivier Robert |
last post by:
Hello,
I'm trying to link PHP with Oracle 9.2.0/OCI8 with gcc 3.2.3 on a Solaris9
system. The link succeeds but everytime I try to run php, I get a SEGV from
inside the libcnltsh.so library.
...
|
by: Richard Galli |
last post by:
I want viewers to compare state laws on a single subject.
Imagine a three-column table with a drop-down box on the top. A viewer
selects a state from the list, and that state's text fills the...
|
by: Albert Ahtenberg |
last post by:
Hello,
I have two questions.
1. When the user presses the back button and returns to a form he filled
the form is reseted. How do I leave there the values he inserted?
2. When the...
|
by: inderjit S Gabrie |
last post by:
Hi all
Here is the scenerio ...is it possibly to do this...
i am getting valid course dates output on to a web which i have designed
....all is okay so far , look at the following web url
...
|
by: Jack |
last post by:
Hi All,
What is the PHP equivilent of Oracle bind variables in a SQL statement, e.g.
select x from y where z=:parameter
Which in asp/jsp would be followed by some statements to bind a value...
|
by: Sandwick |
last post by:
I am trying to change the size of a drawing so they are all 3x3.
the script below is what i was trying to use to cut it in half ... I
get errors.
I can display the normal picture but not the...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |