Stray - 5 months ago 51

Python Question

I am trying to figure out a way to return true if input is divisible by 4, but not 100 unless also by 400. I've unsuccessfully tried:

`def is_leap(year):`

leap = False

if [year % 100 == 0 if not year % 400]:

leap = False

else:

if leap % 4 == 0:

leap = True

return leap

input of 2100 is divisible by 4 and 100 but not 400, so I want it to return False.

I've come to a simple conclusion of

`def is_leap(year):`

leap = False

if year % 4 == 0 and year % 400 == 0:

leap = True

elif year % 4 == 0 and year % 100 != 0:

leap = True

return leap

But is there an easier or different way to do an unless? Thank you.

Also, while I am primarily leaning towards python3.5 for what everyone's doing, I enjoy the simplicity of 2.7. Any help in either would be appreciated.

Answer

I'll just point out how `calendar.isleap`

does it:

```
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
```