It is a beginner's question, how come the output is only number 1 in this code. is it supposed to be all numbers?
a = [1, 2, 3, []]
for number in a :
if number == True:
print(number)
I expected to be:
1
2
3
but it only get:
1
It is a beginner's question, how come the output is only number 1 in this code. is it supposed to be all numbers?
a = [1, 2, 3, []]
for number in a :
if number == True:
print(number)
I expected to be:
1
2
3
but it only get:
1
Copyright © 2021 Jogjafile Inc.
Just use
That will cover all non-zero numbers (and other objects) as they are "truthy" as in
bool(number) == True.If you compare
number == True, you only get1since1 == Truebut not2 == True.Note that
boolis a subclass ofintwith0 == Falseand1 == True.