Python Basic
Python Interview Programs
- Python Interview Programs
- Python Abundant Number
- Python Amicable Number
- Python Armstrong Number
- Python Average of N Numbers
- Python Automorphic Number
- Python Biggest of three numbers
- Python Binary to Decimal
- Python Common Divisors
- Python Composite Number
- Python Condense a Number
- Python Cube Number
- Python Decimal to Binary
- Python Decimal to Octal
- Python Disarium Number
- Python Even Number
- Python Evil Number
- Python Factorial of a Number
- Python Fibonacci Series
- Python GCD
- Python Happy Number
- Python Harshad Number
- Python LCM
- Python Leap Year
- Python Magic Number
- Python Matrix Addition
- Python Matrix Division
- Python Matrix Multiplication
- Python Matrix Subtraction
- Python Matrix Transpose
- Python Maximum Value of an Array
- Python Minimum Value of an Array
- Python Multiplication Table
- Python Natural Number
- Python Number Combination
- Python Odd Number
- Python Palindrome Number
- Python Pascalβs Triangle
- Python Perfect Number
- Python Perfect Square
- Python Power of 2
- Python Power of 3
- Python Pronic Number
- Python Prime Factor
- Python Prime Number
- Python Smith Number
- Python Strong Number
- Python Sum of Array
- Python Sum of Digits
- Python Swap Two Numbers
- Python Triangular Number
Python Program to check Perfect Square
Photo Credit to CodeToFun
π Introduction
In the realm of programming, various mathematical problems challenge programmers to devise efficient solutions. One such problem is determining whether a given number is a perfect square.
A perfect square is a number that is the square of an integer, meaning it can be expressed as the product of an integer multiplied by itself.
In this tutorial, we'll explore a Python program designed to check whether a given number is a perfect square or not.
π Example
Let's take a look at the Python code that accomplishes this task.
# Function to check if a number is a perfect square
def is_perfect_square(number):
i = 1
while i * i <= number:
# If i * i is equal to the number, it's a perfect square
if i * i == number:
return True
i += 1
return False
# Driver program
if __name__ == "__main__":
# Replace this value with your desired number
test_number = 16
# Check if the number is a perfect square
if is_perfect_square(test_number):
print(f"{test_number} is a perfect square.")
else:
print(f"{test_number} is not a perfect square.")
π» Testing the Program
To test the program with different numbers, simply replace the value of test_number in the if __name__ == "__main__": block.
16 is a perfect square.
Run the script to see if the number is a perfect square.
π§ How the Program Works
- The program defines a function is_perfect_square that takes an integer as input and returns True if the number is a perfect square, and False otherwise.
- Inside the function, it iterates through integers starting from 1 until the square of the current integer is greater than the given number.
- If the square of the current integer is equal to the given number, the function returns True indicating a perfect square.
- The main program then checks if a specific number (in this case, 16) is a perfect square and prints the result.
π Between the Given Range
Let's take a look at the python code that checks for perfect squares in the specified range.
# Function to check if a number is a perfect square
def isPerfectSquare(num):
sqrt_num = int(num ** 0.5)
return num == sqrt_num * sqrt_num
# Define the range from 1 to 50
start_range = 1
end_range = 50
# Find perfect squares in the range
perfect_squares = [num for num in range(start_range, end_range + 1) if isPerfectSquare(num)]
# Print the output in the specified format
print("Perfect Square in the range 1 to 50:")
for num in perfect_squares:
print(num, end=" ")
π» Testing the Program
Perfect Squares in the Range 1 to 50: 1 4 9 16 25 36 49
Run the script to see the perfect squares in the range from 1 to 50.
π§ How the Program Works
- The program defines a function isPerfectSquare that checks if a given number is a perfect square.
- It then defines the range from 1 to 50 using start_range and end_range.
- Using a list comprehension, it finds perfect squares in the specified range.
- Finally, it prints the perfect squares in the specified format.
π§ Understanding the Concept of Perfect Square
Before diving into the code, let's take a moment to understand the concept of perfect squares.
A perfect square is a number that can be expressed as the product of an integer multiplied by itself.
For example, 4, 9, and 16 are perfect squares because they are the squares of 2, 3, and 4, respectively.
π’ Optimizing the Program
While the provided program is effective, there are alternative methods for checking perfect squares that involve mathematical properties. Consider exploring and implementing such optimizations to enhance the efficiency of your program.
Feel free to incorporate and modify this code as needed for your specific use case. Happy coding!
π¨βπ» Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (Python Program to check Perfect Square), please comment here. I will help you immediately.