-2

Would python intrepet this:

if hour < 7 and hour > 0 or hour > 20 and hour < 23:

the same as

if  7 > hour > 0 or 23 > hour > 20 (this one is just the usual mathematical inequality)

if not then what should I write to tell python this inequality?

azro
  • 53,056
  • 7
  • 34
  • 70
Hugo atm
  • 11
  • 3

2 Answers2

1

You can use comparison chaining.

if (0 < hour < 7) or (20 < hour < 23):
    # do stuff

(Parenthesis for emphasis.)

timgeb
  • 76,762
  • 20
  • 123
  • 145
0

You can always use parentheses to be sure:

if (hour < 7 and hour > 0) or (hour > 20 and hour < 23):
alift
  • 1,855
  • 2
  • 13
  • 28