Create a Python function that takes a list of integers as input andreturns a new list containing only the prime numbers from the input list.Provide a clear explanation of your implementation and include a test case.
Question
Create a Python function that takes a list of integers as input andreturns a new list containing only the prime numbers from the input list.Provide a clear explanation of your implementation and include a test case.
Solution
Sure, here is a Python function that filters out the prime numbers from a given list of integers.
def filter_primes(num_list):
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
primes = [num for num in num_list if is_prime(num)]
return primes
Explanation:
- The function
filter_primestakes a list of integers as input. - Inside this function, we define another function
is_primethat checks if a given number is prime. is_primefunction works by checking if the number is less than 2 (the smallest prime number) and returns False if it is.- If the number is 2 or greater, it checks if the number can be divided evenly by any number from 2 to the square root of the number. If it can, it returns False (meaning the number is not prime).
- If the number cannot be divided evenly by any other number (other than 1 and itself), it returns True (meaning the number is prime).
- We then use a list comprehension to create a new list that contains only the numbers from the input list that are prime. This is done by checking each number with the
is_primefunction. - Finally, the function returns the list of prime numbers.
Test case:
print(filter_primes([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]))
This should output: [2, 3, 5, 7]
Similar Questions
Write a program primes.py that reads a positive integer from standard input, and determines whether or not the number is prime.
Unique PrimeSend FeedbackProblem Statement:Write a program that takes a list of numbers as input and prints all the unique prime numbers from the list in a single line. The program should contain two functions: one to check if a number is prime and another to return the unique values from the list.Input:A list of numbers.Output:The function should return a single line containing the unique prime numbers from the list.Sample Input:[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13]Sample Output:2 3 5 7 11 13
prime number in python
# Write a function in python which accept a number from user to return True, if the number#is a prime number else return False. Use this function to print all prime numbers from 1#to 100.
A prime number is an integer greater or equal to 2 that is only divisible by 1 and by itself. The first few primes are: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47 …N is a prime if and only if it is not divisible evenly by any of the numbers from 2 to N−1. Let’s implement this decision as a function.In the same program numbers.cpp, add a functionbool isPrime(int n);The function should return true if n is a prime, otherwise return false. Change the main function to test your new code.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.