Perfect Number Program in Python Last Updated : 26 Feb, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report A Perfect Number is a positive integer that is equal to the sum of its proper divisors, excluding the number itself.Example:6 is a perfect number because its divisors (excluding itself) are 1, 2, and 3, and their sum is 6 (1 + 2 + 3 = 6).28 is a perfect number because its divisors are 1, 2, 4, 7, 14, and their sum is 28 (1 + 2 + 4 + 7 + 14 = 28).Below are some of the ways by which we can check if the number is a perfect number or not in Python:Using for LoopIn this approach, we simply take a number from the user and make a variable named Sum initially its value is 0 and then we make a logic by using the for loop along with the conditional statement of if/ else. Finally, we get proper output. Python n = 78 Sum = 0 for i in range(1, n): if(n % i == 0): Sum = Sum + i if (Sum == n): print("Number is a Perfect Number.") else: print("Number is not a Perfect Number.") OutputNumber is not a Perfect Number. Explanation: It sums divisors of the number (excluding itself) and if the sum equals the number, it's a perfect number.Using List ComprehensionHere, in this code, we leverage the power of list comprehension to efficiently find the divisors of the given number. The sum of those divisors is then compared to the original number to determine if it is a perfect number. Python n = 6 divisors = sum([i for i in range(1, n) if n % i == 0]) if n == divisors: print("Number is a Perfect Number.") else: print("Number is not a Perfect Number.") OutputNumber is a Perfect Number. Explanation:The list comprehension calculates the sum of divisors of n.It checks if the sum of divisors equals n, which determines whether it's a perfect number.Using filter() methodIn this technique, we use filter() to find all divisors of the given number. The sum of those divisors is then compared to the original number to determine if it is a perfect number. Python n = 25 divisors = list(filter(lambda x: n % x == 0, range(1, n))) if sum(divisors) == n: print("Number is a Perfect Number.") else: print("Number is not a Perfect Number.") OutputNumber is not a Perfect Number. Explanationfilter() method is used to find all divisors of n.sum of the divisors is then compared to n to check if it is a perfect number. Comment More infoAdvertise with us Next Article Intermediate Coding Problems in Python K khanaayuxyc Follow Improve Article Tags : Python Python Programs python-basics Practice Tags : python Similar Reads Python List Creation Programs Python provides multiple ways to create lists based on different requirements, such as generating lists of numbers, creating nested lists, forming lists of tuples or dictionaries, and more.This article covers various ways to create lists efficiently, including:Generating lists of numbers, strings, a 2 min read Python Fundamentals Coding Practice Problems Welcome to this article on Python basic problems, featuring essential exercises on coding, number swapping, type conversion, conditional statements, loops and more. These problems help beginners build a strong foundation in Python fundamentals and problem-solving skills. Letâs start coding!Python Ba 1 min read Output of Python Program | Set 1 Predict the output of following python programs: Program 1:Python r = lambda q: q * 2 s = lambda q: q * 3 x = 2 x = r(x) x = s(x) x = r(x) print (x) Output:24Explanation : In the above program r and s are lambda functions or anonymous functions and q is the argument to both of the functions. In firs 2 min read Output of Python programs | Set 8 Prerequisite - Lists in Python Predict the output of the following Python programs. Program 1 Python list = [1, 2, 3, None, (1, 2, 3, 4, 5), ['Geeks', 'for', 'Geeks']] print len(list) Output: 6Explanation: The beauty of python list datatype is that within a list, a programmer can nest another list, 3 min read Intermediate Coding Problems in Python Python, being a very dynamic and versatile programming language, is used in almost every field. From software development to machine learning, it covers them all. This article will focus on some interesting coding problems which can be used to sharpen our skills a bit more and at the same time, have 8 min read Output of Python programs | Set 7 Prerequisite - Strings in Python Predict the output of the following Python programs. These question set will make you conversant with String Concepts in Python programming language. Program 1Python var1 = 'Hello Geeks!' var2 = "GeeksforGeeks" print "var1[0]: ", var1[0] # statement 1 print "var2[1:5 3 min read Like