Python – Program for printing Prime Numbers from the List of Numbers

Python – Program for printing Prime Numbers from the List of Numbers

In this post, We will see how to write a python program for finding prime numbers from the list of numbers in different ways.

Click on this link : Prime Number

Example:

Input:  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Output:  Prime Number : -> 2, 3, 5, 7,

Input:  [15, 30, 45, 60]
Output:  No any number from the given list is Prime

Now, Let’s see the codes:

Code 1: Use of for-else python concept. due to this you don’t need to take flag variable.
for-else concept: if for loop run successfully i.e. for loop run completely then else block is not executed otherwise else block is executed.

# Code 1:

# Given number list
numberList = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Empty list
ansList = []

# Iterate through each number 
# form the list
for num in numberList :
    
    # 0 and 1 is not a 
    # prime number
    # so skip this number
    if num == 0 or num == 1 :
        continue
        
    # loop from 2 to half of the
    # given number
    for i in range(2, num // 2 + 1) :

        # If number is divisible by any
        # number (i) then it is not
        # a prime number
        if num % i == 0 :
            break

    # If not divisible then it is
    # a prime number
    else :
        
        # if number is prime
        # then append to the list
        ansList.append(num)

# If list is non-empty then
# print th elements
if len(ansList) :
    
    print("Prime Number : ",end = "-> ")
    
    # printing the prime number
    # from the ansList
    for ans in ansList :
        print(ans, end = ", ")
    
else :
    print("No any number from the given list is Prime")

Output:

Prime Number : -> 2, 3, 5, 7,

Code 2: Make a user defined function for checking number is prime or not.

# define a user defined 
# function for checking
# a number is prime or 
# not
def checkPrime(number) :

    # loop from 2 to half of the
    # given number
    for i in range(2, number // 2 + 1) :

        # if number is divisible by any
        # number (i) then it is not
        # a prime number
        if number % i == 0 :
            return 0

    # if not divisible then it is
    # a prime number
    else :
        return 1
        
# Main code
if __name__ == "__main__" :
    
    # Given number list
    numberList = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    
    # Empty list
    ansList = []
    
    # Iterate through each number 
    # form the list
    for num in numberList :

        # 0 and 1 is not a 
        # prime number
        # so skip this number
        if num == 0 or num == 1 :
            continue
        
        # function call for checking
        # a number is prime or not
        if checkPrime(num) :
            
            # if number is prime
            # then append to the list
            ansList.append(num)

    # If list is non-empty then
    # print th elements
    if len(ansList) :

        print("Prime Number : ",end = "-> ")

        # printing the prime number
        # from the ansList
        for ans in ansList :
            print(ans, end = ", ")

    else :
        print("No any number from the given list is Prime")

Output:

Prime Number : -> 2, 3, 5, 7,

Code 3: Use of map() and sum() built-in python function.

# Code 3:

# define a user defined 
# function for checking
# a number is prime or 
# not
def checkPrime(number) :
    
    # 0 and 1 is not a 
    # prime number
    # so skip this number
    if number == 0 or number == 1 :
        return 0

    # loop from 2 to half of the
    # given number
    for i in range(2, number // 2 + 1) :

        # if number is divisible by any
        # number (i) then it is not
        # a prime number
        if number % i == 0 :
            return 0

    # if not divisible then it is
    # a prime number
    else :
        # return prime number
        return number
        
# Main code
if __name__ == "__main__" :
    
    # Given number list
    numberList = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    
    # use of map built-in function to call
    # user defined function for all
    # element present in the list
    # and return final list
    ansList = list(map(checkPrime, numberList))

    # if sum of ansList is > 0 that 
    # means it contains prime number 
    # otherwise not
    if sum(ansList) :

        print("Prime Number : ",end = "-> ")

        # printing the prime number
        # from the ansList
        for ans in ansList :
            
            # if ans is non-zero 
            # then print the value
            if ans :
                print(ans, end = ", ")

    else :
        print("No any number from the given list is Prime")

Output:

Prime Number : -> 2, 3, 5, 7, 

Leave a Reply

Your email address will not be published.

📢 Need further clarification or have any questions? Let's connect!

Connect 1:1 With Me: Schedule Call


If you have any doubts or would like to discuss anything related to this blog, feel free to reach out to me. I'm here to help! You can schedule a call by clicking on the above given link.
I'm looking forward to hearing from you and assisting you with any inquiries you may have. Your understanding and engagement are important to me!

This will close in 20 seconds