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












1














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
























  • Be specific about what you don't understand. As it stands your question is too broad.
    – kenlukas
    Nov 19 '18 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 '18 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 '18 at 14:20
















1














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
























  • Be specific about what you don't understand. As it stands your question is too broad.
    – kenlukas
    Nov 19 '18 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 '18 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 '18 at 14:20














1












1








1







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















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















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 14:15









J100

951413




951413










asked Nov 19 '18 at 13:54









DavidP75

61




61












  • Be specific about what you don't understand. As it stands your question is too broad.
    – kenlukas
    Nov 19 '18 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 '18 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 '18 at 14:20


















  • Be specific about what you don't understand. As it stands your question is too broad.
    – kenlukas
    Nov 19 '18 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 '18 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 '18 at 14:20
















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




Be specific about what you don't understand. As it stands your question is too broad.
– kenlukas
Nov 19 '18 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 '18 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 '18 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 '18 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 '18 at 14:20












5 Answers
5






active

oldest

votes


















4














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 '18 at 14:09





















3














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 '18 at 14:11






  • 1




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



















0














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





























    0














    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































      0














      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',
        autoActivateHeartbeat: false,
        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%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









        4














        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 '18 at 14:09


















        4














        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 '18 at 14:09
















        4












        4








        4






        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 '18 at 13:58









        OperatorOverload

        409312




        409312












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




















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


















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






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















        3














        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 '18 at 14:11






        • 1




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
















        3














        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 '18 at 14:11






        • 1




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














        3












        3








        3






        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 '18 at 14:11

























        answered Nov 19 '18 at 14:00









        Matina G

        50619




        50619












        • 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 '18 at 14:11






        • 1




          Thanks, fixed it
          – Matina G
          Nov 19 '18 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 '18 at 14:11






        • 1




          Thanks, fixed it
          – Matina G
          Nov 19 '18 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 '18 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 '18 at 14:11




        1




        1




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




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











        0














        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


























          0














          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
























            0












            0








            0






            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












            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












            share|improve this answer



            share|improve this answer










            answered Nov 19 '18 at 14:22









            jandor

            12




            12























                0














                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




























                  0














                  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


























                    0












                    0








                    0






                    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 '18 at 14:29

























                    answered Nov 19 '18 at 14:24









                    Vovk Donets

                    465




                    465























                        0














                        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


























                          0














                          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
























                            0












                            0








                            0






                            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 '18 at 14:31









                            Ali Kargar

                            1444




                            1444






























                                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%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

                                android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

                                SQL update select statement

                                'app-layout' is not a known element: how to share Component with different Modules