Sum of non-primes in Python

Problem Statement:

In the Sum of non-primes in Python, we are given a list of numbers, we need to find the sum of non-prime numbers in the given list.

Code for Sum of non-primes in Python:

# declaring list of numbers
numbers = [2, 35, 5634, 12, 354, 87, 34, 123, 43, 67, 34, 98, 13, 11]

# declaring empty list
non_primes =[]

# declaring for loop
for i in numbers:
    flag=1
    # checking whether a number is prime or not
    for j in range(2, i//2+1):
        if i%j==0:
            flag=0
            break
    # storing non prime numbers only
    if flag==0:
        non_primes.append(i)

# printing sum of non primes
print(sum(non_primes))

Output:

Sum of non-primes in Python

Explanation:

We simply stored non-prime numbers in an empty list by filtering the original list containing all numbers. At last, we simply printed the sum with the help of sum() method(a built-in method for list) in Python. Please comment for any queries


Also Read:

Share:

Author: Harry

Hello friends, thanks for visiting my website. I am a Python programmer. I, with some other members, write blogs on this website based on Python and Programming. We are still in the growing phase that's why the website design is not so good and there are many other things that need to be corrected in this website but I hope all these things will happen someday. But, till then we will not stop ourselves from uploading more amazing articles. If you want to join us or have any queries, you can mail me at admin@copyassignment.com Thank you