I wrote a condition like:
if a>100 and b>100 and c>100 and d>100:
print("yes")
I want to shorten it, is there any way to make it like this:
if (a,b,c,d)>100:
print("yes")
CodePudding user response:
You could use all
along with a generator expression:
if all(value > 100 for value in [a, b, c, d]):
print("yes")
CodePudding user response:
If the minimum of those values is greater than 100, then all of them are.
if min(a, b, c, d) > 100:
print("yes")
This works well a for small number of values, but it may not be as fast as using all
if you have a lot of values to test. all
should return as soon as it finds one value that doesn't meet the condition, but min
has to look at every one to find the minimum.