-2

So, I'm learning Python through a series of videos on YT and this time, while loops are being covered. The exemple code is thus:

given_list2=[5,4,4,3,1,-2,-3,-5]
total3=0
i=0
while True:
    total3+=given_list2[i]
    i+=1
    if given_list2[i]<=0:
        break
print(total3)

Running the script, I get 17 as a result. Then, experimenting with the code, I exchanged True for False as thus:

given_list2=[5,4,4,3,1,-2,-3,-5]
total3=0
i=0
while False:
    total3+=given_list2[i]
    i+=1
    if given_list2[i]<=0:
        break
print(total3)

And it gives me 0 as a result. I'm trying to understand why that's the case. Like, what is it being considered True that keeps running the code, and what is being considered False that it fails to even initialize the code?

AnFa
  • 47
  • 2
  • 9
  • `True` and `False` are getting evaluated as themselves. `while False:` never enters the loop... `while True` never terminates unless you break or return from inside the loop (return only in a function, fo course) – juanpa.arrivillaga Jan 16 '19 at 23:10

3 Answers3

0

The reason why the answer is 0 is because while False means that the body of the loop is not going to be executed even a single time, and since total3 is only incremented in the body loop, its value is going to stay the same as before the loop, which is 0 because of total3=0 line above it.

In order for the loop body to execute the value of the expression after while should be truthy. The most common truthy value is True.

Taras Tsugrii
  • 832
  • 6
  • 5
-1

A while loop evaluates a condition and executes the code in its block when the condition evaluates to True, otherwise it exits the loop. The condition True causes the loop to continue infinitely since it can only ever evaluate to True, while False causes the loop to immediately exit without running the code in its block.

I know this is only an example of how to use a while loop, however, had this been an actual use case you'd want to use a for loop instead.

given_list2 = [5, 4, 4, 3, 1, -2, -3, -5]
total3 = 0

for n in given_list2:
    if n > 0:
        total3 += n
    else:
        break

print(total3)

or even

total3 = sum(n for n in given_list2 if n > 0)
nicholishen
  • 2,602
  • 2
  • 9
  • 13
-1

True and False are boolean literal values. That is, their values are known and set by the language. Imagine if you had something like:

while 1 < 2:

The "1" and the "2" are integer literal values. The expression is never changing, the results will always be the same. In this case, the result is a boolean value equal to True.

So a while loop that has "True" or any unchanging true expression, like 1 < 2, as the condition, is going to want to run "forever" because it will never fail that test. The only way to stop such a loop is to generate a keyboard exception (usually by pressing "Ctrl-C"), or to have an uncaught exception occur inside the code somewhere, or to have some piece of code execute a break statement.

In your example, you are adding up the numbers in the given_list2 and stop (by executing a break) when you encounter a negative number. So the positive numbers are summed, which is 17.

Similarly, a while loop that has "False" or any unchanging false expression as the condition is never going to run, because the very first test of while 1 > 2 will fail and the loop will abort. This results in none of the inside code being executed.

In your example, you start with total3 = 0 and never run any code, so it stays 0.

aghast
  • 14,785
  • 3
  • 24
  • 56
  • 1
    Please don't encourage low-quality questions by answering them -- if you see something that has duplicates, shows little research effort, or both, just link to the duplicate. – 0xdd Jan 17 '19 at 14:51