Function to Calculate Sum of Digits - Python Programming Exercise

In this exercise, you will develop a Python program with a function named "sum_digits" that takes a number as input and returns the sum of its digits. This exercise is perfect for practicing function definition, loops, and arithmetic operations in Python. By implementing this function, you will gain hands-on experience in handling function definitions, loops, and arithmetic operations in Python. This exercise not only reinforces your understanding of functions but also helps you develop efficient coding practices for managing user interactions.

 Category

Mastering Functions

 Exercise

Function To Calculate Sum Of Digits

 Objective

Develop a Python program with a function named "sum_digits" that takes a number as input and returns the sum of its digits. For instance, if the input number is 123, the function should return 6.

For example: print(sum_digits(123)) should output 6.

 Example Python Exercise

 Copy Python Code
# Define the sum_digits function
def sum_digits(number):
    total = 0
    # Convert the number to a string to access each digit
    for digit in str(abs(number)):  # abs() to handle negative numbers
        total += int(digit)  # Add each digit to the total
    return total

# Main function to test the sum_digits function
def main():
    number = 123
    print(sum_digits(number))  # This should print 6

# Call the main function to execute the program
if __name__ == "__main__":
    main()

 Output

6

 Share this Python Exercise

 More Python Programming Exercises of Mastering Functions

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  Function to Compute Factorial

    In this exercise, you will develop a Python program with a recursive function to compute the factorial of a given number. The factorial of a number is defined as: 𝑛...

  •  Main Function Parameters and Reversal

    In this exercise, you will develop a Python program named "reverse" that takes multiple words from the command line and prints them in reverse order. This exercise...

  •  Function to Obtain Integer Value

    In this exercise, you will develop a Python program with a function named "get_int" that displays the text received as a parameter, prompts the user for an integer, a...

  •  Function to Manage Task Database

    In this exercise, you will develop a Python program to enhance the "tasks database" by dividing it into multiple functions for better organization and readability. Th...

  •  Function to Find Maximum Value in an Array

    In this exercise, you will develop a Python program containing a function that accepts a list of floating-point numbers as input and returns the maximum value within ...

  •  Iterative Implementation of the Factorial Function

    In this exercise, you will develop a Python program that calculates the factorial of a given number using an iterative (non-recursive) method. This exercise is...