Python Forum
Thread Rating:
  • 2 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
' | ' Operator and 'OR'
#4
This is why it's incomplete:

5 % 5 | 5 % 4 == 0
Output:
False
As I interpret your lambda function, you want to know if n is divisible by x or y. If you use n = 5, x = 5, and y = 4; the result is false. However, 5 is evenly divisible by 5 so the result is wrong. The interpreter is evaluating the expressions on each side of the "|" before doing the bitwise or:

5 % 5 evaluates to 0 (which is equivalent to False in Python).
5 % 4 == 0 evaluates to False (or 0).
0 | False evaluates to False.

To correct that, the first expression needs to be compared to 0 as well as the second:

is_divisible = lambda n,x,y : n % x == 0 | n % y == 0
Reply


Messages In This Thread
' | ' Operator and 'OR' - by blackknite - Jan-10-2019, 01:10 AM
RE: ' | ' Operator and 'OR' - by stullis - Jan-10-2019, 02:24 AM
RE: ' | ' Operator and 'OR' - by blackknite - Jan-10-2019, 04:47 PM
RE: ' | ' Operator and 'OR' - by stullis - Jan-10-2019, 06:28 PM
RE: ' | ' Operator and 'OR' - by nilamo - Jan-10-2019, 06:40 PM
RE: ' | ' Operator and 'OR' - by Gribouillis - Jan-10-2019, 06:54 PM
RE: ' | ' Operator and 'OR' - by nilamo - Jan-10-2019, 07:08 PM
RE: ' | ' Operator and 'OR' - by blackknite - Jan-10-2019, 11:08 PM
RE: ' | ' Operator and 'OR' - by stullis - Jan-11-2019, 01:23 AM
RE: ' | ' Operator and 'OR' - by nilamo - Jan-11-2019, 05:51 AM
RE: ' | ' Operator and 'OR' - by blackknite - Jan-11-2019, 09:32 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020