Behaviour of `and` vs `&`

hello, I found the source code of the function `and` but not for `&` as I want to see why the truthfulness of only left condition is evaluated in case of `and` but did not find anything for it. Could someone please help?

`and` is a logical operation. It returns `True` if both of the operands are true.
`&` is a bitwise operator, performing a bit-by-bit operation.

Consider `42 and 24`. If `42` was `False` (which it can’t be), the evaluation would stop. This is known as “lazy evaluation”. But the first statement is `True`, so the second statement is evaluated; which in this case is also `True`, so that is what will be returned.

Now, consider `42 & 24`:

``````42 - 101010
24 - 011000
& - 001000

001000 - 8
``````

Just to add: I see, from the excellent post by @jeff5, that my answer is maybe not what you were looking for, but I’ll leave it posted; it could be of use to someone.

You may have found the source code for `&`, for some type, but you can’t have found the source code for `and`. In operator `&` both arguments will be evaluated and dealt with as @rob42 explained.

The compiler deals with `and` so that you get the short-circuit evaluation like this (varies with version of CPython):

``````>>> prog = """
if a and b():
print("hello")
print("world")
"""
>>> dis(compile(prog, '', 'exec'))
2 POP_JUMP_IF_FALSE       18
6 CALL_FUNCTION            0
8 POP_JUMP_IF_FALSE       18

14 CALL_FUNCTION            1
16 POP_TOP

4     >>   18 LOAD_NAME                2 (print)
22 CALL_FUNCTION            1
24 POP_TOP
If `a` is false, `b()` is never called.
Have a look at the `__and__` magic method: Python __and__() Magic Method – Finxter