Linked Questions

3 votes
2 answers
10k views

Python XOR preference: bitwise operator vs. boolean operators [duplicate]

Is there a preferred method for doing a logical XOR in python? For example, if I have two variables a and b, and I want to check that at least one exists but not both, I have two methods: Method 1 (...
dizzyf's user avatar
  • 3,703
1 vote
5 answers
3k views

Given two non-zero integers, print "YES" if exactly one of them is positive and print "NO" otherwise (Python) [duplicate]

First time asking a question on stackoverflow. I am stuck trying to solve this. This is my code: a = int(input()) b = int(input()) Given two non-zero integers, print "YES" if exactly one of them is ...
Rich Spence's user avatar
2 votes
2 answers
1k views

Writing a Boolean Expression using Python [duplicate]

I am trying to write a boolean expression in Python, but it appears Python can only do XOR expressions with bit operations. What would be the best way to go about writing this expression in Python ...
Bob's user avatar
  • 756
-1 votes
1 answer
2k views

Python: Combine AND and NOT operators? [duplicate]

if (cond1 and cond2) or (not cond1 and not cond2): Is there a simpler way to write this in Python?
Dova's user avatar
  • 310
0 votes
1 answer
2k views

How to check if exactly one of two condition is met [duplicate]

I am checking wether one of two conditions applies and only one. My question is if there is a way to have a single if statement to do this: if x == True: if y == False: do_something() elif ...
klnstngr's user avatar
-2 votes
3 answers
2k views

I want to take the XOR of all the elements of 1 list with another. How do I do it? [duplicate]

I have a bunch of lists in the form of say [0,0,1,0,1...], and I want to take the XOR of 2 lists and give the output as a list. Like: [ 0, 0, 1 ] XOR [ 0, 1, 0 ] -> [ 0, 1, 1 ] res = [] tmp = [] for ...
Ankit Billa's user avatar
-1 votes
1 answer
313 views

How to prevent Python script from printing even though conditions to print are True? [duplicate]

I want my Python script to print a list only if one condition is true between two separate conditions. However, it is possible for both conditions to be true at the same time. Below is the portion of ...
Marshall's user avatar
0 votes
2 answers
102 views

What does the ^ character do in logical operation [duplicate]

I'm practicing code on codingbat and came across this logic question: Given a number n, return True if n is in the range 1..10, inclusive. Unless outside_mode is True, in which case return True if ...
zelfde's user avatar
  • 330
0 votes
2 answers
110 views

Python - Conflict with Regex and Exclusive or Operator [duplicate]

I'm importing regex for a word count in a string, and I'm trying to use the ^ operator. Although, the compiler is recognizing the ^ as part of regex, and not the exclusive or operator. This is my code:...
Nathan Chan's user avatar
415 votes
11 answers
1.9m views

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Let x be a NumPy array. The following: (x > 1) and (x < 3) Gives the error message: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() How ...
Homunculus Reticulli's user avatar
142 votes
15 answers
293k views

Python syntax for "if a or b or c but not all of them"

I have a python script that can receive either zero or three command line arguments. (Either it runs on default behavior or needs all three values specified.) What's the ideal syntax for something ...
Chris Wilson's user avatar
  • 6,719
168 votes
16 answers
151k views

How to toggle a value?

What is the most efficient way to toggle between 0 and 1?
user avatar
45 votes
9 answers
120k views

Use of True, False, and None as return values in Python functions

I think that I fully understand this, but I just want to make sure since I keep seeing people say to never ever test against True, False, or None. They suggest that routines should raise an error ...
Vorticity's user avatar
  • 4,946
20 votes
5 answers
11k views

Check if only one variable in a list of variables is set

I'm looking for a simple method to check if only one variable in a list of variables has a True value. I've looked at this logical xor post and is trying to find a way to adapt to multiple variables ...
Deon's user avatar
  • 3,473
12 votes
4 answers
27k views

Python conditional one or the other but not both

I am trying to compile an if statement in python where it checks two variables to see if they are <= .05. Now if both variables are True, I just want the code to pass/continue, but if only one of ...
TsvGis's user avatar
  • 632

15 30 50 per page