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:
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:
- Hyphenate Letters in Python
- Earthquake in Python | Easy Calculation
- Striped Rectangle in Python
- Perpendicular Words in Python
- Free shipping in Python
- Raj has ordered two electronic items Python | Assignment Expert
- Team Points in Python
- Ticket selling in Cricket Stadium using Python | Assignment Expert
- Split the sentence in Python
- String Slicing in JavaScript
- First and Last Digits in Python | Assignment Expert
- List Indexing in Python
- Date Format in Python | Assignment Expert
- New Year Countdown in Python
- Add Two Polynomials in Python
- Sum of even numbers in Python | Assignment Expert
- Evens and Odds in Python
- A Game of Letters in Python
- Sum of non-primes in Python
- Smallest Missing Number in Python
- String Rotation in Python
- Secret Message in Python
- Word Mix in Python
- Single Digit Number in Python
- Shift Numbers in Python | Assignment Expert
- Weekend in Python
- Shift Numbers in Python | Assignment Expert
- Temperature Conversion in Python
- Special Characters in Python
- Sum of Prime Numbers in the Input in Python
- Numbers in String-1 in Python
- Replace Elements with Zeros in Python
- Remove Words in Python
- Print Digit 9 in Python | Assignment Expert