We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

- Prepare
- Python
- Introduction
- Write a function
- Discussions

# Write a function

# Write a function

+ 0 comments def is_leap(year): leap = False # Write your logic here if year>=1900 and year<=10**5: if year%4!= 0: leap = False elif year%4==0 and year%100!=0: leap = True elif year%4==0 and year%100==0 and year%400!=0: leap = False elif year%4==0 and year%100==0 and year%400==0: leap = True return leap year = int(input()) print(is_leap(year))

+ 0 comments Three conditions are used to identify leap years: The year can be evenly divided by 4, is a leap year,

if year % 4 == 0: leap == True

Unless, the year can be evenly divided by 100, it is NOT a leap year,

if year % 100 == 0: leap == False

Unless, the year is also evenly divisible by 400. Then it is a leap year.

if year % 400 == 0: leap == True

EXAMPLE:

def is_leap(year): leap = False # Write your logic here if year % 4 == 0: leap = True if year % 100 == 0: leap = False if year % 400 == 0: leap = True return leap year = int(input()) print(is_leap(year))

+ 0 comments **This is a simple solution just by doing the checks and return the value of the checks (True or False):**def is_leap(year): return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0) year = int(input()) print(is_leap(year))

+ 0 comments Shorter solution:

def is_leap(year): leap = False

`# Write your logic here if year % 4 == 0 and year % 100 > 0 or year % 100 ==0 and year % 400 == 0: leap = True else: leap return leap`

year = int(input()) print(is_leap(year))

`return leap`

year = int(input()) print(is_leap(year))

+ 0 comments # write code in simple manner

def is_leap(year): leap = False if year % 400 == 0: leap = True elif year % 100 == 0: leap = False elif year % 4 == 0: leap = True

`return leap`

year = int(input()) print(is_leap(year))

Load more conversations

Sort 1971 Discussions, By:

Please Login in order to post a comment