0

The following code assigns a certain value to the a variable depending on the range that the v variable falls in.

if v>0 and v<1000:
    c='green'
elif v>=1000 and v<2000:
    c='yellow'
else:
    c='red'

That works well, but I was wondering if there is a more Pythonic way to write the following conditional block.

multigoodverse
  • 7,638
  • 19
  • 64
  • 106

1 Answers1

6

0 < v < 1000

thats all ...

edit

this would work for your specific use case

var_color = ['green', 'yellow', 'red'][(v >= 1000) + (v >= 2000)]
Community
  • 1
  • 1
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179