Data types and variables
Operators
Modules and Packages
Conversion Programs
More Code Examples
Cheat Sheet

Python Code Example: Perfect Number

In this example, we will examine a Python script that checks whether a given number is a perfect number. A perfect number is a positive integer that is equal to the sum of its proper divisors, excluding itself. The script prompts the user to input a number and then uses a loop to find all proper divisors, summing them up. It finally compares the sum to the original number to determine if it is perfect, and prints the appropriate message. This example highlights the use of basic input/output functions, loops, conditional statements, and arithmetic operations in Python.

Code Example

sum = 0
number = int(input("Enter a number to check for perfect: "))

for i in range(1, number):
    if number % i == 0:
        sum = sum + i

if sum == number:
    print(str(number) + " is a perfect number")
else:
    print(str(number) + " is not a perfect number")

Detailed Code Explanation

Initial Variable and Input Statement

sum = 0
number = int(input("Enter a number to check for perfect: "))

These lines initialize the variable sum to 0 and prompt the user to enter a number, which will be stored in the variable number.

Step-by-Step Explanation

  • sum = 0: Initializes a variable sum to 0, which will be used to accumulate the sum of the proper divisors of number.
  • input("Enter a number to check for perfect: "): Displays the message “Enter a number to check for perfect:” and waits for the user to input a value.
  • int(...): Converts the user input (a string) to an integer and assigns it to the variable number.

Loop to Find Proper Divisors

for i in range(1, number):
    if number % i == 0:
        sum = sum + i

This block of code iterates through all numbers from 1 to number - 1 to find the proper divisors of number and sum them up.

Step-by-Step Explanation

  • for i in range(1, number):: Initiates a loop that iterates over each integer i from 1 to number - 1.
  • if number % i == 0:: Checks if i is a proper divisor of number by verifying that the remainder of number divided by i is 0.
  • sum = sum + i: If i is a proper divisor, it is added to sum.

Conditional Statement to Check for Perfect Number

if sum == number:
    print(str(number) + " is a perfect number")
else:
    print(str(number) + " is not a perfect number")

This block of code checks if the sum of the proper divisors equals the original number and prints the appropriate message.

Step-by-Step Explanation

  • if sum == number:: Checks if the accumulated sum of proper divisors is equal to number.
    • print(str(number) + " is a perfect number"): If sum equals number, constructs a string indicating that number is a perfect number and prints it to the console.
  • else:: If sum does not equal number, the code inside the else block is executed.
    • print(str(number) + " is not a perfect number"): Constructs a string indicating that number is not a perfect number and prints it to the console.