- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program to Find all Numbers in a Range which are Perfect Squares and Sum of all Digits in the Number is Less than 10
When it is required to find all numbers in a range where there are perfect square, and sum of digits in the number is less than 10, list comprehension is used.
Below is the demonstration of the same −
Example
lower_limit = int(input(“Enter the lower range: “)) upper_limit = int(input(“Enter the upper range: “)) my_list = [] my_list = [x for x in range(lower_limit,upper_limit+1) if (int(x**0.5))**2==x and sum(list(map(int,str(x))))<10] print(“The result is : “) print(my_list)
Output
Enter the lower range: 5 Enter the upper range: 12 The result is : [9]
Explanation
The lower range and upper range are taken by the user.
An empty list is defined.
The list comprehension is used, to iterate over the lower and upper limits.
The square root of the elements are found.
The elements are summed up.
It is converted to a list.
This is assigned to a variable.
The output is displayed on the console.
Advertisements