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...
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
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).
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.
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
  if cond1 or cond2 or ...condN is not True asheru93 4 158 Jun-03-2019, 04:24 PM
Last Post: buran
  Returning True or False vs. True or None trevorkavanaugh 6 256 Apr-04-2019, 08:42 AM
Last Post: DeaD_EyE
  file.write stops while true loop from executing in python3 boonr 3 187 Mar-25-2019, 12:50 PM
Last Post: ichabod801
  'True' if 'string' has at least one uppercase letter ClassicalSoul 1 158 Feb-28-2019, 12:08 PM
Last Post: buran
  How to make loop go back to the beginning if test evaluaes to True. FWendeburg 1 201 Feb-13-2019, 01:26 AM
Last Post: stullis
  os.path.isdir(<whatever>) returns always True Fabrizio 3 436 Dec-04-2018, 04:28 PM
Last Post: nilamo
  Returning true or false in a for loop bbop1232012 3 316 Nov-22-2018, 04:44 PM
Last Post: bbop1232012
  True == not False Skaperen 6 588 Aug-23-2018, 10:26 AM
Last Post: DeaD_EyE
  saving a true/false of a single value Skaperen 3 429 Aug-20-2018, 02:31 AM
Last Post: ichabod801
  Get True of false outside a loop morgandebray 2 357 Aug-09-2018, 12:39 PM
Last Post: morgandebray

Forum Jump:


Users browsing this thread: 1 Guest(s)