Find Average of a Number Digits in Python Last Updated : 07 Feb, 2023 Comments Improve Suggest changes Like Article Like Report AIn this article, we will see how to find the average of the digits of a number in Python. Examples: Input: N = 642 Output: 4.0 Explanation: (6+4+2)/3 = 12/3 = 4.0 Input: N = 3504 Output: 3.0 Explanation: (3+5+0+4)/4 = 12/4 = 3.0Calculate the average of a number of digits in python if the number is an integer: Follow the below steps to solve the problem: Loop over the digits of the number.Count the total number of digits of the numberCalculate the sum of all the digits of the numberCalculate and return the number digit average Following is an implementation of the above approach: Python3 # Python function to find the # average of all the digits of a number def digitAverage(n): digiSum, digiLen = 0, 0 # loop over the digits of the number while n: # calculating the number of all the digits digiLen += 1 # calculating the sum of all the digits digiSum += n % 10 # remove the last digit from the original number # to continue to loop over the digits n = n//10 # calculate and return the average return (digiSum/digiLen) print(digitAverage(642)) print(digitAverage(3504)) Output4.0 3.0 Time complexity: O(n)Auxiliary Space: O(1) Calculate the average of a number of digits in python if the number is a floating-point number: In case the input number is a floating point number, we will first convert it into an integer by shifting all the decimal digits by repeatedly multiplying it by 10. To check if a float is equivalent to an integer we can use the is_integer() method of the float class. Follow the below steps to solve the problem: First, we will repeatedly multiply the decimal number by 10 until it becomes an integerThen we can simply follow our previous approachLoop over the digits of the number.Count the total number of digits of the numberCalculate the sum of all the digits of the numberCalculate and return the number digit average Following is an implementation of the above approach: Python3 # Python function to find the average # of all the digits of a number def digitAverage(n): # changing the float into an integer # by repeatedly multiplying the number by 10 n = float(n) while not n.is_integer(): n *= 10 digiSum, digiLen = 0, 0 # loop over the digits of the number while n: # calculating the number of all the digits digiLen += 1 # calculating the sum of all the digits digiSum += n % 10 # remove the last digit from the original number # to continue to loop over the digits n = n//10 # calculate and return the average return (digiSum/digiLen) print(digitAverage(642.0)) print(digitAverage(350.4)) Output4.0 3.0 Time complexity: O(d), d is the number of digits in the given number n.Auxiliary space: O(1). Comment More infoAdvertise with us Next Article Find Average of a Number Digits in Python akashjha2671 Follow Improve Article Tags : Technical Scripter Python Technical Scripter 2022 Practice Tags : python Similar Reads What is a digit in a number? The number is an arithmetical representation of measurement and count that gives an idea. It can be represented in various ways. Say two or "2" otherwise denote by showing the two fingers. Without using numbers, one can not count anything such as money, date, time, etc. Sometimes numbers are used fo 3 min read How to find Sum the Digits of a given Number in PHP ? We will explore how to find the sum of the digits of a given number in PHP. This task involves extracting each digit from the number and adding them together to get the final sum. Table of Content Using a loop to extract digits one by oneUsing mathematical operations to extract digitsUsing a loop to 2 min read Implement IsNumber() function in Python In this article we will see how to implement isNumber() method using Python. This method takes in a string as input and returns True or False according to whether the string is a number or not. Examples: Input : "12345" Output : True Input : "-12345" Output : True Input : "abc" Output : False Approa 2 min read How to Add leading Zeros to a Number in Python In this article, we will learn how to pad or add leading zeroes to the output in Python. Example:Input: 11 Output: 000011 Explanation: Added four zeros before 11(eleven).Display a Number With Leading Zeros in PythonAdd leading Zeros to numbers using format() For more effective handling of sophistica 3 min read What is the biggest 3 digit number in decimal system? The method to represent and work with numbers is known as the number system. A number system is a system of writing to represent numbers. It is the mathematical notation used to represent numbers of a given set by using digits or other symbols. It allows us to operate arithmetic operations such as d 5 min read What is the least 10-digit whole number? Numerals are the mathematical figures used in financial, professional as well as a social field in the social world. The digits and place value in the number and the base of the number system determine the value of a number. Numbers are used in various mathematical operations as summation, subtracti 5 min read Python Program to Check Armstrong Number Given a number x, determine whether the given number is Armstrong number or not. A positive integer of n digits is called an Armstrong number of order n (order is number of digits) if. abcd... = pow(a,n) + pow(b,n) + pow(c,n) + pow(d,n) + .... Example: Input : 153 Output : Yes 153 is an Armstrong nu 6 min read Python | sympy.digits() method With the help of sympy.digits() method, we can find the digits of a given integer in any given base in SymPy. Syntax: digits(n, t=10) Parameter: n - It denotes an integer. b - It denotes an base integer(optional). Default for b is 10. Returns: Returns a list of the digits of n in base b. The first e 2 min read How to remove all decimals from a number using Python? We have to remove all decimals from a number and get the required output using Python. There are several ways to achieve this, depending on whether you want to simply truncate the decimal part, round it or perform other manipulations. For example, number is 12.345 then result is 12.Using int( )int( 3 min read How to generate a random phone number using Python? In this article, we will learn how to generate a random phone number using Python. In general, Indian phone numbers are of 10 digits and start with 9, 8, 7, or 6. Approach: We will use the random library to generate random numbers.The number should contain 10 digits.The first digit should start with 1 min read Like