How the sum() function works in python?












-3















sum({'x':-10,'y':-20,'z':-30},60) 
#TypeError: unsupported operand type(s) for +: 'int' and 'str'


but



sum({-10: 'x', -20: 'y', -30: 'z'},60)  
#returns 0









share|improve this question




















  • 2





    When you iterate over a dictionary you iterate over its keys. This is demonstrated by the examples you've posted.

    – jonrsharpe
    Nov 17 '18 at 13:14











  • sum([4],[1,2,3]) #why this error message appears:TypeError: can only concatenate list (not "int") to list

    – Damodara Sahu
    Nov 17 '18 at 13:50













  • Because it's trying to do [1, 2, 3] + 4, that doesn't make sense.

    – jonrsharpe
    Nov 17 '18 at 14:02











  • Can we able to add the contents of two lists using sum method; just like, [1,2,3] add [4] results to 10?

    – Damodara Sahu
    Nov 18 '18 at 12:55











  • Not just using sum once, no; you'd need to combine the lists then sum them, e.g.: sum([1, 2, 3] + [4]).

    – jonrsharpe
    Nov 18 '18 at 12:56
















-3















sum({'x':-10,'y':-20,'z':-30},60) 
#TypeError: unsupported operand type(s) for +: 'int' and 'str'


but



sum({-10: 'x', -20: 'y', -30: 'z'},60)  
#returns 0









share|improve this question




















  • 2





    When you iterate over a dictionary you iterate over its keys. This is demonstrated by the examples you've posted.

    – jonrsharpe
    Nov 17 '18 at 13:14











  • sum([4],[1,2,3]) #why this error message appears:TypeError: can only concatenate list (not "int") to list

    – Damodara Sahu
    Nov 17 '18 at 13:50













  • Because it's trying to do [1, 2, 3] + 4, that doesn't make sense.

    – jonrsharpe
    Nov 17 '18 at 14:02











  • Can we able to add the contents of two lists using sum method; just like, [1,2,3] add [4] results to 10?

    – Damodara Sahu
    Nov 18 '18 at 12:55











  • Not just using sum once, no; you'd need to combine the lists then sum them, e.g.: sum([1, 2, 3] + [4]).

    – jonrsharpe
    Nov 18 '18 at 12:56














-3












-3








-3








sum({'x':-10,'y':-20,'z':-30},60) 
#TypeError: unsupported operand type(s) for +: 'int' and 'str'


but



sum({-10: 'x', -20: 'y', -30: 'z'},60)  
#returns 0









share|improve this question
















sum({'x':-10,'y':-20,'z':-30},60) 
#TypeError: unsupported operand type(s) for +: 'int' and 'str'


but



sum({-10: 'x', -20: 'y', -30: 'z'},60)  
#returns 0






python-3.x






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 '18 at 13:14









jonrsharpe

77.2k11103208




77.2k11103208










asked Nov 17 '18 at 13:13









Damodara SahuDamodara Sahu

216




216








  • 2





    When you iterate over a dictionary you iterate over its keys. This is demonstrated by the examples you've posted.

    – jonrsharpe
    Nov 17 '18 at 13:14











  • sum([4],[1,2,3]) #why this error message appears:TypeError: can only concatenate list (not "int") to list

    – Damodara Sahu
    Nov 17 '18 at 13:50













  • Because it's trying to do [1, 2, 3] + 4, that doesn't make sense.

    – jonrsharpe
    Nov 17 '18 at 14:02











  • Can we able to add the contents of two lists using sum method; just like, [1,2,3] add [4] results to 10?

    – Damodara Sahu
    Nov 18 '18 at 12:55











  • Not just using sum once, no; you'd need to combine the lists then sum them, e.g.: sum([1, 2, 3] + [4]).

    – jonrsharpe
    Nov 18 '18 at 12:56














  • 2





    When you iterate over a dictionary you iterate over its keys. This is demonstrated by the examples you've posted.

    – jonrsharpe
    Nov 17 '18 at 13:14











  • sum([4],[1,2,3]) #why this error message appears:TypeError: can only concatenate list (not "int") to list

    – Damodara Sahu
    Nov 17 '18 at 13:50













  • Because it's trying to do [1, 2, 3] + 4, that doesn't make sense.

    – jonrsharpe
    Nov 17 '18 at 14:02











  • Can we able to add the contents of two lists using sum method; just like, [1,2,3] add [4] results to 10?

    – Damodara Sahu
    Nov 18 '18 at 12:55











  • Not just using sum once, no; you'd need to combine the lists then sum them, e.g.: sum([1, 2, 3] + [4]).

    – jonrsharpe
    Nov 18 '18 at 12:56








2




2





When you iterate over a dictionary you iterate over its keys. This is demonstrated by the examples you've posted.

– jonrsharpe
Nov 17 '18 at 13:14





When you iterate over a dictionary you iterate over its keys. This is demonstrated by the examples you've posted.

– jonrsharpe
Nov 17 '18 at 13:14













sum([4],[1,2,3]) #why this error message appears:TypeError: can only concatenate list (not "int") to list

– Damodara Sahu
Nov 17 '18 at 13:50







sum([4],[1,2,3]) #why this error message appears:TypeError: can only concatenate list (not "int") to list

– Damodara Sahu
Nov 17 '18 at 13:50















Because it's trying to do [1, 2, 3] + 4, that doesn't make sense.

– jonrsharpe
Nov 17 '18 at 14:02





Because it's trying to do [1, 2, 3] + 4, that doesn't make sense.

– jonrsharpe
Nov 17 '18 at 14:02













Can we able to add the contents of two lists using sum method; just like, [1,2,3] add [4] results to 10?

– Damodara Sahu
Nov 18 '18 at 12:55





Can we able to add the contents of two lists using sum method; just like, [1,2,3] add [4] results to 10?

– Damodara Sahu
Nov 18 '18 at 12:55













Not just using sum once, no; you'd need to combine the lists then sum them, e.g.: sum([1, 2, 3] + [4]).

– jonrsharpe
Nov 18 '18 at 12:56





Not just using sum once, no; you'd need to combine the lists then sum them, e.g.: sum([1, 2, 3] + [4]).

– jonrsharpe
Nov 18 '18 at 12:56












2 Answers
2






active

oldest

votes


















1














As explained on Python's documentation, the sum function will sum the start value (2nd argument) with the items from an iterable data structure (1st argument). And, as mentioned on the comments, a dict by default is iterable over its keys.



Thus, your second example is adding 60 (your start value) with your numerical dict items (keys):




>>> 60 + (-10) + (-20) + (-30)



0




Your first example, however, is trying to add strings to numbers, which is not what sum() was intended to do:




>>> 60 + 'x' + 'y' + 'z'



(...) TypeError: unsupported operand type(s) for +: 'int' and 'str'




If you want, for instance, to iterate a dict over its values you can use the values() function. For example:



sum({'x':-10,'y':-20,'z':-30}.values(),60)





share|improve this answer
























  • Thanq. So nice explanation.

    – Damodara Sahu
    Nov 17 '18 at 22:06



















0














Basically, sum({'x':-10,'y':-20,'z':-30},60) will result in an error as the program will encounter strings instead of integers as keys. Obviously, the program won't add 60 to strings as that operation doesn't make any sense. What is the result of 'x' + 'y', 'z' + 60?



In the second example,sum({-10: 'x', -20: 'y', -30: 'z'},60) the program iterates over integers only. That's why you have received 0 as the sum of -10, -20, -30 + 60 = 0. That makes sense.



Hope this helps.






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%2f53351596%2fhow-the-sum-function-works-in-python%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









    1














    As explained on Python's documentation, the sum function will sum the start value (2nd argument) with the items from an iterable data structure (1st argument). And, as mentioned on the comments, a dict by default is iterable over its keys.



    Thus, your second example is adding 60 (your start value) with your numerical dict items (keys):




    >>> 60 + (-10) + (-20) + (-30)



    0




    Your first example, however, is trying to add strings to numbers, which is not what sum() was intended to do:




    >>> 60 + 'x' + 'y' + 'z'



    (...) TypeError: unsupported operand type(s) for +: 'int' and 'str'




    If you want, for instance, to iterate a dict over its values you can use the values() function. For example:



    sum({'x':-10,'y':-20,'z':-30}.values(),60)





    share|improve this answer
























    • Thanq. So nice explanation.

      – Damodara Sahu
      Nov 17 '18 at 22:06
















    1














    As explained on Python's documentation, the sum function will sum the start value (2nd argument) with the items from an iterable data structure (1st argument). And, as mentioned on the comments, a dict by default is iterable over its keys.



    Thus, your second example is adding 60 (your start value) with your numerical dict items (keys):




    >>> 60 + (-10) + (-20) + (-30)



    0




    Your first example, however, is trying to add strings to numbers, which is not what sum() was intended to do:




    >>> 60 + 'x' + 'y' + 'z'



    (...) TypeError: unsupported operand type(s) for +: 'int' and 'str'




    If you want, for instance, to iterate a dict over its values you can use the values() function. For example:



    sum({'x':-10,'y':-20,'z':-30}.values(),60)





    share|improve this answer
























    • Thanq. So nice explanation.

      – Damodara Sahu
      Nov 17 '18 at 22:06














    1












    1








    1







    As explained on Python's documentation, the sum function will sum the start value (2nd argument) with the items from an iterable data structure (1st argument). And, as mentioned on the comments, a dict by default is iterable over its keys.



    Thus, your second example is adding 60 (your start value) with your numerical dict items (keys):




    >>> 60 + (-10) + (-20) + (-30)



    0




    Your first example, however, is trying to add strings to numbers, which is not what sum() was intended to do:




    >>> 60 + 'x' + 'y' + 'z'



    (...) TypeError: unsupported operand type(s) for +: 'int' and 'str'




    If you want, for instance, to iterate a dict over its values you can use the values() function. For example:



    sum({'x':-10,'y':-20,'z':-30}.values(),60)





    share|improve this answer













    As explained on Python's documentation, the sum function will sum the start value (2nd argument) with the items from an iterable data structure (1st argument). And, as mentioned on the comments, a dict by default is iterable over its keys.



    Thus, your second example is adding 60 (your start value) with your numerical dict items (keys):




    >>> 60 + (-10) + (-20) + (-30)



    0




    Your first example, however, is trying to add strings to numbers, which is not what sum() was intended to do:




    >>> 60 + 'x' + 'y' + 'z'



    (...) TypeError: unsupported operand type(s) for +: 'int' and 'str'




    If you want, for instance, to iterate a dict over its values you can use the values() function. For example:



    sum({'x':-10,'y':-20,'z':-30}.values(),60)






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 17 '18 at 14:17









    Aurora WangAurora Wang

    713216




    713216













    • Thanq. So nice explanation.

      – Damodara Sahu
      Nov 17 '18 at 22:06



















    • Thanq. So nice explanation.

      – Damodara Sahu
      Nov 17 '18 at 22:06

















    Thanq. So nice explanation.

    – Damodara Sahu
    Nov 17 '18 at 22:06





    Thanq. So nice explanation.

    – Damodara Sahu
    Nov 17 '18 at 22:06













    0














    Basically, sum({'x':-10,'y':-20,'z':-30},60) will result in an error as the program will encounter strings instead of integers as keys. Obviously, the program won't add 60 to strings as that operation doesn't make any sense. What is the result of 'x' + 'y', 'z' + 60?



    In the second example,sum({-10: 'x', -20: 'y', -30: 'z'},60) the program iterates over integers only. That's why you have received 0 as the sum of -10, -20, -30 + 60 = 0. That makes sense.



    Hope this helps.






    share|improve this answer




























      0














      Basically, sum({'x':-10,'y':-20,'z':-30},60) will result in an error as the program will encounter strings instead of integers as keys. Obviously, the program won't add 60 to strings as that operation doesn't make any sense. What is the result of 'x' + 'y', 'z' + 60?



      In the second example,sum({-10: 'x', -20: 'y', -30: 'z'},60) the program iterates over integers only. That's why you have received 0 as the sum of -10, -20, -30 + 60 = 0. That makes sense.



      Hope this helps.






      share|improve this answer


























        0












        0








        0







        Basically, sum({'x':-10,'y':-20,'z':-30},60) will result in an error as the program will encounter strings instead of integers as keys. Obviously, the program won't add 60 to strings as that operation doesn't make any sense. What is the result of 'x' + 'y', 'z' + 60?



        In the second example,sum({-10: 'x', -20: 'y', -30: 'z'},60) the program iterates over integers only. That's why you have received 0 as the sum of -10, -20, -30 + 60 = 0. That makes sense.



        Hope this helps.






        share|improve this answer













        Basically, sum({'x':-10,'y':-20,'z':-30},60) will result in an error as the program will encounter strings instead of integers as keys. Obviously, the program won't add 60 to strings as that operation doesn't make any sense. What is the result of 'x' + 'y', 'z' + 60?



        In the second example,sum({-10: 'x', -20: 'y', -30: 'z'},60) the program iterates over integers only. That's why you have received 0 as the sum of -10, -20, -30 + 60 = 0. That makes sense.



        Hope this helps.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 17 '18 at 14:33









        Abdulaziz Al JumaiaAbdulaziz Al Jumaia

        118113




        118113






























            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53351596%2fhow-the-sum-function-works-in-python%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

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)