- Practice
- Python
- Introduction
- Write a function
- Discussions

# Write a function

# Write a function

kpagcha + 0 comments For the learners: you should know that doing something like the setup for this challenge inclines you to do is a bad practice. Never do the following:

def f(): if condition: return True else: return False

This is just dumb. You are returning a boolean, so why even use if blocks in the first place? The correct what of doing this would be:

def f(): return condition

Because this already evaluates as a boolean.

So in this challenge, forget about ifs and elses, and that leap variable, and just do the following:

def is_leap(year): return year % 4 == 0 and (year % 400 == 0 or year % 100 != 0)

Don't be redundant, be DRY.

am110 + 0 comments The calendar builtin module has a function isleap() that returns True if the year is leap, otherwise it returns False

import calendar def is_leap(year): return calendar.isleap(year)

flmmartins + 0 comments The problem's explanation is not clear. I am not an english speaker and the bullet points were not clear enough for me.

Can that be improved?

Suggested explanation is bellow:

In the Gregorian calendar three criteria must be taken into account to identify leap years

`The year can be evenly divided by 4; If the year can be evenly divided by 100, then it is not a leap year The year is also evenly divisible by 400 then it is a leap year.`

JonathanV + 0 comments Working

if year%4 == 0 and (year%100 != 0 or year%400 == 0): leap = True

nayanbhiwapurkar + 0 comments Method 1

def is_leap(year): return year % 4 == 0 and (year % 400 == 0 or year % 100 != 0)

Method 2

def is_leap(y): return (y%400==0) or (y%100!=0 and y%4==0)

Method 3

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

Sort 1040 Discussions, By:

Please Login in order to post a comment