Power of Comprehension in python

Comprehension is unique feature of python which is not available in c and cpp and all……

Comprehension stands for decrese length of a code as max as possible…….

#code for finding numbers which satisfy these properties : divide by 6=5, divide by 5=4 upto .... 1
#Range is taken from user
#METHOD 1: (Normal)
a=int(input("Enter lower range : "))
b=int(input("Enter higher range : "))
l=[]
for n in range(a,b):
    if (n%6==5 and n%5==4 and n%4==3 and n%3==2 and n%2==1):
        l.append(n)
print(l)
        
#METHOD 2: (Comprehension)        
print([n for n in range(int(input("Enter lower range : ")),int(input("Enter higher range :"))) if n%6==5 and n%5==4 and n%4==3 and n%3==2 and n%2==1])

#CHALLANGE : try is logic in diff languages as small as possible. Then you will believe how much  python can do. 
Output>>>
Enter lower range : 1
Enter higher range : 1000
[59, 119, 179, 239, 299, 359, 419, 479, 539, 599, 659, 719, 779, 839, 899, 959] 

Sharing is caring!

1 reply on “Power of Comprehension in python”

  • Deep Joshi
    13/10/2020 at 10:35 PM

    Great Concept πŸ‘πŸ»πŸ‘πŸ»πŸ‘πŸ» Python is Awesome Language πŸ”₯

Leave a Reply

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