BENSALVAGE
Posts: 65
Joined: Thu Jun 25, 2015 6:57 pm

Between symbol

Tue Sep 29, 2015 8:48 pm

Can someone tell me the between to symbol for Python so I can create an action if my readings are between two numbers

User avatar
PeterO
Posts: 5357
Joined: Sun Jul 22, 2012 4:14 pm

Re: Between symbol

Tue Sep 29, 2015 8:54 pm

Do you mean something like this ?

Code: Select all

if reading > lowerlimit and reading < upperlimit:
   print "In Range"
else:
   print "Out of Range"
PeterO
Last edited by PeterO on Tue Sep 29, 2015 9:22 pm, edited 1 time in total.
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

gordon77
Posts: 4373
Joined: Sun Aug 05, 2012 3:12 pm

Re: Between symbol

Tue Sep 29, 2015 8:58 pm

Or

if lowerlimit <= x <= upperlimit:

User avatar
PeterO
Posts: 5357
Joined: Sun Jul 22, 2012 4:14 pm

Re: Between symbol

Tue Sep 29, 2015 9:01 pm

See, I'm really not a very good python programmer :-)
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

BENSALVAGE
Posts: 65
Joined: Thu Jun 25, 2015 6:57 pm

Re: Between symbol

Tue Sep 29, 2015 9:01 pm

Yes that's the sort of thing I need I've just tried it in my code and it brings up syntax error

BENSALVAGE
Posts: 65
Joined: Thu Jun 25, 2015 6:57 pm

Re: Between symbol

Tue Sep 29, 2015 9:09 pm

Just tried the >=value<= value symbols and works brilliant thanks

User avatar
PeterO
Posts: 5357
Joined: Sun Jul 22, 2012 4:14 pm

Re: Between symbol

Tue Sep 29, 2015 9:23 pm

BENSALVAGE wrote:Yes that's the sort of thing I need I've just tried it in my code and it brings up syntax error
Probably due to and extra space that crept in !
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

W. H. Heydt
Posts: 11288
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: Between symbol

Tue Sep 29, 2015 11:57 pm

gordon77 wrote:Or

if lowerlimit <= x <= upperlimit:
Okay....who runs the Obfuscated Python competition?

User avatar
kusti8
Posts: 3439
Joined: Sat Dec 21, 2013 5:29 pm
Location: USA

Re: Between symbol

Wed Sep 30, 2015 12:35 am

W. H. Heydt wrote:
gordon77 wrote:Or

if lowerlimit <= x <= upperlimit:
Okay....who runs the Obfuscated Python competition?
To me that's not obfuscated. It's not the most clear it could be, but it shows just like a compound inequality (is that the name?) in math.
There are 10 types of people: those who understand binary and those who don't.

gordon77
Posts: 4373
Joined: Sun Aug 05, 2012 3:12 pm

Re: Between symbol

Wed Sep 30, 2015 7:38 am

W. H. Heydt wrote:
gordon77 wrote:Or

if lowerlimit <= x <= upperlimit:
Okay....who runs the Obfuscated Python competition?
Looks clear to me
:D

User avatar
PeterO
Posts: 5357
Joined: Sun Jul 22, 2012 4:14 pm

Re: Between symbol

Wed Sep 30, 2015 9:53 am

I find that these "pythonic" ways of doing things do in general make sense on first reading (Unlike most of C++! :lol: )
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
RogerW
Posts: 286
Joined: Sat Dec 20, 2014 12:15 pm
Location: London UK

Re: Between symbol

Wed Sep 30, 2015 10:41 am

Perhaps more pythonesque:

Code: Select all

if x in range(lowerlimit, upperlimit):

User avatar
PeterO
Posts: 5357
Joined: Sun Jul 22, 2012 4:14 pm

Re: Between symbol

Wed Sep 30, 2015 10:48 am

RogerW wrote:Perhaps more pythonesque:

Code: Select all

if x in range(lowerlimit, upperlimit):
But probably not very good from a performance point of view !
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
RogerW
Posts: 286
Joined: Sat Dec 20, 2014 12:15 pm
Location: London UK

Re: Between symbol

Wed Sep 30, 2015 1:43 pm

PeterO wrote: But probably not very good from a performance point of view !
PeterO
True but not that bad

Code: Select all

import time as tm

count = 1000
t1 = tm.perf_counter()
while count:
    if(1 << count << 500):
        pass
    count -= 1
print(tm.perf_counter() - t1)

count = 1000
t1 = tm.perf_counter()
while count:
    if(count in range(1,500)):
        pass
    count -= 1
print(tm.perf_counter() - t1)
The range loop takes twice as long as the first one but still only takes 3msec so unless the test is right at the heart of a massive loop it is unlikely to be significant. Anyway if performance is an issue is python the right language.

The test code ran on python 3.4.2 (Rasbian jessie)

User avatar
jojopi
Posts: 3115
Joined: Tue Oct 11, 2011 8:38 pm

Re: Between symbol

Wed Sep 30, 2015 3:13 pm

In Python 3, range() returns a "range" object that operators like "in" can use directly. It is only expanded to a list as a last resort.

In Python 2, range() returns a list, so it can be slow and use a lot of memory. (Python 2 has xrange(), but it does not seem to be as efficient as plain range() is in Python 3.)

I think that mathematical style "A < x < B" is the most Pythonic, and it also works correctly with other orderable types such as floats and strings. You get to choose the short-circuit order by writing "B > x > A" if you really want to.

The fact that many other languages still parse multiple inequalities strictly as binary operations like "(A < x) < B" is really a throwback to hardware and software limitations that have not been relevant for decades. It is confusing (for beginners) and almost never useful.

Return to “Troubleshooting”