Skip to main content
typo
Source Link
xnor
  • 149.7k
  • 26
  • 287
  • 676

Python

a = 1 b = 2 if a: print("a is truthy") if b: print("b is truthy") if a & b: print("a and b are both truthy") 

Try it online!

In Python, & does "bitwise and" as opposed to logical and. So, 1&2==0 since these numbers share no setsset bits. All numbers but 0 are truthy, so 1 and 2 each satisfy an if, but 0 does not.

We could also have a and b be disjoint sets, on which & does set intersection.

Python

a = 1 b = 2 if a: print("a is truthy") if b: print("b is truthy") if a & b: print("a and b are both truthy") 

Try it online!

In Python, & does "bitwise and" as opposed to logical and. So, 1&2==0 since these numbers share no sets bits. All numbers but 0 are truthy, so 1 and 2 each satisfy an if, but 0 does not.

We could also have a and b be disjoint sets, on which & does set intersection.

Python

a = 1 b = 2 if a: print("a is truthy") if b: print("b is truthy") if a & b: print("a and b are both truthy") 

Try it online!

In Python, & does "bitwise and" as opposed to logical and. So, 1&2==0 since these numbers share no set bits. All numbers but 0 are truthy, so 1 and 2 each satisfy an if, but 0 does not.

We could also have a and b be disjoint sets, on which & does set intersection.

Source Link
xnor
  • 149.7k
  • 26
  • 287
  • 676

Python

a = 1 b = 2 if a: print("a is truthy") if b: print("b is truthy") if a & b: print("a and b are both truthy") 

Try it online!

In Python, & does "bitwise and" as opposed to logical and. So, 1&2==0 since these numbers share no sets bits. All numbers but 0 are truthy, so 1 and 2 each satisfy an if, but 0 does not.

We could also have a and b be disjoint sets, on which & does set intersection.