##### How to sum up the elements of an integer using python split/join?
 How to sum up the elements of an integer using python split/join? mohanraj1986 Unladen Swallow Posts: 3 Threads: 1 Joined: Aug 2018 Reputation: 0 Aug-27-2018, 05:18 AM I am trying to sum up the elements of an integer using python split join Here is my code, ----------------------- a='1234' b=list(a) c='+'.join(b) print© ##output- 1+2+3+4 ------------------------------------- How to get the sum of all the elements Reply perfringo Da Bishop Posts: 1,513 Threads: 7 Joined: Jun 2018 Reputation: 150 Aug-27-2018, 05:27 AM Do you want "sum up the elements of an integer" or "sum up the elements of an integer using python split join"? What is expected outcome? 1 + 2 + 3 + 4 = 10 or 1 + 2 + 3 + 4 = 10 --> 1 + 0 = 1 I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame. Reply mohanraj1986 Unladen Swallow Posts: 3 Threads: 1 Joined: Aug 2018 Reputation: 0 Aug-27-2018, 05:52 AM (This post was last modified: Aug-27-2018, 05:54 AM by mohanraj1986.) Thanks for you reply. i want the output like this 1+2+3+4= 10 Reply perfringo Da Bishop Posts: 1,513 Threads: 7 Joined: Jun 2018 Reputation: 150 Aug-27-2018, 05:56 AM (This post was last modified: Aug-27-2018, 06:06 AM by perfringo.) Still, output should be '1 + 2 + 3 + 4 = 10' or just '10'? You can have this way: ```>>> a = '1234' >>> b = list(a) >>> total = sum([int(x) for x in b]) >>> f"{' + '.join(b)} = {total}" '1 + 2 + 3 + 4 = 10' ```Or just sum: ```>>> a = '1234' >>> sum([int(x) for x in list(a)]) 10 ``` I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame. Reply mohanraj1986 Unladen Swallow Posts: 3 Threads: 1 Joined: Aug 2018 Reputation: 0 Aug-27-2018, 06:27 AM Thanks a lot perfringo!! Reply perfringo Da Bishop Posts: 1,513 Threads: 7 Joined: Jun 2018 Reputation: 150 Aug-27-2018, 09:13 AM (This post was last modified: Aug-27-2018, 09:13 AM by perfringo.) You are welcome! Some observations and for future references. It is very important to have correct name for thread. Currently it reads "How to sum up the elements of an integer using python split/join?" and there are several problems with that: there is no elements in integer as integer is not iterable; integer does not support split/join. Problem is actually about string which looks like integer. If somebody searches for solution how sum up digits in integer and reaches this thread will be disappointed that despite promising name it's about something else (of course, one can always do str(1234) and use code above). For those, who looking for "how to sum up digits in integer" without converting into string: 1. Only digits which are at display (shallow sum) i.e 149 = 1 + 4 + 9 = 14. This solution relies of Python feature that in truth value testing zero of any numeric type is false ```def sum_digits(i): total = 0 while i: total += i % 10 i //= 10 return total ```2. Sum integer digits until there is only one digit (deep sum), i.e 149 --> 1 + 4 + 9 = 14 --> 1 + 4 = 5 This relies on math concept of modulo 9 and not any specific Python feature: ```def sum_digits(i): return (i - 1) % 9 + 1 ```If modulo 9 concept seems foreign one can use shallow sum recursively to have deep sum: ```def sum_digits(i): total = 0 while i: total += i % 10 i //= 10 if total < 10: return total else: return sum_digits(total) ``` I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame. Reply

 Possibly Related Threads… Thread Author Replies Views Last Post How does a set in python store the elements? idknuttin 5 453 Jul-10-2020, 10:46 PM Last Post: Gribouillis Join table with differnt datype column using python query abhishek6555 1 404 Jan-07-2020, 11:44 PM Last Post: micseydel python cache for small integer Uchikago 1 767 Jun-27-2019, 05:32 PM Last Post: ichabod801 How to create meshgrid with non-integer stepsize of list elements? dazzabaijan 1 727 Mar-11-2019, 02:00 AM Last Post: ichabod801 SQL select join operation in python(Select.. join) pradeepkumarbe 1 648 Feb-14-2019, 08:34 PM Last Post: woooee Rearranging elements in Python Nirmal 10 1,489 Nov-23-2018, 12:05 PM Last Post: Nirmal Python function that uses a word as the encryption key, rather than an integer wak_stephanie 4 1,729 Aug-31-2018, 12:16 PM Last Post: perfringo Join the Python Standard Library to my project sylas 1 793 May-16-2018, 05:59 AM Last Post: buran Python show the combinations of list of elements zydjohn 6 3,829 Mar-06-2018, 11:23 PM Last Post: zydjohn How to convert character to integer in python rajeev1729 2 1,740 Oct-03-2017, 04:30 PM Last Post: nilamo

Forum Jump:

### User Panel Messages

##### Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020