About the if else combination of python list derivation

about python list derivation, if else always doesn"t understand. Ask God to know
, as shown in the following code

list_range=[1,2,3,4,5]
cnum=5
list_range[(cnum-3) if (cnum-3)>0 else 0:cnum-1]

the final output result is [3br 4]
according to my understanding, if cnum=5, satisfies the if condition, then it should be list_range [2], that is, the output value should be 3, and now the output is [3Power4]
puzzling ah, is it possible that after meeting the conditions, the value of cnum-3 will replace the 0 in front of 0:cnum-1?

Mar.03,2021

 

list_range [(cnum-3) if (cnum-3) > 0 else 0:cnum-1] take a closer look, this is a range with start and end

  • cnum = 5, so if else returns true, that is, start=cnum-3=2
  • end=cnum-1 = 5-1 = 4
  • final expression: list_range [2:4], so it is [3jing4]
Menu