What does sum(1 for c in sentence if c.isupper())) mean in non programming terms











up vote
1
down vote

favorite












I need to count the amount of upper-case letters in a user entered sentence.



When I search Google for a solution, I came across the command sum(1 for c in sentence if c.isupper())).



I used it and it works but I also need to explain the code to my teacher.



How would I go about doing this?










share|improve this question









New contributor




DavidP75 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Be specific about what you don't understand. As it stands your question is too broad.
    – kenlukas
    Nov 19 at 14:04










  • You could also use sum(c.isupper() for c in sentence). This works because True has a numeric value of 1 and False has a numeric value of 0. So you convert the characters into ones and zeroes based on whether or not they are capital, then sum that sequence.
    – Patrick Haugh
    Nov 19 at 14:18






  • 1




    So you have an assignment, for which you did not write the code yourself, and you are asking for help convincing your teacher you did write it yourself?
    – chepner
    Nov 19 at 14:20















up vote
1
down vote

favorite












I need to count the amount of upper-case letters in a user entered sentence.



When I search Google for a solution, I came across the command sum(1 for c in sentence if c.isupper())).



I used it and it works but I also need to explain the code to my teacher.



How would I go about doing this?










share|improve this question









New contributor




DavidP75 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Be specific about what you don't understand. As it stands your question is too broad.
    – kenlukas
    Nov 19 at 14:04










  • You could also use sum(c.isupper() for c in sentence). This works because True has a numeric value of 1 and False has a numeric value of 0. So you convert the characters into ones and zeroes based on whether or not they are capital, then sum that sequence.
    – Patrick Haugh
    Nov 19 at 14:18






  • 1




    So you have an assignment, for which you did not write the code yourself, and you are asking for help convincing your teacher you did write it yourself?
    – chepner
    Nov 19 at 14:20













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I need to count the amount of upper-case letters in a user entered sentence.



When I search Google for a solution, I came across the command sum(1 for c in sentence if c.isupper())).



I used it and it works but I also need to explain the code to my teacher.



How would I go about doing this?










share|improve this question









New contributor




DavidP75 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I need to count the amount of upper-case letters in a user entered sentence.



When I search Google for a solution, I came across the command sum(1 for c in sentence if c.isupper())).



I used it and it works but I also need to explain the code to my teacher.



How would I go about doing this?







python string python-3.x command






share|improve this question









New contributor




DavidP75 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




DavidP75 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Nov 19 at 14:15









J100

854312




854312






New contributor




DavidP75 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 19 at 13:54









DavidP75

61




61




New contributor




DavidP75 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





DavidP75 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






DavidP75 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • Be specific about what you don't understand. As it stands your question is too broad.
    – kenlukas
    Nov 19 at 14:04










  • You could also use sum(c.isupper() for c in sentence). This works because True has a numeric value of 1 and False has a numeric value of 0. So you convert the characters into ones and zeroes based on whether or not they are capital, then sum that sequence.
    – Patrick Haugh
    Nov 19 at 14:18






  • 1




    So you have an assignment, for which you did not write the code yourself, and you are asking for help convincing your teacher you did write it yourself?
    – chepner
    Nov 19 at 14:20


















  • Be specific about what you don't understand. As it stands your question is too broad.
    – kenlukas
    Nov 19 at 14:04










  • You could also use sum(c.isupper() for c in sentence). This works because True has a numeric value of 1 and False has a numeric value of 0. So you convert the characters into ones and zeroes based on whether or not they are capital, then sum that sequence.
    – Patrick Haugh
    Nov 19 at 14:18






  • 1




    So you have an assignment, for which you did not write the code yourself, and you are asking for help convincing your teacher you did write it yourself?
    – chepner
    Nov 19 at 14:20
















Be specific about what you don't understand. As it stands your question is too broad.
– kenlukas
Nov 19 at 14:04




Be specific about what you don't understand. As it stands your question is too broad.
– kenlukas
Nov 19 at 14:04












You could also use sum(c.isupper() for c in sentence). This works because True has a numeric value of 1 and False has a numeric value of 0. So you convert the characters into ones and zeroes based on whether or not they are capital, then sum that sequence.
– Patrick Haugh
Nov 19 at 14:18




You could also use sum(c.isupper() for c in sentence). This works because True has a numeric value of 1 and False has a numeric value of 0. So you convert the characters into ones and zeroes based on whether or not they are capital, then sum that sequence.
– Patrick Haugh
Nov 19 at 14:18




1




1




So you have an assignment, for which you did not write the code yourself, and you are asking for help convincing your teacher you did write it yourself?
– chepner
Nov 19 at 14:20




So you have an assignment, for which you did not write the code yourself, and you are asking for help convincing your teacher you did write it yourself?
– chepner
Nov 19 at 14:20












5 Answers
5






active

oldest

votes

















up vote
4
down vote













So for each "c in sentence" (i.e., step through each letter in sentence, making each equal to c for the duration of the loop), substitute in a 1, ("1 for c in sentence"), but only if "c" (the current character) is an uppercase character ("if c.isupper()").



Then, sum (add together) the total number of 1's that were produced (one for each upper case character in the sentence), resulting in the total count of upper case characters.






share|improve this answer





















  • Nice. I would also add that sum() in the OPs case is a generator expression.
    – RoadRunner
    Nov 19 at 14:09




















up vote
3
down vote













Try rewriting in this way, instead of using list inclusions:



count =0
for c in sentence:
if c.isupper():
count+=1
print(count)


That way, there won't be much to explain!






share|improve this answer























  • Can't go wrong with a simple loop. Also note that it seems that the OP is using Python 3, and print needs parentheses.
    – RoadRunner
    Nov 19 at 14:11






  • 1




    Thanks, fixed it
    – Matina G
    Nov 19 at 14:12


















up vote
0
down vote













Your code is basically a loop that is placed inside the sum function. The loop checks every letter in turn and adds a 1 to the total if it finds a capital letter. I.e if the isupper() function returns True.



sentence = "Hi My Name is"
a = 0

for c in sentence:
if c.isupper():
a = a + 1
else:
pass

print(a, " capital letters")

b = sum(1 for c in sentence if c.isupper())

print(b, " capital letters")


~






share|improve this answer








New contributor




jandor is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    up vote
    0
    down vote













    Try to decomposite "sum(1 for c in sentence if c.isupper())":



    1) 1 for c in sentence if c.isupper()



    It means: "generate 1 if symbol if c is uppercase".



    Also, note that (1 for c in sentence if c.isupper()) is a generator
    But because it placed as a function positional argument it can be placed there without parentheses.



    2) sum(...) -- summarize all "1" generated from step 1)



    sum expect iterable as an argument: list, tuple, generator or another object that support iterator protocol






    share|improve this answer






























      up vote
      0
      down vote













      sum(1 for i in iterable )


      will add 1 for each member of iterable. look at this :



      sum(1 for i in [True,True,True]) 


      will return 3



      sum(10 for i in [True,True,True])


      will return 30



      so for each iteration sum will add the number to itself.
      the "for c in sentence if c.isupper()" return a list of "True"s so for any character that is uppercase sum will add 1.






      share|improve this answer





















        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
        });


        }
        });






        DavidP75 is a new contributor. Be nice, and check out our Code of Conduct.










         

        draft saved


        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53376146%2fwhat-does-sum1-for-c-in-sentence-if-c-isupper-mean-in-non-programming-terms%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        5 Answers
        5






        active

        oldest

        votes








        5 Answers
        5






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        4
        down vote













        So for each "c in sentence" (i.e., step through each letter in sentence, making each equal to c for the duration of the loop), substitute in a 1, ("1 for c in sentence"), but only if "c" (the current character) is an uppercase character ("if c.isupper()").



        Then, sum (add together) the total number of 1's that were produced (one for each upper case character in the sentence), resulting in the total count of upper case characters.






        share|improve this answer





















        • Nice. I would also add that sum() in the OPs case is a generator expression.
          – RoadRunner
          Nov 19 at 14:09

















        up vote
        4
        down vote













        So for each "c in sentence" (i.e., step through each letter in sentence, making each equal to c for the duration of the loop), substitute in a 1, ("1 for c in sentence"), but only if "c" (the current character) is an uppercase character ("if c.isupper()").



        Then, sum (add together) the total number of 1's that were produced (one for each upper case character in the sentence), resulting in the total count of upper case characters.






        share|improve this answer





















        • Nice. I would also add that sum() in the OPs case is a generator expression.
          – RoadRunner
          Nov 19 at 14:09















        up vote
        4
        down vote










        up vote
        4
        down vote









        So for each "c in sentence" (i.e., step through each letter in sentence, making each equal to c for the duration of the loop), substitute in a 1, ("1 for c in sentence"), but only if "c" (the current character) is an uppercase character ("if c.isupper()").



        Then, sum (add together) the total number of 1's that were produced (one for each upper case character in the sentence), resulting in the total count of upper case characters.






        share|improve this answer












        So for each "c in sentence" (i.e., step through each letter in sentence, making each equal to c for the duration of the loop), substitute in a 1, ("1 for c in sentence"), but only if "c" (the current character) is an uppercase character ("if c.isupper()").



        Then, sum (add together) the total number of 1's that were produced (one for each upper case character in the sentence), resulting in the total count of upper case characters.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 at 13:58









        OperatorOverload

        409312




        409312












        • Nice. I would also add that sum() in the OPs case is a generator expression.
          – RoadRunner
          Nov 19 at 14:09




















        • Nice. I would also add that sum() in the OPs case is a generator expression.
          – RoadRunner
          Nov 19 at 14:09


















        Nice. I would also add that sum() in the OPs case is a generator expression.
        – RoadRunner
        Nov 19 at 14:09






        Nice. I would also add that sum() in the OPs case is a generator expression.
        – RoadRunner
        Nov 19 at 14:09














        up vote
        3
        down vote













        Try rewriting in this way, instead of using list inclusions:



        count =0
        for c in sentence:
        if c.isupper():
        count+=1
        print(count)


        That way, there won't be much to explain!






        share|improve this answer























        • Can't go wrong with a simple loop. Also note that it seems that the OP is using Python 3, and print needs parentheses.
          – RoadRunner
          Nov 19 at 14:11






        • 1




          Thanks, fixed it
          – Matina G
          Nov 19 at 14:12















        up vote
        3
        down vote













        Try rewriting in this way, instead of using list inclusions:



        count =0
        for c in sentence:
        if c.isupper():
        count+=1
        print(count)


        That way, there won't be much to explain!






        share|improve this answer























        • Can't go wrong with a simple loop. Also note that it seems that the OP is using Python 3, and print needs parentheses.
          – RoadRunner
          Nov 19 at 14:11






        • 1




          Thanks, fixed it
          – Matina G
          Nov 19 at 14:12













        up vote
        3
        down vote










        up vote
        3
        down vote









        Try rewriting in this way, instead of using list inclusions:



        count =0
        for c in sentence:
        if c.isupper():
        count+=1
        print(count)


        That way, there won't be much to explain!






        share|improve this answer














        Try rewriting in this way, instead of using list inclusions:



        count =0
        for c in sentence:
        if c.isupper():
        count+=1
        print(count)


        That way, there won't be much to explain!







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 19 at 14:11

























        answered Nov 19 at 14:00









        Matina G

        37619




        37619












        • Can't go wrong with a simple loop. Also note that it seems that the OP is using Python 3, and print needs parentheses.
          – RoadRunner
          Nov 19 at 14:11






        • 1




          Thanks, fixed it
          – Matina G
          Nov 19 at 14:12


















        • Can't go wrong with a simple loop. Also note that it seems that the OP is using Python 3, and print needs parentheses.
          – RoadRunner
          Nov 19 at 14:11






        • 1




          Thanks, fixed it
          – Matina G
          Nov 19 at 14:12
















        Can't go wrong with a simple loop. Also note that it seems that the OP is using Python 3, and print needs parentheses.
        – RoadRunner
        Nov 19 at 14:11




        Can't go wrong with a simple loop. Also note that it seems that the OP is using Python 3, and print needs parentheses.
        – RoadRunner
        Nov 19 at 14:11




        1




        1




        Thanks, fixed it
        – Matina G
        Nov 19 at 14:12




        Thanks, fixed it
        – Matina G
        Nov 19 at 14:12










        up vote
        0
        down vote













        Your code is basically a loop that is placed inside the sum function. The loop checks every letter in turn and adds a 1 to the total if it finds a capital letter. I.e if the isupper() function returns True.



        sentence = "Hi My Name is"
        a = 0

        for c in sentence:
        if c.isupper():
        a = a + 1
        else:
        pass

        print(a, " capital letters")

        b = sum(1 for c in sentence if c.isupper())

        print(b, " capital letters")


        ~






        share|improve this answer








        New contributor




        jandor is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






















          up vote
          0
          down vote













          Your code is basically a loop that is placed inside the sum function. The loop checks every letter in turn and adds a 1 to the total if it finds a capital letter. I.e if the isupper() function returns True.



          sentence = "Hi My Name is"
          a = 0

          for c in sentence:
          if c.isupper():
          a = a + 1
          else:
          pass

          print(a, " capital letters")

          b = sum(1 for c in sentence if c.isupper())

          print(b, " capital letters")


          ~






          share|improve this answer








          New contributor




          jandor is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.




















            up vote
            0
            down vote










            up vote
            0
            down vote









            Your code is basically a loop that is placed inside the sum function. The loop checks every letter in turn and adds a 1 to the total if it finds a capital letter. I.e if the isupper() function returns True.



            sentence = "Hi My Name is"
            a = 0

            for c in sentence:
            if c.isupper():
            a = a + 1
            else:
            pass

            print(a, " capital letters")

            b = sum(1 for c in sentence if c.isupper())

            print(b, " capital letters")


            ~






            share|improve this answer








            New contributor




            jandor is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            Your code is basically a loop that is placed inside the sum function. The loop checks every letter in turn and adds a 1 to the total if it finds a capital letter. I.e if the isupper() function returns True.



            sentence = "Hi My Name is"
            a = 0

            for c in sentence:
            if c.isupper():
            a = a + 1
            else:
            pass

            print(a, " capital letters")

            b = sum(1 for c in sentence if c.isupper())

            print(b, " capital letters")


            ~







            share|improve this answer








            New contributor




            jandor is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            share|improve this answer



            share|improve this answer






            New contributor




            jandor is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            answered Nov 19 at 14:22









            jandor

            12




            12




            New contributor




            jandor is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.





            New contributor





            jandor is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.






            jandor is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.






















                up vote
                0
                down vote













                Try to decomposite "sum(1 for c in sentence if c.isupper())":



                1) 1 for c in sentence if c.isupper()



                It means: "generate 1 if symbol if c is uppercase".



                Also, note that (1 for c in sentence if c.isupper()) is a generator
                But because it placed as a function positional argument it can be placed there without parentheses.



                2) sum(...) -- summarize all "1" generated from step 1)



                sum expect iterable as an argument: list, tuple, generator or another object that support iterator protocol






                share|improve this answer



























                  up vote
                  0
                  down vote













                  Try to decomposite "sum(1 for c in sentence if c.isupper())":



                  1) 1 for c in sentence if c.isupper()



                  It means: "generate 1 if symbol if c is uppercase".



                  Also, note that (1 for c in sentence if c.isupper()) is a generator
                  But because it placed as a function positional argument it can be placed there without parentheses.



                  2) sum(...) -- summarize all "1" generated from step 1)



                  sum expect iterable as an argument: list, tuple, generator or another object that support iterator protocol






                  share|improve this answer

























                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    Try to decomposite "sum(1 for c in sentence if c.isupper())":



                    1) 1 for c in sentence if c.isupper()



                    It means: "generate 1 if symbol if c is uppercase".



                    Also, note that (1 for c in sentence if c.isupper()) is a generator
                    But because it placed as a function positional argument it can be placed there without parentheses.



                    2) sum(...) -- summarize all "1" generated from step 1)



                    sum expect iterable as an argument: list, tuple, generator or another object that support iterator protocol






                    share|improve this answer














                    Try to decomposite "sum(1 for c in sentence if c.isupper())":



                    1) 1 for c in sentence if c.isupper()



                    It means: "generate 1 if symbol if c is uppercase".



                    Also, note that (1 for c in sentence if c.isupper()) is a generator
                    But because it placed as a function positional argument it can be placed there without parentheses.



                    2) sum(...) -- summarize all "1" generated from step 1)



                    sum expect iterable as an argument: list, tuple, generator or another object that support iterator protocol







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 19 at 14:29

























                    answered Nov 19 at 14:24









                    Vovk Donets

                    465




                    465






















                        up vote
                        0
                        down vote













                        sum(1 for i in iterable )


                        will add 1 for each member of iterable. look at this :



                        sum(1 for i in [True,True,True]) 


                        will return 3



                        sum(10 for i in [True,True,True])


                        will return 30



                        so for each iteration sum will add the number to itself.
                        the "for c in sentence if c.isupper()" return a list of "True"s so for any character that is uppercase sum will add 1.






                        share|improve this answer

























                          up vote
                          0
                          down vote













                          sum(1 for i in iterable )


                          will add 1 for each member of iterable. look at this :



                          sum(1 for i in [True,True,True]) 


                          will return 3



                          sum(10 for i in [True,True,True])


                          will return 30



                          so for each iteration sum will add the number to itself.
                          the "for c in sentence if c.isupper()" return a list of "True"s so for any character that is uppercase sum will add 1.






                          share|improve this answer























                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            sum(1 for i in iterable )


                            will add 1 for each member of iterable. look at this :



                            sum(1 for i in [True,True,True]) 


                            will return 3



                            sum(10 for i in [True,True,True])


                            will return 30



                            so for each iteration sum will add the number to itself.
                            the "for c in sentence if c.isupper()" return a list of "True"s so for any character that is uppercase sum will add 1.






                            share|improve this answer












                            sum(1 for i in iterable )


                            will add 1 for each member of iterable. look at this :



                            sum(1 for i in [True,True,True]) 


                            will return 3



                            sum(10 for i in [True,True,True])


                            will return 30



                            so for each iteration sum will add the number to itself.
                            the "for c in sentence if c.isupper()" return a list of "True"s so for any character that is uppercase sum will add 1.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 19 at 14:31









                            Ali Kargar

                            1444




                            1444






















                                DavidP75 is a new contributor. Be nice, and check out our Code of Conduct.










                                 

                                draft saved


                                draft discarded


















                                DavidP75 is a new contributor. Be nice, and check out our Code of Conduct.













                                DavidP75 is a new contributor. Be nice, and check out our Code of Conduct.












                                DavidP75 is a new contributor. Be nice, and check out our Code of Conduct.















                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53376146%2fwhat-does-sum1-for-c-in-sentence-if-c-isupper-mean-in-non-programming-terms%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

                                Wiesbaden

                                To store a contact into the json file from server.js file using a class in NodeJS

                                Marschland