# 5 examples of 'prime numbers from 1 to 10000' in Python

Every line of 'prime numbers from 1 to 10000' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

## All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
``20def prime(num):21    # num is actually a string because input() returns strings. We'll convert it to int22    num = int(num)2324    if num &lt; 0:25        print("Negative integers can not be prime")26        quit()27    if num is 1:28        print("1 is neither prime nor composite")29        # See how I lazily terminated program otherwise it'd forward "None"(default behaviour of python when function30        # returns nothing) rather than True or False. Which could mess up the program.31        # If we hit this if statement above statement is printed then program exits.32        quit()  # Now you don't need to get sys.exit() to exit python has quit to handle the same thing33    if num in [2, 3]:34        # if given argument is 2 or 3, it is prime. We used list without defining a variable which is perfectly valid35        return True36    if num % 2 == 0:  # excluding all even numbers except two.37        return False38    else:39        # Here we are starting counter variable from 3 in range. Second argument excludes numbers above one third40        # of the given argument. Third argument in range sets steps to take to 2. This makes loop to iterate odds41        for x in range(3, int(num/3), 2):42            # Checking if argument is divisible by counter. % is modulus operator which returns remainder of division43            if num % x == 0:44                return False45    # It's okay to have more than one return statement when program hits return statement it exits the function.46    return True``
``16def primeGen(n):17    for i in xrange(2, n):18        prime = True19        if i % 2 == 0 and i != 2:20            continue21        sqrtp = int(i ** 1 / 2)22        for j in xrange(2, sqrtp):23            if j % 2 == 0:24                continue25            if i % j == 0:26                prime = False27                break28        if prime:29            yield i``
``7def isPrime(num):8    # Returns True if num is a prime number, otherwise False.910    # Note: Generally, isPrime() is slower than primeSieve().1112    # all numbers less than 2 are not prime13    if num &lt; 2:14        return False1516    # see if num is divisible by any number up to the square root of num17    for i in range(2, int(math.sqrt(num)) + 1):18        if num % i == 0:19            return False20    return True``
``12def genprime(n):13  global isprime14  isprime = [True] * (n+1)15  sN = int(math.floor(math.sqrt(n)))1617  for i in range(3, n+1, 2): 18      if (isprime[i]):19          yield i20          if (i &lt; sN):21              ni = 2*i22              while (ni &lt;= n):23                  isprime[ni] = False24                  ni += i``
``12def primes():13    yield 214    yield 31516    for i in itertools.count(start=5, step=2):17        if is_prime(i):18            yield i``