Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 str in str == True
#1
Why do my 3rd and 4th lines give different results? 3 prints True, 4 prints False.

Thanks!

blue = "lm"
my_size = "l"
print(my_size in blue)
print(my_size in blue == True)
Quote
#2
line 4 is equal to
print(my_size in (blue == True))
which is
print(my_size in False)


EDIT:
Obviously I was wrong - see at @micseydel's post below
Quote
#3
buran, I love you buddy but that's not true.
>>> print('l' in 'lb' == True)
False
>>> print('l' in ('lb' == True))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: argument of type 'bool' is not iterable
>>> print(('l' in 'lb') == True)
True
I'm actually baffled as to how this is returning False. I plan to look into it a little bit more, expect a followup...
Feel like you're not getting the answers you want? Checkout the help/rules for things like what to include/not include in a post, how to use code tags, how to ask smart questions, and more.

Pro-tip - there's an inverse correlation between the number of lines of code posted and my enthusiasm for helping with a question :)
Quote
#4
Haven't had a chance yet to review this but my very smart roommate said something I imagine is helpful:
Mikel Wrote:According to dis.dis(lambda: 'a' in 'ab' == True), ('a' in 'ab' == True) is equivalent to ('a' in 'ab' and 'ab' == True)
I think I know why: comparisons in Python are allowed to written similar to what we do naturally
So x < y < z is valid and equivalent to x < y and y < z
The 'in' operator is treated equivalently to '<', '==', etc.
So x in y == z is equivalent to x in y and y == z
Mystery solved
Feel like you're not getting the answers you want? Checkout the help/rules for things like what to include/not include in a post, how to use code tags, how to ask smart questions, and more.

Pro-tip - there's an inverse correlation between the number of lines of code posted and my enthusiasm for helping with a question :)
Quote
#5
Still need to read that but in the mean time I wanted suggest a solution - just use the first form of the two. The second form gains you nothing, it's generally seen as newby in Python to compare to a boolean (since any expression can be treated as a boolean in most important situations, the shorter code doesn't have any disadvantage).
Feel like you're not getting the answers you want? Checkout the help/rules for things like what to include/not include in a post, how to use code tags, how to ask smart questions, and more.

Pro-tip - there's an inverse correlation between the number of lines of code posted and my enthusiasm for helping with a question :)
Quote
#6
Ok yeah it makes sense now. Bummer that that Python feature has such a downside, since I'm surprised by the result of the expression you provided and definitely wouldn't have expected it. Trade-offs, I guess.
Feel like you're not getting the answers you want? Checkout the help/rules for things like what to include/not include in a post, how to use code tags, how to ask smart questions, and more.

Pro-tip - there's an inverse correlation between the number of lines of code posted and my enthusiasm for helping with a question :)
Quote
#7
(Mar-26-2019, 11:25 PM)micseydel Wrote: buran, I love you buddy but that's not true.
I admit this one of the rare times when I post without actually run the code I post Blush Angry
micseydel likes this post
Quote
#8
Yes Mikel is right,can look a little at grouping of this.
So i looked a little into comparison operator chaining.
So my understand of this is.
>>> blue = "lm"
>>> my_size = "l"
>>> my_size in blue
True
>>> my_size in blue == True
False

>>> # Chaining (this is what comparison operators do) left to right
>>> (my_size in blue) and (blue == True)
False
>>> # So it is 
>>> True and False
False
If look at grouping for non-comparison operators.
>>> 7 - 1 + 4
10 
>>> # Means
>>> (7 - 1) + 4
10
>>> # Grouping right to left would give a different result
>>> 7 - (1 + 4)
2 
One more with comparison operator chaining
a < b < c 
# Grouping is
(a < b) and (b < c)
The other way around:
>>> blue = "lm"
>>> my_size = "a"
>>> my_size in blue
False
>>> # Both are False 
>>> my_size in blue == True
False
>>> my_size in blue == False
False

>>> # Comparison operator chaining
>>> (my_size in blue) and (blue == True)
False
>>> # So if first grouping is False,then always return False
>>> (my_size in blue)
False
>>> # Then second group is not looked at all,as first group is False
>>> (blue == True)
False

# So we never get to this
>>> False == False
True
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Returning True or False vs. True or None trevorkavanaugh 6 485 Apr-04-2019, 08:42 AM
Last Post: DeaD_EyE

Forum Jump:


Users browsing this thread: 1 Guest(s)