def isprime(n):
    """ Check if n is a prime number.
    Sample usage:
    >>> isprime(0)
    False
    >>> isprime(1)
    True
    >>> isprime(2)
    True
    >>> isprime(3)
    True
    >>> isprime(4)
    False
    >>> isprime(5)
    True
    """

    if n<=0:
        # This is only for numbers>0.
        return False
    for x in range(2,n):
        if n%x==0:
            return False
        else:
            pass
    return True

def _test():
    import doctest
    doctest.testmod()

if __name__ == "__main__":
    _test()

This code is part of the book "Python for Bioinformatics", by Sebastian Bassi (sbassi@genesdigitales.com). Return to home page.