Python: condition between two values, in both directions quickly
Solved
Lecodeurhtmlcss
Posted messages
79
Status
Member
-
Lecodeurhtmlcss Posted messages 79 Status Member -
Lecodeurhtmlcss Posted messages 79 Status Member -
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
Thank you very much!
--
(ง ͠° ͟ل͜ ͡°)ง щ(ಠ益ಠщ) (≖ ͜ʖ≖) Σ(⊙ロ⨀) ! /╲/╭(ఠఠ益ఠఠ)╮/╱\ \(º □ º l|l)/
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
ifthat needs to test if a value is between two others, like
a<machin<b. But the condition must also work for
a>machin>bbecause a and b are variables and can be negative! Is there a simple function to check
a>machin>b and a<machin<bmore logically?
Thank you very much!
--
(ง ͠° ͟ل͜ ͡°)ง щ(ಠ益ಠщ) (≖ ͜ʖ≖) Σ(⊙ロ⨀) ! /╲/╭(ఠఠ益ఠఠ)╮/╱\ \(º □ º l|l)/
1 answer
-
yg_be Posted messages 23437 Registration date Status Contributor Last intervention Ambassadeur 1 588
Hello,
what have you tried?-
████████████████████████ EDIT ██████████████████████
Ah ! I meanta>machin>b or a<machin<b
! OR of course !
(And so I know that we can doif a>machin>b or a<machin<b
, but isn't there a function like if machin between(a,b) ?)- 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.
-