Python “in” and “==” confusion











up vote
5
down vote

favorite












print('a' in 'aa')
print('a' in 'aa' == True)
print(('a' in 'aa') == True)
print('a' in ('aa' == True))


The output is



True
False
True
Traceback (most recent call last):
File "main.py", line 6, in <module>
print('a' in ('aa' == True))
TypeError: argument of type 'bool' is not iterable


If line 2 is neither line 3 nor line 4, then what is it? How does it get False?










share|improve this question






















  • @PatrickArtner I'm finding it unusually difficult to find the canonical I'm thinking of. It's due to chaining but I can't find the link :/
    – roganjosh
    Nov 21 at 18:23










  • That's the one you originally flagged with but I'm sure there is a canonical. Where is it?! :P
    – roganjosh
    Nov 21 at 18:25










  • @roganjosh this here? stackoverflow.com/questions/6074018/…
    – Patrick Artner
    Nov 21 at 18:27








  • 1




    @PatrickArtner it got hammered and they presumably decided to reverse that, but now it shows no close votes to me.
    – roganjosh
    Nov 21 at 18:42








  • 1




    @PatrickArtner i saw timgeb as the second closer. It must be a hammer. I'm not sure how to view that history, though.
    – roganjosh
    Nov 21 at 18:46















up vote
5
down vote

favorite












print('a' in 'aa')
print('a' in 'aa' == True)
print(('a' in 'aa') == True)
print('a' in ('aa' == True))


The output is



True
False
True
Traceback (most recent call last):
File "main.py", line 6, in <module>
print('a' in ('aa' == True))
TypeError: argument of type 'bool' is not iterable


If line 2 is neither line 3 nor line 4, then what is it? How does it get False?










share|improve this question






















  • @PatrickArtner I'm finding it unusually difficult to find the canonical I'm thinking of. It's due to chaining but I can't find the link :/
    – roganjosh
    Nov 21 at 18:23










  • That's the one you originally flagged with but I'm sure there is a canonical. Where is it?! :P
    – roganjosh
    Nov 21 at 18:25










  • @roganjosh this here? stackoverflow.com/questions/6074018/…
    – Patrick Artner
    Nov 21 at 18:27








  • 1




    @PatrickArtner it got hammered and they presumably decided to reverse that, but now it shows no close votes to me.
    – roganjosh
    Nov 21 at 18:42








  • 1




    @PatrickArtner i saw timgeb as the second closer. It must be a hammer. I'm not sure how to view that history, though.
    – roganjosh
    Nov 21 at 18:46













up vote
5
down vote

favorite









up vote
5
down vote

favorite











print('a' in 'aa')
print('a' in 'aa' == True)
print(('a' in 'aa') == True)
print('a' in ('aa' == True))


The output is



True
False
True
Traceback (most recent call last):
File "main.py", line 6, in <module>
print('a' in ('aa' == True))
TypeError: argument of type 'bool' is not iterable


If line 2 is neither line 3 nor line 4, then what is it? How does it get False?










share|improve this question













print('a' in 'aa')
print('a' in 'aa' == True)
print(('a' in 'aa') == True)
print('a' in ('aa' == True))


The output is



True
False
True
Traceback (most recent call last):
File "main.py", line 6, in <module>
print('a' in ('aa' == True))
TypeError: argument of type 'bool' is not iterable


If line 2 is neither line 3 nor line 4, then what is it? How does it get False?







python






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 at 18:13









pete

7112827




7112827












  • @PatrickArtner I'm finding it unusually difficult to find the canonical I'm thinking of. It's due to chaining but I can't find the link :/
    – roganjosh
    Nov 21 at 18:23










  • That's the one you originally flagged with but I'm sure there is a canonical. Where is it?! :P
    – roganjosh
    Nov 21 at 18:25










  • @roganjosh this here? stackoverflow.com/questions/6074018/…
    – Patrick Artner
    Nov 21 at 18:27








  • 1




    @PatrickArtner it got hammered and they presumably decided to reverse that, but now it shows no close votes to me.
    – roganjosh
    Nov 21 at 18:42








  • 1




    @PatrickArtner i saw timgeb as the second closer. It must be a hammer. I'm not sure how to view that history, though.
    – roganjosh
    Nov 21 at 18:46


















  • @PatrickArtner I'm finding it unusually difficult to find the canonical I'm thinking of. It's due to chaining but I can't find the link :/
    – roganjosh
    Nov 21 at 18:23










  • That's the one you originally flagged with but I'm sure there is a canonical. Where is it?! :P
    – roganjosh
    Nov 21 at 18:25










  • @roganjosh this here? stackoverflow.com/questions/6074018/…
    – Patrick Artner
    Nov 21 at 18:27








  • 1




    @PatrickArtner it got hammered and they presumably decided to reverse that, but now it shows no close votes to me.
    – roganjosh
    Nov 21 at 18:42








  • 1




    @PatrickArtner i saw timgeb as the second closer. It must be a hammer. I'm not sure how to view that history, though.
    – roganjosh
    Nov 21 at 18:46
















@PatrickArtner I'm finding it unusually difficult to find the canonical I'm thinking of. It's due to chaining but I can't find the link :/
– roganjosh
Nov 21 at 18:23




@PatrickArtner I'm finding it unusually difficult to find the canonical I'm thinking of. It's due to chaining but I can't find the link :/
– roganjosh
Nov 21 at 18:23












That's the one you originally flagged with but I'm sure there is a canonical. Where is it?! :P
– roganjosh
Nov 21 at 18:25




That's the one you originally flagged with but I'm sure there is a canonical. Where is it?! :P
– roganjosh
Nov 21 at 18:25












@roganjosh this here? stackoverflow.com/questions/6074018/…
– Patrick Artner
Nov 21 at 18:27






@roganjosh this here? stackoverflow.com/questions/6074018/…
– Patrick Artner
Nov 21 at 18:27






1




1




@PatrickArtner it got hammered and they presumably decided to reverse that, but now it shows no close votes to me.
– roganjosh
Nov 21 at 18:42






@PatrickArtner it got hammered and they presumably decided to reverse that, but now it shows no close votes to me.
– roganjosh
Nov 21 at 18:42






1




1




@PatrickArtner i saw timgeb as the second closer. It must be a hammer. I'm not sure how to view that history, though.
– roganjosh
Nov 21 at 18:46




@PatrickArtner i saw timgeb as the second closer. It must be a hammer. I'm not sure how to view that history, though.
– roganjosh
Nov 21 at 18:46












2 Answers
2






active

oldest

votes

















up vote
7
down vote



accepted










According to Expressions



print('a' in 'aa' == True)


is evaluated as



'a' in 'aa' and 'aa' == True


which is False.



See



print("a" in "aa" and "aa" == True)


==> False



The rest is trivial - it helps to keep operator precedence in mind to figure them out.





Similar ones:




  • Multiple comparison operators in single statement

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


with different statements. I flagged for dupe but the UI is wonky - I answered non the less to explain why yours exactly printed what it did.






share|improve this answer






























    up vote
    1
    down vote













    Case 1 : it's simple the answers is True.



    print('a' in 'aa')


    Case 2 : This operation is evaluated as 'a' in 'aa' and 'aa' == True, so obviously it will return false.



    print('a' in 'aa' == True)


    Case 3: Now because we have () enclosing ('a' in 'aa') and the precedence of () is highest among all so first 'a' in 'aa' is evaluated as True and then True == True



    print(('a' in 'aa') == True)


    Case 4 : Same as above because of precedence of (), its evaluated as 'aa' == True, which will result in error as it tries to apply in on a non iterable that is bool value.



    print('a' in ('aa' == True))





    share|improve this answer



















    • 1




      comparing a string with a bool is perfectly fine - the error is trying to use in on a non iterable bool type ...
      – Patrick Artner
      Nov 21 at 18:32












    • Comparing a string to a Boolean does not throw an error.
      – roganjosh
      Nov 21 at 18:32










    • My bad, I put it in wrong words.
      – Sanchit Kumar
      Nov 21 at 18:33













    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53418231%2fpython-in-and-confusion%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    7
    down vote



    accepted










    According to Expressions



    print('a' in 'aa' == True)


    is evaluated as



    'a' in 'aa' and 'aa' == True


    which is False.



    See



    print("a" in "aa" and "aa" == True)


    ==> False



    The rest is trivial - it helps to keep operator precedence in mind to figure them out.





    Similar ones:




    • Multiple comparison operators in single statement

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


    with different statements. I flagged for dupe but the UI is wonky - I answered non the less to explain why yours exactly printed what it did.






    share|improve this answer



























      up vote
      7
      down vote



      accepted










      According to Expressions



      print('a' in 'aa' == True)


      is evaluated as



      'a' in 'aa' and 'aa' == True


      which is False.



      See



      print("a" in "aa" and "aa" == True)


      ==> False



      The rest is trivial - it helps to keep operator precedence in mind to figure them out.





      Similar ones:




      • Multiple comparison operators in single statement

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


      with different statements. I flagged for dupe but the UI is wonky - I answered non the less to explain why yours exactly printed what it did.






      share|improve this answer

























        up vote
        7
        down vote



        accepted







        up vote
        7
        down vote



        accepted






        According to Expressions



        print('a' in 'aa' == True)


        is evaluated as



        'a' in 'aa' and 'aa' == True


        which is False.



        See



        print("a" in "aa" and "aa" == True)


        ==> False



        The rest is trivial - it helps to keep operator precedence in mind to figure them out.





        Similar ones:




        • Multiple comparison operators in single statement

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


        with different statements. I flagged for dupe but the UI is wonky - I answered non the less to explain why yours exactly printed what it did.






        share|improve this answer














        According to Expressions



        print('a' in 'aa' == True)


        is evaluated as



        'a' in 'aa' and 'aa' == True


        which is False.



        See



        print("a" in "aa" and "aa" == True)


        ==> False



        The rest is trivial - it helps to keep operator precedence in mind to figure them out.





        Similar ones:




        • Multiple comparison operators in single statement

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


        with different statements. I flagged for dupe but the UI is wonky - I answered non the less to explain why yours exactly printed what it did.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 21 at 18:42

























        answered Nov 21 at 18:23









        Patrick Artner

        18.9k51940




        18.9k51940
























            up vote
            1
            down vote













            Case 1 : it's simple the answers is True.



            print('a' in 'aa')


            Case 2 : This operation is evaluated as 'a' in 'aa' and 'aa' == True, so obviously it will return false.



            print('a' in 'aa' == True)


            Case 3: Now because we have () enclosing ('a' in 'aa') and the precedence of () is highest among all so first 'a' in 'aa' is evaluated as True and then True == True



            print(('a' in 'aa') == True)


            Case 4 : Same as above because of precedence of (), its evaluated as 'aa' == True, which will result in error as it tries to apply in on a non iterable that is bool value.



            print('a' in ('aa' == True))





            share|improve this answer



















            • 1




              comparing a string with a bool is perfectly fine - the error is trying to use in on a non iterable bool type ...
              – Patrick Artner
              Nov 21 at 18:32












            • Comparing a string to a Boolean does not throw an error.
              – roganjosh
              Nov 21 at 18:32










            • My bad, I put it in wrong words.
              – Sanchit Kumar
              Nov 21 at 18:33

















            up vote
            1
            down vote













            Case 1 : it's simple the answers is True.



            print('a' in 'aa')


            Case 2 : This operation is evaluated as 'a' in 'aa' and 'aa' == True, so obviously it will return false.



            print('a' in 'aa' == True)


            Case 3: Now because we have () enclosing ('a' in 'aa') and the precedence of () is highest among all so first 'a' in 'aa' is evaluated as True and then True == True



            print(('a' in 'aa') == True)


            Case 4 : Same as above because of precedence of (), its evaluated as 'aa' == True, which will result in error as it tries to apply in on a non iterable that is bool value.



            print('a' in ('aa' == True))





            share|improve this answer



















            • 1




              comparing a string with a bool is perfectly fine - the error is trying to use in on a non iterable bool type ...
              – Patrick Artner
              Nov 21 at 18:32












            • Comparing a string to a Boolean does not throw an error.
              – roganjosh
              Nov 21 at 18:32










            • My bad, I put it in wrong words.
              – Sanchit Kumar
              Nov 21 at 18:33















            up vote
            1
            down vote










            up vote
            1
            down vote









            Case 1 : it's simple the answers is True.



            print('a' in 'aa')


            Case 2 : This operation is evaluated as 'a' in 'aa' and 'aa' == True, so obviously it will return false.



            print('a' in 'aa' == True)


            Case 3: Now because we have () enclosing ('a' in 'aa') and the precedence of () is highest among all so first 'a' in 'aa' is evaluated as True and then True == True



            print(('a' in 'aa') == True)


            Case 4 : Same as above because of precedence of (), its evaluated as 'aa' == True, which will result in error as it tries to apply in on a non iterable that is bool value.



            print('a' in ('aa' == True))





            share|improve this answer














            Case 1 : it's simple the answers is True.



            print('a' in 'aa')


            Case 2 : This operation is evaluated as 'a' in 'aa' and 'aa' == True, so obviously it will return false.



            print('a' in 'aa' == True)


            Case 3: Now because we have () enclosing ('a' in 'aa') and the precedence of () is highest among all so first 'a' in 'aa' is evaluated as True and then True == True



            print(('a' in 'aa') == True)


            Case 4 : Same as above because of precedence of (), its evaluated as 'aa' == True, which will result in error as it tries to apply in on a non iterable that is bool value.



            print('a' in ('aa' == True))






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 21 at 18:35

























            answered Nov 21 at 18:31









            Sanchit Kumar

            31117




            31117








            • 1




              comparing a string with a bool is perfectly fine - the error is trying to use in on a non iterable bool type ...
              – Patrick Artner
              Nov 21 at 18:32












            • Comparing a string to a Boolean does not throw an error.
              – roganjosh
              Nov 21 at 18:32










            • My bad, I put it in wrong words.
              – Sanchit Kumar
              Nov 21 at 18:33
















            • 1




              comparing a string with a bool is perfectly fine - the error is trying to use in on a non iterable bool type ...
              – Patrick Artner
              Nov 21 at 18:32












            • Comparing a string to a Boolean does not throw an error.
              – roganjosh
              Nov 21 at 18:32










            • My bad, I put it in wrong words.
              – Sanchit Kumar
              Nov 21 at 18:33










            1




            1




            comparing a string with a bool is perfectly fine - the error is trying to use in on a non iterable bool type ...
            – Patrick Artner
            Nov 21 at 18:32






            comparing a string with a bool is perfectly fine - the error is trying to use in on a non iterable bool type ...
            – Patrick Artner
            Nov 21 at 18:32














            Comparing a string to a Boolean does not throw an error.
            – roganjosh
            Nov 21 at 18:32




            Comparing a string to a Boolean does not throw an error.
            – roganjosh
            Nov 21 at 18:32












            My bad, I put it in wrong words.
            – Sanchit Kumar
            Nov 21 at 18:33






            My bad, I put it in wrong words.
            – Sanchit Kumar
            Nov 21 at 18:33




















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53418231%2fpython-in-and-confusion%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Lallio

            Unable to find Lightning Node

            Futebolista