Search code examples
pythonand-operator

AND operator while assignment


What does this means :

>>> a = 12 and 13
>>> a
13

How does and operator works while assigning values or is just that it takes the last value ?


Solution

  • If the expression on the left of and is false, it will be returned. Otherwise, and returns the value on the right:

    0 and 13
    Out[3]: 0
    
    12 and 13
    Out[4]: 13
    

    You should think about how this works- check all the combinations of True/False on the left and right, and you'll see that having and work this way means the value it returns always reflects the True/False status of both expressions.