# 10 examples of 'print prime numbers from 1 to n in python' in Python

Every line of 'print prime numbers from 1 to n in python' 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``
``1def is_prime(n):2	if n &lt; 2:3		return False45	i = 267	while i * i &lt;= n:8		if n % i == 0:9			return False1011		i += 11213	return True``
``3def is_prime(n):4    i = 35    while i * i &lt;= n:6        if n % i == 0: return False7        i += 28    return True``
``6def is_prime(n):7   '''8   checks if a number is prime9   '''10   if n &lt; 2:11      return False12   if n == 2:13      return True14   for x in range(2, int(n**0.5)+1, 2):15      if n % x == 0:16         return False17   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``
``746def prime_number_factorisation(n):747    if n &lt; 2:748        return [n]749    i = 2750    factors = []751    while i * i &lt;= n:752        if n % i:753            i += 1754        else:755            n //= i756            factors.append(i)757    if n &gt; 1:758        factors.append(n)759    return factors``
``3def is_prime(n):4    if n &lt;= 1:5        return False6    elif n == 2:7        return True8    elif n % 2 == 0:9        return False10    for i in xrange(3, int(sqrt(n))+1, 2):11        if n % i == 0:12            return False13    return True``
``21def is_prime(n):22    for i in xrange(2, int(sqrt(n)) + 1):23        if n % i == 0:24            return False2526    return True``
``36def primes_from(prime_sieve):37    for n, is_prime in enumerate(prime_sieve):38        if is_prime:39            yield n``
``50def prime_factors(n):51    """Lists prime factors of a given natural integer, from greatest to smallest52    :param n: Natural integer53    :rtype : list of all prime factors of the given natural n54    """55    i = 256    while i &lt;= sqrt(n):57        if n % i == 0:58            l = prime_factors(n/i)59            l.append(i)60            return l61        i += 162    return [n]      # n is prime``