﻿ Python - Check if each number is prime in a list of numbers

# Python Exercises: Check if each number is prime in a list of numbers

## Python List: Exercise - 17 with Solution

Write a Python program to check if each number is prime in a given list of numbers. Return True if all numbers are prime otherwise False.

Sample Data:
([0, 3, 4, 7, 9]) -> False
([3, 5, 7, 13]) -> True
([1, 5, 3]) -> False

Sample Solution-1:

Python Code:

``````# Define a function named 'test' that takes a list 'nums' as a parameter
def test(nums):
# Use a generator expression to check if each number in 'nums' is prime, and return True if all are prime
return all(is_prime(i) for i in nums)

# Define a function named 'is_prime' that checks if a number 'n' is prime
def is_prime(n):
# Check if 'n' is equal to 1; if so, it's not prime
if (n == 1):
return False
# Check if 'n' is equal to 2; if so, it's prime
elif (n == 2):
return True
else:
# Iterate through numbers from 2 to 'n' - 1
for x in range(2, n):
# If 'n' is divisible by 'x', it's not prime; return False
if (n % x == 0):
return False
# If no divisors were found, 'n' is prime; return True
return True

# Define a list 'nums' containing a sequence of numbers
nums = [0, 3, 4, 7, 9]
# Print the original list of numbers
print("Original list of numbers:")
print(nums)
# Check if each number in 'nums' is prime and print the result
print("Check if each number is prime in the said list of numbers:")
print(test(nums))

# Reassign 'nums' with a different list of numbers
nums = [3, 5, 7, 13]
# Print the original list of numbers
print("\nOriginal list of numbers:")
print(nums)
# Check if each number in the new 'nums' list is prime and print the result
print("Check if each number is prime in the said list of numbers:")
print(test(nums))

# Reassign 'nums' with another list of numbers
nums = [1, 5, 3]
# Print the original list of numbers
print("\nOriginal list of numbers:")
print(nums)
# Check if each number in the new 'nums' list is prime and print the result
print("Check if each number is prime in the said list of numbers:")
print(test(nums))
```
```

Sample Output:

```Original list of numbers:
[0, 3, 4, 7, 9]
Check if each number is prime in the said list of numbers:
False

Original list of numbers:
[3, 5, 7, 13]
Check if each number is prime in the said list of numbers:
True

Original list of numbers:
[1, 5, 3]
Check if each number is prime in the said list of numbers:
False
```

Flowchart:

Sample Solution-2:

Python Code:

``````def test(nums):
result = [is_prime(i) for i in nums]
return all(result)

def is_prime(n):
if (n==1):
return False
elif (n==2):
return True;
else:
for x in range(2,n):
if(n % x==0):
return False
return True

nums = [0, 3, 4, 7, 9]
print("Original list of numbers:")
print(nums)
print("Check if each number is prime in the said list of numbers:")
print(test(nums))
nums = [3, 5, 7, 13]
print("\nOriginal list of numbers:")
print(nums)
print("Check if each number is prime in the said list of numbers:")
print(test(nums))
nums = [1, 5, 3]
print("\nOriginal list of numbers:")
print(nums)
print("Check if each number is prime in the said list of numbers:")
print(test(nums))
```
```

Sample Output:

```Original list of numbers:
[0, 3, 4, 7, 9]
Check if each number is prime in the said list of numbers:
False

Original list of numbers:
[3, 5, 7, 13]
Check if each number is prime in the said list of numbers:
True

Original list of numbers:
[1, 5, 3]
Check if each number is prime in the said list of numbers:
False
```

Flowchart:

Python Code Editor:

Previous Python Exercise: Generate and print a list of first and last 5 elements where the values are square of numbers between two numbers.
Next Python Exercise: Generate all permutations of a list in Python.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿