[Python] is_prime one liner in python

Discussão em 'Python' iniciado por Stack, Fevereiro 18, 2025.

  1. Stack

    Stack Membro Participativo

    I tried writing a function that returns 'True' if the given parameter is a prime number and 'False' otherwise. In a sense, I achieved my goal but I just wanted to know of a better, more pythonic way to do it, here's what I came up with:

    def prime_one_liner(n):
    return True if len([i for i in range(2,n) if n % i == 0]) == 0 else False

    Continue reading...

Compartilhe esta Página