Count Prime Numbers in List

Count how many prime numbers are present in a list of integers.

IntermediateTopic: List Programs
Back

Python Count Prime Numbers in List Program

This program helps you to learn the fundamental structure and syntax of Python programming.

Try This Code
# Program to count prime numbers in a list

def is_prime(n: int) -> bool:
    if n < 2:
        return False
    for i in range(2, int(n ** 0.5) + 1):
        if n % i == 0:
            return False
    return True

numbers = list(map(int, input("Enter integers separated by space: ").split()))

count = sum(1 for x in numbers if is_prime(x))

print("Number of primes in list:", count)
Output
Enter integers separated by space: 2 3 4 5 6
Number of primes in list: 3

Understanding Count Prime Numbers in List

We define a helper is_prime and count how many list elements satisfy it.

Note: To write and run Python programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Python Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Python programs.

Table of Contents