comparison-operators

What happens "behind the scenes" if I call `None == x` in Python?

What happens "behind the scenes" if I call `None == x` in Python? Question: I am learning and playing around with Python and I came up with the following test code (please be aware that I would not write productive code like that, but when learning new languages I like to play around with the …

Total answers: 2

Can a string and int comparison be ever True in python2.7

Can a string and int comparison be ever True in python2.7 Question: I know python2.7 allows comparison between different types. So the equality comparison between str and int will result in False. Can the comparison ever be true depending on the values of a and b? a = input(‘we input some string value a’) if …

Total answers: 2

Sympy – Comparing expressions

Sympy – Comparing expressions Question: Is there a way to check if two expressions are mathematically equal? I expected tg(x)cos(x) == sin(x) to output True, but it outputs False. Is there a way to make such comparisons with sympy? Another example is (a+b)**2 == a**2 + 2*a*b + b**2 which surprisingly also outputs False. I …

Total answers: 5

Why does "not(True) in [False, True]" return False?

Why does "not(True) in [False, True]" return False? Question: If I do this: >>> False in [False, True] True That returns True. Simply because False is in the list. But if I do: >>> not(True) in [False, True] False That returns False. Whereas not(True) is equal to False: >>> not(True) False Why? Asked By: Texom512 …

Total answers: 8

What is the pythonic way to test whether two objects are the same, not just equal

What is the pythonic way to test whether two objects are the same, not just equal Question: My solution has been to make the object’s address into a string, which should always be unique. Below is a case where it works both ways, comparing the objects and comparing the values of str(object_a) and str(object_b). >>> …

Total answers: 2

How to check if the string is empty?

How to check if the string is empty? Question: Does Python have something like an empty string variable where you can do: if myString == string.empty: Regardless, what’s the most elegant way to check for empty string values? I find hard coding “” every time for checking an empty string not as good. Asked By: …

Total answers: 25

Why is 'True == not False' a SyntaxError?

Why is 'True == not False' a SyntaxError? Question: Comparing boolean values with == works in Python. But when I apply the boolean not operator, the result is a syntax error: Python 2.7 (r27:82500, Sep 16 2010, 18:02:00) [GCC 4.5.1 20100907 (Red Hat 4.5.1-3)] on linux2 Type “help”, “copyright”, “credits” or “license” for more information. …

Total answers: 5

Why does the expression 0 < 0 == 0 return False in Python?

Why does the expression 0 < 0 == 0 return False in Python? Question: Looking into Queue.py in Python 2.6, I found this construct that I found a bit strange: def full(self): “””Return True if the queue is full, False otherwise (not reliable!).””” self.mutex.acquire() n = 0 < self.maxsize == self._qsize() self.mutex.release() return n If …

Total answers: 9