Questions tagged [boolean-expression]

A boolean expression is an expression in a programming language that produces a boolean value when evaluated, i.e. one of true or false.

In computer science, a Boolean expression is an expression in a programming language that produces a Boolean value when evaluated, i.e. one of true or false. A Boolean expression may be composed of a combination of the Boolean constants true or false, Boolean-typed variables, Boolean-valued operators, and Boolean-valued functions.

1049 questions
3459
votes
104 answers

How can I convert a string to boolean in JavaScript?

Can I convert a string representing a boolean value (e.g., 'true', 'false') into a intrinsic type in JavaScript? I have a hidden form in HTML that is updated based upon a user's selection within a list. This form contains some fields which represent…
Kevin
  • 7,856
  • 11
  • 35
  • 40
994
votes
9 answers

Python `if x is not None` or `if not x is None`?

I've always thought of the if not x is None version to be more clear, but Google's style guide and PEP-8 both use if x is not None. Are there any minor performance differences (I'm assuming not), and is there any case where one really doesn't fit…
orokusaki
  • 55,146
  • 59
  • 179
  • 257
447
votes
8 answers

How to make "if not true condition"?

I would like to have the echo command executed when cat /etc/passwd | grep "sysa" is not true. What am I doing wrong? if ! [ $(cat /etc/passwd | grep "sysa") ]; then echo "ERROR - The user sysa could not be looked up" exit 2 fi
Sandra Schlichting
  • 25,050
  • 33
  • 110
  • 162
201
votes
8 answers

What is Truthy and Falsy? How is it different from True and False?

I just learned there are truthy and falsy values in python which are different from the normal True and False. Can someone please explain in depth what truthy and falsy values are? Where should I use them? What is the difference between truthy and…
user6932350
186
votes
8 answers

'and' (boolean) vs '&' (bitwise) - Why difference in behavior with lists vs numpy arrays?

What explains the difference in behavior of boolean and bitwise operations on lists vs NumPy arrays? I'm confused about the appropriate use of & vs and in Python, illustrated in the following examples. mylist1 = [True, True, True, False, …
rysqui
  • 2,779
  • 2
  • 19
  • 26
172
votes
8 answers

Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?

I am writing a security system that denies access to unauthorized users. name = input("Hello. Please enter your name: ") if name == "Kevin" or "Jon" or "Inbar": print("Access granted.") else: print("Access denied.") It grants access to…
Kevin
  • 74,910
  • 12
  • 133
  • 166
167
votes
8 answers

How to use OR condition in a JavaScript IF statement?

I understand that in JavaScript you can write: if (A && B) { do something } But how do I implement an OR such as: if (A OR B) { do something }
sadmicrowave
  • 39,964
  • 34
  • 108
  • 180
148
votes
4 answers

Why does `if None.__eq__("a")` seem to evaluate to True (but not quite)?

If you execute the following statement in Python 3.7, it will (from my testing) print b: if None.__eq__("a"): print("b") However, None.__eq__("a") evaluates to NotImplemented. Naturally, "a".__eq__("a") evaluates to True, and "b".__eq__("a")…
The AI Architect
  • 1,887
  • 2
  • 17
  • 24
133
votes
2 answers

Convert truthy or falsy to an explicit boolean, i.e. to True or False

I have a variable. Let's call it toto. This toto can be set to undefined, null, a string, or an object. I would like to check if toto is set to a data, which means set to a string or an object, and neither undefined nor null, and set corresponding…
Aracthor
  • 5,757
  • 6
  • 31
  • 59
123
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,624
  • 2
  • 11
  • 9
118
votes
8 answers

Priority of the logical operators (order of operations) for NOT, AND, OR in Python

As far as I know, in C & C++, the priority sequence for NOT AND & OR is NOT>AND>OR. But this doesn't seem to work in a similar way in Python. I tried searching for it in the Python documentation and failed (Guess I'm a little impatient.). Can…
96
votes
7 answers

Is this the proper way to do boolean test in SQL?

Assume active is a "boolean field" (tiny int, with 0 or 1) -- Find all active users select * from users where active -- Find all inactive users select * from users where NOT active In words, can the "NOT" operator be applied directly on the…
Eric
  • 1,858
  • 2
  • 16
  • 17
65
votes
15 answers

Merge lists that share common elements

My input is a list of lists. Some of them share common elements, eg. L = [['a','b','c'],['b','d','e'],['k'],['o','p'],['e','f'],['p','a'],['d','g']] I need to merge all lists, that share a common element, and repeat this procedure as long as there…
WlJs
  • 977
  • 1
  • 10
  • 12
57
votes
7 answers

What is "!!" in C?

I have encountered the following snippet: pt->aa[!!(ts->flags & MASK)] = -val; What does !! (double exclamation marks/ exclamation points/ two NOT operators) stand for in c? Doesn't (!!NULL) == NULL?
0x90
  • 39,472
  • 36
  • 165
  • 245
49
votes
9 answers

Boolean to integer conversion

I have 3 separate Boolean variables, bit1, bit2 & bit3 and I need to calculate the decimal integer equivalent in JavaScript?
user1661099
  • 591
  • 1
  • 4
  • 3
1
2 3
69 70