I was reading a pdf so that I could learn python however I still don’t really understand "None". Here is the code:
largest = None
print('Before:', largest)
for itervar in [3, 41, 12, 9, 74, 15]:
if largest is None or itervar > largest :
largest = itervar
print('Loop:', itervar, largest)
print('Largest:', largest)
I replaced the "None" with 0 instead and the code worked completely fine. So why would you not use 0 instead?
>Solution :
None just indicates that you haven’t looked at any value yet. (If you had, one of them would be the value of largest, not None.)
0 works as an initial value if your list contains at least one non-negative value. Otherwise, you’ll get an odd result like "0 is the largest value in [-1, -2]" or "0 is the largest value in []".