Search code examples
pythonboolean-expression

Why is year 1900 not returning False?


def is_leap_year(year):
    if (year%4==0 & (year%100!=0)) | (year%4==0 & year%100==0 & year%400==0):
        return True
    else:
        return False
print(is_leap_year(1900))

I would like for the argument (1900) to yield a False, since both sides of the or ( | ) statement fails to be true. What am I missing? This is written in Python.


Solution

  • Try

    def is_leap_year(year):
        if (year%4==0 and year%100!=0) or (year%4==0 and year%100==0 and year%400==0):
            return True
        else:
            return False
    print(is_leap_year(1900))
    

    & and | are bitwise AND and OR respectively, not logical operators.