Open In App

Python Program to Check if a Number is Perfect Square in List

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will understand how to print all perfect squares from a list in Python using the list comprehension and math module. The main function that is present in the math module that we will be using is the sqrt()  and floor() function. Let’s see the output of these two functions one by one.

Check if a Number is Perfect Square Using Square Root function(sqrt)

 As the name itself suggests, this function is used to calculate the square root of any number.

print(math.sqrt(100)) #Output will be 10

Floor function(floor)

The floor function is an interesting and handy function as it rounds any decimal number down to its closest integer. It is present in the math module as well which we discussed earlier. 

print(math.floor(5.3)) #Output will be 5

Now after learning all the prerequisites, let us now move on to the program that will print only perfect squares from a list.

Example 1:

Python3




import math
 
# Creating A List
Numbers = [4, 16, 17, 11, 36, 82,
           81, 49, 110, 120, 100]
 
# Printing the original array
print("The original List is : ", Numbers)
 
# Using List comprehension to find perfect squares
perfect_squares = [i for i in Numbers if (
    math.sqrt(i) == math.floor(math.sqrt(i)))]
 
# Printing the perfect squares
print("The perfect squares are: ", perfect_squares)


Output

The original List is :  [4, 16, 17, 11, 36, 82, 81, 49, 110, 120, 100]
The perfect squares are:  [4, 16, 36, 81, 49, 100]

Time Complexity: O(n) 
Auxiliary Space: O(1)

Example 2:

Python3




import math
 
# Creating a list
 
Nums = [500, 540, 11, 10, 8, 4, 144, 256, 343, 121]
 
# Printing the original array
print("The original list is: ", Nums)
 
# Using List comprehension to find perfect squares
p_square = [i for i in Nums if (math.sqrt(i) == math.floor(math.sqrt(i)))]
# Printing the perfect squares
print("Perfect squares: ", p_square)


Output

The original list is:  [500, 540, 11, 10, 8, 4, 144, 256, 343, 121]
Perfect squares:  [4, 144, 256, 121]

The time complexity of this code is O(n), because the code is iterating over the input list “Nums” with a length of n, and checking the condition math.sqrt(i) == math.floor(math.sqrt(i)) for each element in the list, which takes O(1) time. Hence the total time complexity is O(n).

The Auxiliary space of this code is O(n), because the code creates a new list “p_square” to store the perfect squares, which takes a space of n at most.

Example 3:

To check if a number is a perfect square in a list using a Python program, you can iterate through the list and verify if each element is a perfect square. A perfect square is an integer that can be expressed as the square of an integer. Here’s a Python program to achieve this:

Python




def is_perfect_square(num):
    # Function to check if a number is a perfect square
    return int(num ** 0.5) ** 2 == num
 
def check_perfect_squares_in_list(num_list):
    # Function to check for perfect squares in the list
    perfect_squares = [num for num in num_list if is_perfect_square(num)]
    return perfect_squares
 
if __name__ == "__main__":
    # Example list of numbers
    numbers = [4, 16, 17, 11, 36, 82,
           81, 49, 110, 120, 100]
 
    perfect_squares_list = check_perfect_squares_in_list(numbers)
    print("Perfect squares in the list:", perfect_squares_list)


Output:

Perfect squares in the list: [4, 16, 36, 81, 49, 100]



Last Updated : 27 Jul, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads