My task was to create a leapyear program. The following was created:
def LeapYear(year):
if year % 400 == 0:
return True
elif year % 100 == 0:
return False
elif year % 4 == 0:
return True
else:
return False
print (LeapYear(400))
print (LeapYear(1000))
print (LeapYear(1012))
print (LeapYear(2000))
print (LeapYear(2010))
Very raw and basic as you can see - However this isn't an issue for me at the moment, im just figuring out the forumla's.
I understand this version with if, elif and else statements but i found there was another way of doing the same thing.
def isLeapYear(year):
"""
return True if year is a leap year
"""
return (not year%4 and year%100 or not year%400) and True
# Testing ...
print( isLeapYear(400) )
print( isLeapYear(800) )
print( isLeapYear(1600) )
print( isLeapYear(1601) )
print( isLeapYear(2004) )
Which is boolean logic right? Anyway i've figured out that:
not year%4
is the equivalent to
year%4 ==0
And i already understand truth values, the expression True AND True = True, the expression True AND True OR False = True... etc.
I just do not understand what this not is doing, not is negation, so not True is False but when its used in the example below
not year%4
I sort of know what its doing but i don't quite understand the concept of how?!
Some insight to the exact workings of this line would be much apperciated!
(not year%4 and year%100 or not year%400) and True