Succint Code for finding Prime Numbers in a provided Range

So, this idea just came in mind of using list comprehensions and returning lists from a function to check if it is prime or not.

Basically we are returning list with True or False in it and will check that if there is not even one False in the list then take that number in the ans list and all this will happen inside the print only, including the input of the range(n). In the parameters of primeOrNot I have used a typeHint which is basically used to tell the type of a variable and is optional, mainly used in assertiveness of some programs. 

def primeOrNot(n:int):

    '''Tells whether no. is prime or not'''

    if n<2 or n%2==0:return [False]

    return [False for i in range(3,n,2) if n%i==0 ]

    return [True]

    

'''Will give range of prime numbers in one line including input'''

print([i for i in range(int(input("What's a? ")),int(input("What's b? "))+1) if False not in primeOrNot(i)])

Comments

Popular Posts