Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

how to sum digits of a number in python

number = 123 # the number you want summed up
sum_of_digits = 0 
for digit in str(number):
  sum_of_digits += int(digit)
  
print(sum_of_digits) # printing the final sum of number
Comment

how to find the sum of digits of a number in python

num = int(input("Enter the number: "))
sum_of_digits = 0
while num > 0:
    digit = num % 10
    num //= 10
    sum_of_digits += digit
print("The sum of digits is", sum_of_digits)
Comment

python sum of digits in a string

# Here is the short version
number = 159
sum((int(n) for n in str(abs(number))))
Comment

sum of number digits python

num = input("Enter your number: ")
result = 0
for n in str(num):
        result += int(n)
print("Result: ",result)

#code by fawlid
Comment

sum of digits in python

# Python program to
# compute sum of digits in 
# number.
   
# Function to get sum of digits 
def getSum(n):
     
    strr = str(n)
    list_of_number = list(map(int, strr.strip()))
    return sum(list_of_number)
   
n = 12345
print(getSum(n))
Comment

PREVIOUS NEXT
Code Example
Python :: python distance calculator 
Python :: python turtle window not responding 
Python :: resize multiple images to same size python 
Python :: how to save the history of keras model 
Python :: igraph adjacency matrix python 
Python :: key item loop list python 
Python :: how to plot heatmap in python 
Python :: django template iterate dict 
Python :: change tick labelsize matplotlib 
Python :: how to make a pygame window 
Python :: frequency of occurrence of that element in the list and the positions 
Python :: change the style of notebook tkinter 
Python :: ssl unverified certificate python 
Python :: where to find python3 interpreter 
Python :: all permutations python 
Python :: pytz timezone list 
Python :: selenium refresh till the element appears python 
Python :: pyspark select without column 
Python :: panda - subset based on column value 
Python :: django update increment 
Python :: python named tuple 
Python :: How to log a python crash? 
Python :: python number with comma to float 
Python :: pandas open text file 
Python :: django.db.utils.OperationalError: no such table: 
Python :: pyspark take random sample 
Python :: ball bounce in pygame 
Python :: django.core.exceptions.FieldError: Unknown field(s) (author) specified for Comment 
Python :: dataframe from arrays python 
Python :: python get current user windows 
ADD CONTENT
Topic
Content
Source link
Name
1+1 =