36

In one piece of my program I doubt if i use the comparison correctly. i want to make sure that ( u0 <= u < u0+step ) before do something.

if not (u0 <= u) and (u < u0+step):
 u0 = u0+ step # change the condition until it is satisfied
else:
 do something. # condition is satisfied
Serenity
37k21 gold badges125 silver badges117 bronze badges
asked Nov 11, 2010 at 10:08
1
  • 12
    Python magic: u0 <= u < (u0 + step) Commented Nov 11, 2010 at 10:10

5 Answers 5

57

You can do:

if not (u0 <= u <= u0+step):
 u0 = u0+ step # change the condition until it is satisfied
else:
 do sth. # condition is satisfied

Using a loop:

while not (u0 <= u <= u0+step):
 u0 = u0+ step # change the condition until it is satisfied
do sth. # condition is satisfied
MERose
4,4617 gold badges57 silver badges86 bronze badges
answered Nov 11, 2010 at 10:19
10

Operator precedence in python
You can see that not X has higher precedence than and. Which means that the not only apply to the first part (u0 <= u). Write:

if not (u0 <= u and u < u0+step): 

or even

if not (u0 <= u < u0+step): 
answered Nov 11, 2010 at 10:14
4

In this particular case the clearest solution is the S.Lott answer

But in some complex logical conditions I would prefer use some boolean algebra to get a clear solution.

Using De Morgan's law ¬(A^B) = ¬Av¬B

not (u0 <= u and u < u0+step)
(not u0 <= u) or (not u < u0+step)
u0 > u or u >= u0+step

then

if u0 > u or u >= u0+step:
 pass

... in this case the «clear» solution is not more clear :P

answered Nov 11, 2010 at 12:04
1
  • Interesting advice generally, but as You said, it does not help clarity in this situation. Commented Sep 11, 2015 at 7:14
3

Why think? If not confuses you, switch your if and else clauses around to avoid the negation.

i want to make sure that ( u0 <= u < u0+step ) before do sth.

Just write that.

if u0 <= u < u0+step:
 "do sth" # What language is "sth"? No vowels. An odd-looking word.
else:
 u0 = u0+ step

Why overthink it?

If you need an empty if -- and can't work out the logic -- use pass.

 if some-condition-that's-too-complex-for-me-to-invert:
 pass
 else: 
 do real work here
answered Nov 11, 2010 at 11:08
4
  • "sth" is "some thing?" In what language? Commented Nov 11, 2010 at 15:44
  • 3
    I think I saw this abbreviation in some dictionaries. Anyhow I'm not native English speaker, excuse my English mistakes! Commented Nov 11, 2010 at 16:51
  • maybe the inversion of "some-condition-that's-too-complex-for-me-to-invert" is equal to; "not (some-condition-that's-too-complex-for-me-to-invert)" works? Commented May 30, 2014 at 16:50
  • I totally agree with this answer. Commented Feb 28, 2020 at 10:07
0

There are two ways. In case of doubt, you can always just try it. If it does not work, you can add extra braces to make sure, like that:

if not ((u0 <= u) and (u < u0+step)):
answered Nov 11, 2010 at 10:15

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.