I like Python a lot because you can almost directly translate spoken language into it.
Lets observe objective: "Every year that is exactly divisible by four is a leap year, except for years that are exactly divisible by 100, but these centurial years are leap years if they are exactly divisible by 400."
So 'every year that is exactly divisible by four':
Python first evaluates 'and': if year is divisible by four and it does not end with 00. If so - it's leap year (True), however, if it evaluates to False on years ending with 00 then 'or' clause is executed which checks whether year is divisible by 400. This way 1900 is False and 2000 is True.
Lets observe objective: "Every year that is exactly divisible by four is a leap year, except for years that are exactly divisible by 100, but these centurial years are leap years if they are exactly divisible by 400."
So 'every year that is exactly divisible by four':
year % 4 == 0'except for years that exactly divisible by 100':
year % 100 != 100'but these centurial years are leap years if they are exactly divisible by 400'
year % 400 == 0And together:
if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:EDIT: some explanation about order
Python first evaluates 'and': if year is divisible by four and it does not end with 00. If so - it's leap year (True), however, if it evaluates to False on years ending with 00 then 'or' clause is executed which checks whether year is divisible by 400. This way 1900 is False and 2000 is True.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.