Python Program to get a list of square root of all odd numbers between 1 to 20 Using List Comprehension

Python Program to get a list of square root of all odd numbers between 1 to 20 Using List Comprehension

In this article, we will be solving a very interesting question based on List Comprehension. According to the question we have to create a list of square root of all odd numbers between 1 to 20 using list comprehension only.

If you like the article content, then share it in your coding groups so that more people can take advantage of the content. So, without wasting any time let's solve this question.

Question - Write a Python Program to get a list of square root of all odd numbers between 1 to 20 Using List Comprehension

Python Program to get a list of square root of all odd numbers between 1 to 20 Using List Comprehension

Logic and Explanation

Question - Write a Python Program to get a list of square root of all odd numbers between 1 to 20 Using List Comprehension

Python Program to get a list of square root of all odd numbers between 1 to 20 Using List Comprehension - In this program we have to use list comprehension only. First thing we will be doing to to create a for loop from 1 to 20. Then, we will apply a simple if condition to check whether the number in the loop is odd or not.

For this we check if the remainder on dividing the number with 2 is not equal to zero, then the number is the odd number. In this way we separated the odd number and now we add to the list i raised to power 0.5 which means square root.

Also Read - Python Program to Separate Numbers and Letters in a String


Here's the code for the same.

L = [i ** 0.5 for i in range(1,21) if i%2 != 0]
print(L)


One more can be to use the math module of the Python. From this math module, we can use the built-in function sqrt() to find the square root the odd number. You can check the code for this also below.


import math
L = [math.sqrt(i) for i in range(1,21) if i%2 != 0]
print(L)


Output - 

[1.0, 1.7320508075688772, 2.23606797749979, 2.6457513110645907, 3.0, 3.3166247903554, 3.605551275463989, 3.872983346207417, 4.123105625617661, 4.358898943540674]


So, I hope you liked the article and you would have found the content of the article useful and helpful for you. Share this article in your coding communities so that more people can take advantage of the content. Also, In case of any doubt feel free to ask in the comments below.

For More Such Python Programs - Click Here

Enjoy Coding!!!


Also Read :-

Extract Phone Number Details Using Python





For any doubts feel free to ask in comments below.
Stay Connected to Us for more such Courses and Projects.

Post a Comment

For any doubts feel free to ask in comments below.
Stay Connected to Us for more such Courses and Projects.

Post a Comment (0)

Previous Post Next Post