Printing all perfect squares from a list in Python using list comprehension and math module

In this tutorial, you will learn how to check whether the elements in a Python list entered by the user are perfect squares or not using list comprehension and the math module.

List comprehensions are a very cool trick where we can make a new list based on the values of an existing list in only one line, hence making the code in question look short and concise since we are not writing an entire loop committing to the problem.

The math module in python is a very useful tool as it contains a multitude of mathematical functions that we can use in our code.

In this example, we shall be taking a list input from the user and make a new list that consists of only perfect squares that were present in the previous list by checking if the square root of the number in question yields an integer value or a float value.

Floor Function (math.floor(x))

The floor function is part of the math module in python. It is used to round decimal numbers down to their nearest integers. The working of the floor function is demonstrated below:-

#This demonstrates the working of floor function of math module
import math
print(math.floor(8.9) )
print(math.floor(7.3))
print(math.floor(-9.6))
print(math.floor(-9.2))

Output:-

8
7
-10
-10

Square root function (math.sqrt(x))

The square root function is also part of the math module in python. This function calculates the square root of the number passed as its argument. The working of the sqrt function is demonstrated below:-

#This demonstrates the working of sqrt function of math module
import math
print(math.sqrt(2))
print(math.sqrt(4))
print(math.sqrt(789))
print(math.sqrt(234.98))

Output:-

1.4142135623730951
2.0
28.089143810376278
15.329057374802927

List Comprehension to find Perfect Squares in the List

List Comprehension is a very handy tool in python which helps us create a new list from existing elements in an old list based on certain predefined conditions, which in this case is whether the element in the list is a perfect square or not.

Perfect squares are integers which when square rooted yield a number which is an integer and not a decimal.

Look at this code below:-

#Importing the math module
import math
#Initialising the array
ar = [2, 4, 64, 5, 25, 10, 100, 625, 144, 12]
#Printing the original array
print("The original array is :: ", ar)
#Using List comprehension to find perfect squares
sq_ar = [i for i in ar if (math.sqrt(i) == math.floor(math.sqrt(i)))]
#Printing the perfect squares in list form
print("The elements that were perfect squares from the above array are :: ", sq_ar)

Output:-

The original array is :: [2, 4, 64, 5, 25, 10, 100, 625, 144, 12]
The elements that were perfect squares from the above array are :: [4, 64, 25, 100, 625, 144]

 

Let us analyze what is happening in this code:-

  • Importing the math module initially since it contains the floor and sqrt functions and we need to use them for our code
  • Initialising an array ar which contains 10 elements, some of which are perfect squares.
  • Using list comprehension to identify the perfect squares in ar and store them in a new list sq_ar
  • We then print the list sq_ar which contains all the perfect squares from ar.

What is happening in the List Comprehension?

  1. On iterating through ar wherein every nth iteration, its element is stored in (n-1)the index. For example, when we are considering element 4 (index number = 1), we are in the second iteration.
  2. When we are considering a particular element, we check whether the number yielded on square rooting it is equal to the number yielded on flooring the square root of the same number. For example, math.sqrt(4) = 2 and math.floor(math.sqrt(4)) is math.floor(2) which is equal to 2. Hence math.sqrt(i) == math.floor(math.sqrt(i)) is satisfied and hence 4 is appended to the sq_ar list. Similarly, math.sqrt(2) = 1.414 and math.floor(math.sqrt(2)) is math.floor(1.414) which is equal to 1. Hence math.sqrt(i) == math.floor(math.sqrt(i)) is not satisfied and is not appended to the sq_ar list.

I hope this tutorial was useful for you!!

 

Leave a Reply

Your email address will not be published. Required fields are marked *