If you're learning Python and want to practice number-based logic, writing a perfect number program is a great place to start. It's a popular beginner-friendly problem that teaches you about loops, conditionals, and mathematical logic.

In this blog post, we'll explore:
Let’s begin!
A perfect number is a positive integer that is equal to the sum of its proper positive divisors, excluding itself.
🔍 Definition:
A number n is perfect if:
n = sum of all its divisors (excluding n itself)
🔢 Examples:
Perfect numbers are rare and fascinating—they're tied to ancient mathematics and even cryptographic theories!
To check whether a number is perfect, follow this logic:
1. Take a number n
2. Loop from 1 to n-1
3. For each number i, check if it divides n (i.e., n % i == 0)
4. Sum up all such i
5. If the sum equals n, then it’s perfect
Let’s write the complete Python program step by step.
✅ Code:
python
CopyEdit
Copy Code
# Perfect Number Program in Python
def is_perfect(number):
if number < 1:
return False
sum_of_divisors = 0
for i in range(1, number):
if number % i == 0:
sum_of_divisors += i
return sum_of_divisors == number
# Taking input from user
num = int(input("Enter a number: "))
if is_perfect(num):
print(f"{num} is a Perfect Number ✅")
else:
print(f"{num} is NOT a Perfect Number ❌")🧾 Sample Output:
less
CopyEdit
Enter a number: 28
28 is a Perfect Number ✅
Let’s break the logic:
You can reduce time complexity by looping only till n // 2. Why? No divisor (except n itself) can be more than half of n.
python
CopyEdit
for i in range(1, number // 2 + 1):
python
CopyEdit
Copy Code
def is_perfect(number): if number < 2: return False sum_of_divisors = 1 # Start with 1 as it's always a divisor for i in range(2, number // 2 + 1): if number % i == 0: sum_of_divisors += i return sum_of_divisors == number
This version improves performance slightly, especially for larger numbers.
| Input | Output |
| 6 | 6 is a Perfect Number ✅ |
| 28 | 28 is a Perfect Number ✅ |
| 496 | 496 is a Perfect Number ✅ |
| 12 | 12 is NOT a Perfect Number ❌ |
| 0 | 0 is NOT a Perfect Number ❌ |
| -10 | -10 is NOT a Perfect Number ❌ |
Perfect numbers are rare—only a few exist under 10,000.
1. 6
2. 28
3. 496
4. 8128
Want to print all perfect numbers in a given range? Let’s write a loop!
python
CopyEdit
Copy Code
def is_perfect(n): if n < 2: return False total = 1 for i in range(2, n // 2 + 1): if n % i == 0: total += i return total == n # Find perfect numbers from 1 to 10000 for num in range(1, 10001): if is_perfect(num): print(num, "is a Perfect Number")
Output:
csharp
CopyEdit
6 is a Perfect Number
28 is a Perfect Number
496 is a Perfect Number
8128 is a Perfect Number
Solving problems like perfect number programs is an important part of Python learning. But to truly build confidence, you need more structured learning, regular practice, and expert guidance.
That’s why we recommend the Python Programming Course by Uncodemy.
✅ Beginner to Advanced Python Modules
✅ Hands-on Projects and Assignments
✅ Live Sessions + Mentor Support
✅ Coding Challenges & Interview Preparation
✅ Affordable and Flexible Learning
✅ 100% Job Assistance
Whether you're new to programming or looking to ace Python interviews, Uncodemy provides everything you need—from logic building to real-world applications.
👉 Enroll in Python Course by Uncodemy and start solving smarter.
The Perfect Number Program in Python is more than a coding challenge—it's a practical way to learn:
Now that you know how to write and optimize a perfect number checker, take this further—modify it, enhance it, and most importantly, keep experimenting.
And when you’re ready to master Python beyond the basics, Uncodemy’s expert-led course is just a click away.
Happy Coding! 🐍
Personalized learning paths with interactive materials and progress tracking for optimal learning experience.
Explore LMSCreate professional, ATS-optimized resumes tailored for tech roles with intelligent suggestions.
Build ResumeDetailed analysis of how your resume performs in Applicant Tracking Systems with actionable insights.
Check ResumeAI analyzes your code for efficiency, best practices, and bugs with instant feedback.
Try Code ReviewPractice coding in 20+ languages with our cloud-based compiler that works on any device.
Start Coding
TRENDING
BESTSELLER
BESTSELLER
TRENDING
HOT
BESTSELLER
HOT
BESTSELLER
BESTSELLER
HOT
POPULAR