Sum of all the non-negative and non-zero numbers in a list

Hello, we are supposed to start a new project today but because I am busy doing something else, therefore, I only post a simple solution for one of the questions on codewars. We will start our next python project in the next chapter.

Codewars is the place you all will want to visit to brush up your python skill if you are really serious about learning python. Although some questions on codewars are really tricky and some of them make people hard to understand but overall speaking this is the only place which provides real deal for those python programmer who wants to perfect their python skill. If you find out that some questions on codewars have provided a wrong answer and make your head spins like crazy then just ignore that one and move on to the next one instead.

Given a list of numbers, return the total of those positive numbers.

def positive_sum(arr):
    sum = 0
    for i in arr:
        if(i > 0):
            sum += i
    return sum

Here you have it, a short answer to the above python question. We are relaxing today but in the next chapter onward thing will become complicated once we have started our next python project. Do like and share the below tweet on this post and do follow me on twitter from the below twitter widget. We can talk more about python on twitter.

Leave a Reply

2 Comment threads
0 Thread replies
Most reacted comment
Hottest comment thread
2 Comment authors
Conner P.Mr. X Recent comment authors
newest oldest most voted
Notify of
Mr. X
Mr. X

sum(x for x in arr if x > 0)

Conner P.
Conner P.

I would argue that the following is more pythonic:

sum([x for x in _list if x>0]