could you explain what the purpose of this code is just by looking at it? [closed]
could you explain what the purpose of this code is just by looking at it? [closed]
result = 0
for num in numbers:
is num < 0:
result = result + 1
Yes, I could. So could almost all programmers with a few months of general programming experience or a month of Python-specific experience. Why do you ask? Is something specific confusing you?
– ShadowRanger
Aug 30 at 4:54
and BTW, this code doesn't work
– U9-Forward
Aug 30 at 4:55
@U9-Forward: Heh. Missed that little typo of
is
for if
.– ShadowRanger
Aug 30 at 4:56
is
if
This is not a question on stack-overflow, post it somewhere else
– U9-Forward
Aug 30 at 4:58
3 Answers
3
That isn't valid Python - you need to preserve the whitespace/tabbing, and you have is
instead of if
:
is
if
result = 0
for num in numbers:
if num < 0:
result = result + 1
The code will count how many negative numbers are in the list numbers
. For example:
numbers
>>> numbers = [-2,-1,0,1,2,3,4]
>>> for num in numbers:
... if num < 0:
... result = result + 1
...
>>> result
2
This previous question shows a simpler algorithm using a list comprehension:
Count negative values in a list of integers using Python?
Why is this algorithm inefficient? Your link shows a generator expression with
sum
. Fundamentally, the algorithm will be the same, so algorithmic complexity will be the same. I suspect the actual runtime will be very similar too.– juanpa.arrivillaga
Aug 30 at 5:00
sum
sum(1 for i in num if i < 0)
is slower.– HSK
Aug 30 at 5:04
sum(1 for i in num if i < 0)
@HSK for such a small list, undoubtedly the generator expression will be slower because of the generator overhead. As the number of items in the list grows, they'll probably become comparable.
– juanpa.arrivillaga
Aug 30 at 5:07
@juanpa.arrivillaga I really agree with you
– U9-Forward
Aug 30 at 5:08
@juanpa.arrivillaga That's true, but wouldn't the generator expression always be slower for lists? I tested it using
list(range(-2000000, 2000000))
but it is still slower.– HSK
Aug 30 at 5:10
list(range(-2000000, 2000000))
i dont know where has numbers
list (probably) been defined, but i guess it may contains user input numbers.
the loop is counting the number of negative numbers in the numbers
list, and store it in result
variable.
numbers
numbers
result
What you posted is not valid python code you want if
not is
, see the explanation in comments:
if
is
result = 0 # Create a variable and assign it to zero
for num in numbers: # Iterate trough `numbers` list (or tuple or set..) whatever it is
if num < 0: # Check if the current element smaller than zero
result = result + 1 # if it is add 1 to variable `result`
And remember next time to ask a question please see How to Ask and How to create a Minimal, Complete, and Verifiable example
Alternatively you can use this:
result = sum(1 for i in numbers if i<0)
Also alternatively you can do abs(i)!=i
instead of i<0
abs(i)!=i
i<0
I'm voting to close this question as off-topic because this question just sets the title to `could you explain this code and inside this question is just code
– U9-Forward
Aug 30 at 4:53