Python: condition between two values, in both directions quickly

Solved
Lecodeurhtmlcss Posted messages 79 Status Membre -  
Lecodeurhtmlcss Posted messages 79 Status Membre -
Good evening!
I am working on a Python project that requires as much speed as possible; my level is rather beginner.
So, in one part of my code, I use a condition
if
that needs to test if a value is between two others, like
a<machin<b
. But the condition must also work for
a>machin>b
because a and b are variables and can be negative! Is there a simple function to check
a>machin>b and a<machin<b
more logically?

Thank you very much!

--
(ง ͠° ͟ل͜ ͡°)ง щ(ಠ益ಠщ) (≖ ͜ʖ≖) Σ(⊙ロ⨀) ! /╲/╭(ఠఠ益ఠఠ)╮/╱\ \(º □ º l|l)/

1 réponse

yg_be Posted messages 23437 Registration date   Status Contributeur Last intervention   Ambassadeur 1 587
 
Hello,
what have you tried?
3
Lecodeurhtmlcss Posted messages 79 Status Membre 2
 
████████████████████████ EDIT ██████████████████████

Ah ! I meant
a>machin>b or a<machin<b
! OR of course !
(And so I know that we can do
if a>machin>b or a<machin<b
, but isn't there a function like if machin between(a,b) ?)
0
vortex > Lecodeurhtmlcss Posted messages 79 Status Membre
 
Good evening,

No, it doesn't exist. But it's not hard to make.

def number_between(v, a, b, equal=False): if a > b: a, b = b, a return a < v < b if not equal else a <= v <= b


In terms of speed, well it's a Python function, and in terms of usefulness, meh, usually we know what we need to compare in a program.
1
Lecodeurhtmlcss Posted messages 79 Status Membre 2 > vortex
 
You're welcome!
0