• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Count Primes Python – How to Count Number of Primes in a List

Count Primes Python – How to Count Number of Primes in a List

February 8, 2022 Leave a Comment

In Python, we can count the number of primes in a list by defining a function to check if a number is prime, and then looping through the list and adding up the count.

def isPrime(n):
    if (n % 2 == 0):
        return False
    for i in range(3, int(n**0.5 + 1), 2):
        if (n % i == 0):
            return False
    return True

def countPrimes(list_of_numbers):
    count = 0
    for num in list_of_numbers:
        if isPrime(num):
            count = count + 1
    return count

print(countPrimes([3,10,32,13,70]))

#Output: 
2

When working with lists of numbers, sometimes it can be useful to be able to count the number of primes.

In Python, we can count the number of primes in a list easily.

To count all the primes in a list, we can first define a function which checks if a number is prime.

def isPrime(n):
    if (n % 2 == 0):
        return False
    for i in range(3, int(n**0.5 + 1), 2):
        if (n % i == 0):
            return False
    return True

Then, to count the number of primes in a list, all we need to do is loop over each element in the list and count the primes.

Below is a Python function which will count the number of primes in a list.

def isPrime(n):
    if (n % 2 == 0):
        return False
    for i in range(3, int(n**0.5 + 1), 2):
        if (n % i == 0):
            return False
    return True

def countPrimes(list_of_numbers):
    count = 0
    for num in list_of_numbers:
        if isPrime(num):
            count = count + 1
    return count

print(countPrimes([3,10,32,13,70]))

#Output: 
2

Counting All of the Primes in a Range of Numbers

We can also use our isPrime() function to count the number of primes in a range of numbers.

To get the number of primes in a range, we can define a function which will take in two numbers, the endpoints of our range, and then loop over the odd numbers in that range.

A few things to consider in our prime counting function. First, if one of the endpoints is 2, then we should also add another to our count. Also, if the bottom endpoint is even, we need to make it odd.

Below is a Python function which will get the number of primes between two numbers.

def getPrimeCount(a,b):
    if a > 1 and b > 1:
        count = 0
        if a > b:
            t = a
            a = b
            b = t
        if a == 2:
            count = 1
            a = 3
        if a % 2 == 0: 
            a = a + 1
        while (a < b):
            if(isPrime(a)):
                count = count + 1
            a = a + 2
        return count
    else:
        return "Not a valid range."

print(getPrimeCount(3,13))
print(getPrimeCount(100,1000))
print(getPrimeCount(200,400))
print(getPrimeCount(21,34))

#Output:
4
143
32
3

Hopefully this article has been helpful for you to count the number of primes in a list using Python.

Other Articles You'll Also Like:

  • 1.  Get pandas Index Values as List in Python
  • 2.  Check if String Contains Only Certain Characters in Python
  • 3.  Using Lambda Expression with max() in Python
  • 4.  Python Decrement Counter with -= Decrement Operator
  • 5.  Length of Dictionary Python – Get Dictionary Length with len() Function
  • 6.  Using readlines() and strip() to Remove Spaces and \n from File in Python
  • 7.  PROC FREQ Equivalent in Python
  • 8.  Using Selenium to Close Browser in Python
  • 9.  Change Column Name in pandas DataFrame
  • 10.  Using Python to Get All Combinations of Two Lists

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy