Generate a List of Prime Numbers in Python

To generate a list of prime numbers in Python, we can use various methods, including iteration, the Sieve of Eratosthenes, and list comprehensions. These approaches help identify prime numbers efficiently by checking divisibility conditions.


Examples

1. Using a Simple Loop and Condition

This method iterates through numbers and checks divisibility using a for loop.

</>
Copy
# Function to check if a number is prime
def is_prime(n):
    if n < 2:
        return False
    for i in range(2, int(n ** 0.5) + 1):
        if n % i == 0:
            return False
    return True

# Generate a list of prime numbers up to 50
prime_numbers = [num for num in range(2, 51) if is_prime(num)]

# Printing the prime numbers list
print("Prime Numbers:", prime_numbers)

Explanation:

We define a function is_prime(n) that checks whether a number is prime. It loops from 2 to the square root of n to determine divisibility. If the number is only divisible by 1 and itself, it is considered prime. We then use list comprehension to generate primes between 2 and 50.

Output:

Prime Numbers: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]

2. Using the Sieve of Eratosthenes

The Sieve of Eratosthenes is an efficient algorithm for generating prime numbers by marking multiples of each prime.

</>
Copy
# Function to generate prime numbers using the Sieve of Eratosthenes
def sieve_of_eratosthenes(limit):
    primes = [True] * (limit + 1)
    primes[0], primes[1] = False, False

    for num in range(2, int(limit ** 0.5) + 1):
        if primes[num]:
            for multiple in range(num * num, limit + 1, num):
                primes[multiple] = False

    return [num for num, is_prime in enumerate(primes) if is_prime]

# Generate prime numbers up to 50
prime_numbers = sieve_of_eratosthenes(50)

# Printing the prime numbers list
print("Prime Numbers:", prime_numbers)

Explanation:

The sieve_of_eratosthenes(limit) function creates a Boolean list, initially assuming all numbers are prime. It then iterates through numbers, marking multiples as non-prime. The remaining unmarked numbers are prime.

Output:

Prime Numbers: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]

3. Using a Lambda Function and Filter

We can use the filter() function along with a lambda function to generate prime numbers in a concise way.

</>
Copy
# Lambda function to check for prime numbers
is_prime = lambda n: n > 1 and all(n % i != 0 for i in range(2, int(n ** 0.5) + 1))

# Using filter() to generate a list of prime numbers
prime_numbers = list(filter(is_prime, range(2, 51)))

# Printing the prime numbers list
print("Prime Numbers:", prime_numbers)

Explanation:

The lambda function is_prime checks whether a number is prime using a generator expression inside all(). The filter() function applies this check over the range of numbers, returning only primes.

Output:

Prime Numbers: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]

Conclusion

There are multiple ways to generate a list of prime numbers in Python, each suited for different use cases:

  1. Loop and Condition: A simple approach that iterates through numbers and checks divisibility.
  2. Sieve of Eratosthenes: A highly efficient algorithm for generating large prime lists.
  3. Lambda and Filter: A concise way to generate primes using functional programming techniques.

For small numbers, the loop method is sufficient. For large numbers, the Sieve of Eratosthenes is recommended due to its efficiency.