boolean - Python True/False for the following code -


how evaluated python code:

not not true or false , not true 

as of myself, have 2 guesses:

method 1:

step  code
1        not not true or false , not true
2        not false or false , false
3        true or false , false
4        true , false
5        false

method 2:

step  code
1        not not true or false , not true
2        not false or false , false
3        true or false
4        true

from table of python's operators precedence:

  • or has lower precedence than
  • and in turn has lower precedence
  • than not

according that:

  not not true or false , not true 

is equivalent to

  ((not (not true)) or (false , (not true))) 

edit: noticed martijn pieters in comment below, worth mentionning python has short-circuits operators. means and , or guaranteed evaluated left-to-right and that:

  • if left-term of or true right term never evaluated (as result of true or whatever true in boolean logic)
  • if left-term of and false right term never evaluated (as result of false , whatever false in boolean logic)

so, given example:

  1. the first step evaluate not true false:

     ((not (not true)) or (false , (not true)))         ^^^^^^^^          false 
  2. then not false evaluated true:

     ((not    false  ) or (false , (not true)))    ^^^^^^^^^^^^^^        true 
  3. as no have true or ... python stops evaluation result true:

     (     true        or (false , (not true)))        ^^^^^^^^^^^^^^ ......................                   true         (right-hand side ignored) 

Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -