Python: How to express less or equal, equal greater, environment? - python

I want to do the following example in python:
x = 10
y = 8
if x-5 <= y <= x+5:
print(y)
I see that this is working, but I would like to know if it's "ok" like this, if there is a better solution or something I have to consider doing it like this.

Chained expressions are acceptable in Python:
Comparisons can be chained arbitrarily, e.g., x < y <= z is equivalent
to x < y and y <= z, except that y is evaluated only once (but in both
cases z is not evaluated at all when x < y is found to be False).
In fact, because and is lazy and the syntax is cleaner, they are preferable.
Just be aware that chained expressions take priority. For example, see Why does the expression 0 < 0 == 0 return False in Python?.

Related

How is the result of the expression (0==0<1<2<3>2>1>0==0) calculated in python?

Please explain how is the result of the expression (0==0<1<2<3>2>1>0==0) calculated in python.
Unlike most languages, Python supports chained comparisons. So the following:
0==0<1<2<3>2>1>0==0
is equivalent to:
0==0 and 0<1 and 1<2 and 2<3 and 3>2 and 2>1 and 1>0 and 0==0
You can read about it here. The relevant excerpt is:
Comparisons can be chained arbitrarily, e.g., x < y <= z is equivalent to x < y and y <= z, except that y is evaluated only once (but in both cases z is not evaluated at all when x < y is found to be false).

Efficient and cleaner way of assigning values

x = 5
if y > x:
y = x
Seems a little archaic.
is there a more efficient way to shorten the last 2 lines here into one.
You can make those 2 lines into one:
if y > x: y = x
Or using an inline condition:
y = x if y > x else y
Or using a boolean expression (shorter but not as easy to read):
y = [x,y][y>x]
When dealing with limits, the functions min() and max() come in handy.
min() returns the smallest item in an iterable or the smallest of two or more arguments, max() returns the largest item.
y = min(y, x)
If y is smaller or equal than x, y isn't changed - otherwise it is set to x, because x is the smaller value.
Documentation: Built-in function min()

two numerical comparisons in one expression python

I thought I read somewhere that python (3.x at least) is smart enough to handle this:
x = 1.01
if 1 < x < 0:
print('out of range!')
However it is not working for me.
I know I can use this instead:
if ((x > 1) | (x < 0)):
print('out of range!')
... but is it possible to fix the version above?
It works well, it is your expression that is always False; try this one instead:
x = .99
if 1 > x > 0:
print('out of range!')
Python chained comparisons work like mathematical notation. In math, "0 < x < 1" means that x is greater than 0 and less than one, and "1 < x < 0" means that x is greater than 1 and less than 0.
And. Not or. Both conditions need to hold.
If you want an "or" , you can write one yourself. It's or in Python, not |; | is bitwise OR.
if x > 1 or x < 0:
whatever()
Alternatively, you can write your expression in terms of "and":
if not (0 <= x <= 1):
whatever()
You can do it in one compound expression, as you've already noted, and others have commented. You cannot do it in an expression with an implied conjunction (and / or), as you're trying to do with 1 < x < 0. Your expression requires an or conjunction, but Python's implied operation in this case is and.
Therefore, to get what you want, you have to reverse your conditional branches and apply deMorgan's laws:
if not(0 <= x <= 1):
print('out of range!')
Now you have the implied and operation, and you get the control flow you wanted.

getting syntax error with <= sign

When using this code:
while str.find(target,key,n) != -1 and <= len(target):
I get a syntax error on 'less than or equal to sign'. Why?
In English we can say "if X is not equal to Y and also less than Z", but Python syntax doesn't work that way.
If you want to compare a number to two other numbers, you have to repeat the original number in the second comparison.
i.e., instead of this:
if x != y and < z:
you must do this:
if x != y and x < z:
If you want to use the double operand, you could rewrite it like:
if -1 < target.find(key, n) < len(target):
pass
But that said, I don’t think find can ever return a value larger than the length of target.

What does `<>` mean in Python?

I'm trying to use in Python 3.3 an old library (dating from 2003!). When I import it, Python throws me an error because there are <> signs in the source file, e.g.:
if (cnum < 1000 and nnum <> 1000 and ntext[-1] <> "s":
...
I guess it's a now-abandoned sign in the language.
What exactly does it mean, and which (more recent) sign should I replace it with?
It means not equal to. It was taken from ABC (python's predecessor) see here:
x < y, x <= y, x >= y, x > y, x = y, x <> y, 0 <= d < 10
Order tests (<> means 'not equals')
I believe ABC took it from Pascal, a language Guido began programming with.
It has now been removed in Python 3. Use != instead. If you are CRAZY you can scrap != and allow only <> in Py3K using this easter egg:
>>> from __future__ import barry_as_FLUFL
>>> 1 != 2
File "<stdin>", line 1
1 != 2
^
SyntaxError: with Barry as BDFL, use '<>' instead of '!='
>>> 1 <> 2
True
It means NOT EQUAL, but it is deprecated, use != instead.
It's worth knowing that you can use Python itself to find documentation, even for punctuation mark operators that Google can't cope with.
>>> help("<>")
Comparisons
Unlike C, all comparison operations in Python have the same priority,
which is lower than that of any arithmetic, shifting or bitwise
operation. Also unlike C, expressions like a < b < c have the
interpretation that is conventional in mathematics:
Comparisons yield boolean values: True or False.
Comparisons can be chained arbitrarily, e.g., x < y <= z is
equivalent to x < y and y <= z, except that y is evaluated
only once (but in both cases z is not evaluated at all when x <
y is found to be false).
The forms <> and != are equivalent; for consistency with C,
!= is preferred; where != is mentioned below <> is also
accepted. The <> spelling is considered obsolescent.
See http://docs.python.org/2/reference/expressions.html#not-in
It is an old way of specifying !=, that was removed in Python 3. A library old enough to use it likely runs into various other incompatibilities with Python 3 as well: it is probably a good idea to run it through 2to3, which automatically changes this, among many other things.
Use != or <>. Both stands for not equal.
[Reference: Python language reference]
The comparison operators <> and != are alternate spellings of the same operator. != is the preferred spelling; <> is obsolescent.

Categories