Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is the "largest" variable set to None if it can also be set to 0?

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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 []".

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading