Learning coding means GreatToCode Be more than a Coder ! Greattocode , Join GreatToCode Community,1000+ Students Trusted On Us .If You want to learn coding, Then GreatToCode Help You.No matter what It Takes !


CODE YOUR WAY TO A MORE FULFILLING And HIGHER PAYING CAREER IN TECH, START CODING FOR FREE Camp With GreatToCode - Join the Thousands learning to code with GreatToCode
Interactive online coding classes for at-home learning with GreatToCode . Try ₹Free Per Month Coding Classes With The Top Teachers . Python programs to return prime numbers from a list

Python programs to return prime numbers from a list

Become More Then A coder | Learn & Start Coding Now.

Here are Python programs to return prime numbers from a list using different approaches:


### 1. Using a Function:

```python
# Program 1: Returning prime numbers from a list using a function
def is_prime(num):
    if num <= 1:
        return False
    for i in range(2, int(num ** 0.5) + 1):
        if num % i == 0:
            return False
    return True

def get_prime_numbers(numbers):
    return [num for num in numbers if is_prime(num)]

numbers = [2, 3, 4, 5, 6, 7, 8, 9, 10]
print("Prime numbers:", get_prime_numbers(numbers))
```

### 2. Using Filter Function:

```python
# Program 2: Returning prime numbers from a list using filter function
def is_prime(num):
    if num <= 1:
        return False
    for i in range(2, int(num ** 0.5) + 1):
        if num % i == 0:
            return False
    return True

numbers = [2, 3, 4, 5, 6, 7, 8, 9, 10]
prime_numbers = list(filter(is_prime, numbers))
print("Prime numbers:", prime_numbers)
```

### 3. Using List Comprehension:

```python
# Program 3: Returning prime numbers from a list using list comprehension
def is_prime(num):
    if num <= 1:
        return False
    for i in range(2, int(num ** 0.5) + 1):
        if num % i == 0:
            return False
    return True

numbers = [2, 3, 4, 5, 6, 7, 8, 9, 10]
prime_numbers = [num for num in numbers if is_prime(num)]
print("Prime numbers:", prime_numbers)
```

### 4. Using While Loop:

```python
# Program 4: Returning prime numbers from a list using while loop
def is_prime(num):
    if num <= 1:
        return False
    i = 2
    while i * i <= num:
        if num % i == 0:
            return False
        i += 1
    return True

numbers = [2, 3, 4, 5, 6, 7, 8, 9, 10]
prime_numbers = [num for num in numbers if is_prime(num)]
print("Prime numbers:", prime_numbers)
```

### 5. Using Sieve of Eratosthenes Algorithm:

```python
# Program 5: Returning prime numbers from a list using Sieve of Eratosthenes algorithm
def sieve_of_eratosthenes(n):
    primes = [True] * (n + 1)
    primes[0], primes[1] = False, False
    p = 2
    while p * p <= n:
        if primes[p] == True:
            for i in range(p * p, n + 1, p):
                primes[i] = False
        p += 1
    return [num for num in range(n + 1) if primes[num]]

numbers = [2, 3, 4, 5, 6, 7, 8, 9, 10]
prime_numbers = sieve_of_eratosthenes(max(numbers))
print("Prime numbers:", prime_numbers)
```

These programs return prime numbers from a given list of numbers using different approaches - using a function, filter function, list comprehension, while loop, and Sieve of Eratosthenes algorithm. Feel free to replace the `numbers` variable with any desired list of numbers to find their prime counterparts.

Post a Comment

0 Comments

•Give The opportunity to your child with GreatToCode Kid's • Online Coding Classes for Your Kid • Introduce Your kid To the world's of coding
•Fuel You Career with our 100+ Hiring Partners, Advance Your Career in Tech with GreatToCode. •Join The Largest Tech and coding Community and Fast Forward Your career with GreatToCode. •10000+ Learner's+ 90 % placement Guarantee. • Learning Coding is Better with the GreatToCode community .
•Greattocode Kid's •GreatToCode Career •GreatToCode Interview •GreatToCode Professional •GreatToCode for schools •GreatToCode For colleges •GreatToCods For Businesses.
Are you ready to join the millions of people learning to code? GreatToCode Pass is your one-stop-shop to get access to 1000+ courses, top-notch support, and successful job placement. What are you waiting for? Sign up now and get your future in motion with GreatToCode Pass.