Linked Questions

3 votes
2 answers
10k views

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,723
1 vote
5 answers
3k views

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

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

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

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 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
320 views

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
114 views

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
114 views

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
421 votes
11 answers
1.9m views

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
295k views

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,729
168 votes
16 answers
153k views

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

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,976
20 votes
5 answers
11k views

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,493
12 votes
4 answers
27k views

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