First python program, syntax error in while loop


R

Roy Smith

If I ever have even the slightest doubt, I just go ahead and type
"<language> operator precedence" into a web search and check it :)

Well, that solves the problem once, and it solves it for me. I figure
if I'm not 100% sure, then maybe other people aren't 100% sure either,
and my adding the extra parens helps them too.
 
Ad

Advertisements

T

Terry Jan Reedy

Well, that solves the problem once, and it solves it for me. I figure
if I'm not 100% sure, then maybe other people aren't 100% sure either,
and my adding the extra parens helps them too.

If you keep the Python docs handy, on or off line, the Language manual
Expressions chapter ends with this single page (but better formatted as
a table than here). But I sometimes add parens for quickness or readability.

6.15. Operator precedence
The following table summarizes the operator precedences in Python, from
lowest precedence (least binding) to highest precedence (most binding).
Operators in the same box have the same precedence. Unless the syntax is
explicitly given, operators are binary. Operators in the same box group
left to right (except for comparisons, including tests, which all have
the same precedence and chain from left to right — see section
Comparisons — and exponentiation, which groups from right to left).

Operator Description
lambda Lambda expression
if – else Conditional expression
or Boolean OR
and Boolean AND
not x Boolean NOT
in, not in, is, is not, <, <=, >, >=, !=, == Comparisons, including
membership tests and identity tests,
| Bitwise OR
^ Bitwise XOR
& Bitwise AND
<<, >> Shifts
+, - Addition and subtraction
*, /, //, % Multiplication, division, remainder [5]
+x, -x, ~x Positive, negative, bitwise NOT
** Exponentiation [6]
x[index], x[index:index], x(arguments...), x.attribute Subscription,
slicing, call, attribute reference
(expressions...), [expressions...], {key: value...}, {expressions...}
Binding or tuple display, list display, dictionary display, set display

Footnotes

[1] While abs(x%y) < abs(y) is true mathematically, for floats it may
not be true numerically due to roundoff. For example, and assuming a
platform on which a Python float is an IEEE 754 double-precision number,
in order that -1e-100 % 1e100 have the same sign as 1e100, the computed
result is -1e-100 + 1e100, which is numerically exactly equal to 1e100.
The function math.fmod() returns a result whose sign matches the sign of
the first argument instead, and so returns -1e-100 in this case. Which
approach is more appropriate depends on the application.
[2] If x is very close to an exact integer multiple of y, it’s possible
for x//y to be one larger than (x-x%y)//y due to rounding. In such
cases, Python returns the latter result, in order to preserve that
divmod(x,y)[0] * y + x % y be very close to x.
[3] While comparisons between strings make sense at the byte level, they
may be counter-intuitive to users. For example, the strings "\u00C7" and
"\u0327\u0043" compare differently, even though they both represent the
same unicode character (LATIN CAPITAL LETTER C WITH CEDILLA). To compare
strings in a human recognizable way, compare using unicodedata.normalize().
[4] Due to automatic garbage-collection, free lists, and the dynamic
nature of descriptors, you may notice seemingly unusual behaviour in
certain uses of the is operator, like those involving comparisons
between instance methods, or constants. Check their documentation for
more info.
[5] The % operator is also used for string formatting; the same
precedence applies.
[6] The power operator ** binds less tightly than an arithmetic or
bitwise unary operator on its right, that is, 2**-1 is 0.5.
 
C

Chris Angelico

If you keep the Python docs handy, on or off line, the Language manual
Expressions chapter ends with this single page (but better formatted as a
table than here). But I sometimes add parens for quickness or readability.

or Boolean OR
and Boolean AND

Actually, this is one exception. I tend to parenthesize any case where
there's a complex set of conditions and mixed AND and OR operations.
Part of the reason for this is that any expression that can be
affected by the precedence of and and or will most likely be fairly
long and/or complex anyway, so a few extra parens won't hurt.

ChrisA
 
D

Dennis Lee Bieber

In this case, however, I have no problem with the extra parens. Look at
these two statements:


They have the same meaning. To correctly interpret the second one, you
need to know that != and < bind tighter than "and". One could say that
you should know that, and maybe you would be right.
If there were a chance of confusion, I'd probably have written it

while (number != guess
and tries < 5) :

(yes, a deliberate line break, not a news reader line wrap)
 
A

alex23

One of these days I'll work out why some people insist on using
superfluous parentheses in Python code.  Could it be that they enjoy
exercising their fingers by reaching for the shift key in conjunction
with the 9 or 0 key?

One of these days I'll work out why some programmers consider typing
to be "effort".
 
Ad

Advertisements

M

Mark Lawrence

One of these days I'll work out why some programmers consider typing
to be "effort".

I think it's very important to consider this aspect. E.g. any one using
dynamically typed languages has to put in more effort as they should be
typing up their test code, whereas people using statically typed
languages can simply head down to the pub once their code has compiled.
 
Ad

Advertisements

C

Chris Angelico

I think it's very important to consider this aspect. E.g. any one using
dynamically typed languages has to put in more effort as they should be
typing up their test code, whereas people using statically typed languages
can simply head down to the pub once their code has compiled.

And those who are porting other people's code to a new platform have
weeks of work just to get ./configure to run, but after that it's
smooth...

Everything is variable.

ChrisA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top