Questions tagged [boolean-logic]

An algebraic system developed by George Boole. Uses operations such as "And", "Or" and "Not" on Binary True/False values. It may be used to accomplish complex tasks.

Boolean logic is a two-element boolean algebra, a formalism that gives algebraic semantics for classical propositional logic.

1556 questions
724
votes
27 answers

How to test multiple variables against a single value?

I'm trying to make a function that will compare multiple variables to an integer and output a string of three letters. I was wondering if there was a way to translate this into Python. So say: x = 0 y = 1 z = 3 mylist = [] if x or y or z == 0 : …
user1877442
  • 7,431
  • 3
  • 11
  • 5
590
votes
64 answers

Check if at least two out of three booleans are true

An interviewer recently asked me this question: given three boolean variables, a, b, and c, return true if at least two out of the three are true. My solution follows: boolean atLeastTwo(boolean a, boolean b, boolean c) { if ((a && b) || (b &&…
user282886
  • 2,945
  • 8
  • 20
  • 10
378
votes
4 answers

Does Python support short-circuiting?

Does Python support short-circuiting in boolean expressions?
Dinah
  • 48,876
  • 29
  • 126
  • 149
350
votes
14 answers

Why does (0 < 5 < 3) return true?

I was playing around in jsfiddle.net and I'm curious as to why this returns true? if(0 < 5 < 3) { alert("True"); } So does this: if(0 < 5 < 2) { alert("True"); } But this doesn't: if(0 < 5 < 1) { alert("True"); } Is this quirk ever…
punkrockbuddyholly
  • 9,077
  • 7
  • 33
  • 60
269
votes
6 answers

How can I obtain the element-wise logical NOT of a pandas Series?

I have a pandas Series object containing boolean values. How can I get a series containing the logical NOT of each value? For example, consider a series containing: True True True False The series I'd like to get would…
Louis Thibault
  • 16,122
  • 21
  • 72
  • 136
176
votes
3 answers

pandas: multiple conditions while indexing data frame - unexpected behavior

I am filtering rows in a dataframe by values in two columns. For some reason the OR operator behaves like I would expect AND operator to behave and vice versa. My test code: import pandas as pd df = pd.DataFrame({'a': range(5), 'b': range(5) }) #…
Wojciech Walczak
  • 2,680
  • 2
  • 20
  • 21
165
votes
27 answers

Logic to test that 3 of 4 are True

I want to return True if and only if 3 out of 4 boolean values are true. The closest I've gotten is (x ^ y) ^ (a ^ b): What should I do?
Simon Kuang
  • 3,715
  • 4
  • 24
  • 53
149
votes
12 answers

If condition A is matched, condition B needs to be matched in order to do action C

My question is: if (/* condition A */) { if(/* condition B */) { /* do action C */ } else /* ... */ } else { /* do action C */ } Is it possible to just write the code of action C one time instead of twice? How…
133
votes
13 answers

Easiest way to flip a boolean value?

I just want to flip a boolean based on what it already is. If it's true - make it false. If it's false - make it true. Here is my code excerpt: switch(wParam) { case VK_F11: if (flipVal == true) { flipVal = false; } else { flipVal =…
John T
  • 22,621
  • 11
  • 53
  • 81
133
votes
9 answers

What are bitwise operators?

I'm someone who writes code just for fun and haven't really delved into it in either an academic or professional setting, so stuff like these bitwise operators really escapes me. I was reading an article about JavaScript, which apparently supports…
click
  • 1,365
  • 2
  • 10
  • 5
117
votes
11 answers

Differences in boolean operators: & vs && and | vs ||

I know the rules for && and || but what are & and |? Please explain these to me with an example.
Sumithra
  • 6,197
  • 18
  • 48
  • 49
114
votes
3 answers

Difference between `not` and `!` in ruby

Are not and ! synonyms, or are they evaluated differently?
0112
  • 3,194
  • 6
  • 28
  • 53
114
votes
3 answers

Any good boolean expression simplifiers out there?

I was refactoring old code and encountered several IF conditions that were way too complex and long and I'm certain they can be simplified. My guess is that those conditions grew so much because of later modifications. Anyway, I was wondering if any…
mojarras
  • 1,534
  • 2
  • 10
  • 9
110
votes
10 answers

Is there a more elegant way to express ((x == a and y == b) or (x == b and y == a))?

I'm trying to evaluate ((x == a and y == b) or (x == b and y == a)) in Python, but it seems a bit verbose. Is there a more elegant way?
91
votes
9 answers

How to convert "0" and "1" to false and true

I have a method which is connecting to a database via Odbc. The stored procedure which I'm calling has a return value which from the database side is a 'Char'. Right now I'm grabbing that return value as a string and using it in a simple if…
Chris
  • 6,001
  • 8
  • 31
  • 56
1
2 3
99 100