Python Equality Check Difference -


Suppose we want to execute some blocks of code, when both 'A' and 'B' 5 Are equal to say. We can write like this:

  if a == 5 and b == 5: # do something   

But a few days ago, I just Check out the similar situation that was written effortlessly:

  if a == b and b == 5: do something   

from which I think, someone Is not the difference between the two? Apart from this, there is another way too,

  if a == b == 5: # do something   

is there any difference, any difference Assessed or executed process or time taken?

Is it better or which is better?

Is it related to the concept of transit?

Since they are equivalent to basically , so the way you read Think about the code /:

  if a == 5 and b == 5: #some   

may read as if "if a is equal to 5 and b equal 5 , then do ..." . You must do idea / find , even then a b equals.

This is the opposite of the next example:

  if a == b and b == 5: # something   

As reads "If is equal to one b and b is equal to 5 "and you end will be equal to a at that time 5

That is why I I like:

  if any == b == 5: # do something   

If you are familiar with Python (Thanks) It is immediately clear that all three things are If people with less experience in Python (but programming skills in other languages) see this, then they can evaluate it by

  If (a == b) == 5:   

which will compare the Boolean result of the first comparison with integer 5, , which does not do Python and Due to different results (for example a = 0, b = 0 : a == b == 0 is true while < Code> (a == b) == 0 not !

Says:

Python has eight comparative operations. All of them have the same priority (which is more than the Boolean operations). Can be chains comparatively; For example, X & lt; y & lt; = Z is equal to x; Y and y that y is only evaluated once (but in both cases z is not evaluated when x & lt; y is found false).

There may even be any difference, for example if your example evaulating b will be a side effect.

Regarding transit, you are right.

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -