In which order is an if statement evaluated in Python

后端 未结 5 1858
醉酒成梦
醉酒成梦 2020-12-03 20:56

If you have an if statement where several variables or functions are evaluated, in which order are they evaluated?

if foo > 5 or bar > 6:
    print \'f         


        
5条回答
  •  南方客
    南方客 (楼主)
    2020-12-03 21:32

    Operator evaluated from left to right. but only when the priority of all the operator is the same. When the priority of the operators is not equal then the operator will execute according to the priority.

    ex.

    False or False or True and False False

    True or False and True or False True

    False or False and True or False False

    i.e First in the expression the operator having the highest priority executed then further execute.

提交回复
热议问题