Common Python Coding Interview Questions: Part 5
A breakdown of a common Python problem given in interviews
Published in
3 min readAug 14, 2020
Write a function that takes in a year and returns ‘True’ or ‘False’ whether that year is a leap year. In the Gregorian calendar, three conditions are used to identify leap years:
- The year can be evenly divided by 4, is a leap year, unless:
- The year can be evenly divided by 100, it is NOT a leap year, unless:
- The year is also evenly divisible by 400. Then it is a leap year.
def is_leap(year):
leap = False
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
leap = True
else:
leap = True
return leap
Step 1: Declare the function
Since you are asked to write a function, it’s important to do so! Right away you should type ‘def’ and come up with an appropriate name for your function. Since the prompt tells you that the function will be taking in a year, be sure to include one argument in your function declaration that will represent that year.