I’m researching the differences between Python 2 and Python 3. It’s been mentioned that string to list comparisons do not work in Python 3 as they’re of limited value. I’m trying to find an example of this. Here’s what I’ve tried:
Python 2.7.18 (default, Jun 23 2024, 08:39:49)
[GCC Apple LLVM 15.0.0 (clang-1500.3.9.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> mylist = ['a', 'b', 'c']
>>> print 'abc' == mylist
False
>>>
Shouldn’t ^^^ return True because 3 == 3?
Python 3.12.0 (main, Nov 29 2023, 07:31:13) [Clang 15.0.0 (clang-1500.0.40.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> mylist = ['a', 'b', 'c']
>>> print('abc' == mylist)
False
I was expecting Python 3 to get mad at me for this?
Python 3.12.0 (main, Nov 29 2023, 07:31:13) [Clang 15.0.0 (clang-1500.0.40.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> mylist = ['a', 'b', 'c']
>>> print('abc' >= mylist)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: '>=' not supported between instances of 'str' and 'list
Ok that seems right but why is ==
supported?
New contributor
jsaintrocc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.