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.

How does that contradict what that guy said? You wrote different code than what he wrote. You're exploiting "truthy/falsey" values; the other guy's just using regular booleans

I thought the constraints meant that you could write your code assuming the constraints to be true - that no test cases would include inputs that don't satisfy the constraints.

Reversing the order almost works, but you have to add an else block returning False at the end to capture cases where none of the if blocks execute (like for the year 1992) and also change all elif statements to if statements. I.e:

Why wouldn't 1992 return true if % 4 == 0 is the first statement? it divides cleanly by 4 with no remainder, yet it gives false in the original scenario.

here, the return statement returns in boolean. where the given conditions in the problem are put.a year is a leap year if it is divisible by 4 and 400 but not divisible by 100.

Just like there is order of operations for mathematical operators, there is precedence for Python operators. AND has higher precedence than OR, so it is evaluated first.

if** year % 4 == 0 and (year % 400 == 0 or year % 100 != 0)** is true the how the true boolean is affected to leap variable since we didn't you didn't make leap=something?
I hope you get the idea

learners need to code like this imo. I use some of these programs with my classes and they simply dont understand any inline return equation, hell some of them dont even return values properly

I agree, the multiple return statements are a tad heavy and go against DRY coding practices. However, your recommended solution also sacrifices readability, which IMO is more important than trying to massage your code into a single statement.

Be succinct but be readable. Let your bundlers handle the optimizations.

Thanks for your detail explanation for this problem.
Could you please explaine to me the relationship of your answer and the problem context, please? Because I didn't understand well the answer.

I want to unerstand how did you formulated these steps in this form you had programmed

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

1. The year can be evenly divided by 4;
2. If the year can be evenly divided by 100, it is NOT a leap year, unless;
3. The year is also evenly divisible by 400. Then it is a leap year

Your solution is certainly the most elegant, but when the user is first presented with the problem in the coding box, the return command has already been given the object 'year' and we are given a #Write your logic here note above it. So it's at least forgivable for some of us to construct multiple 'if' blocks to solve it, wouldn't you agree?

Hi just a question about parenthesis placement
why does placing a parenthesis like below
year % 4 == 0 and (year % 400 == 0 or year % 100 != 0) is correct code versus not placing parenthesis at all or writting it like ((y%4==0)and(y%100!=0)or(y%400==0)) results in error

Thanks for this. I've been learning for 4 days now and I really want to be a good coder that abides by the zen of python. Because of your advice I've turned the default code:

That's crazy simple. Wow. I did do if elif and elif.
if year%4 == 0:
leap = True
elif year % 100 == 0:
leap = False
elif year % 400 == 0:
leap = True

for some reason it was wrong for 2100 the test case too. That makes so much sense. and makes it so that both have to be true. Truly a masterful stroke.

Your code is certainly shorter, but you also make a slight sacrifice in readability. Of course, the 'readability' of your code will depend on the experience level of the reader. But you won't always be working with talented programmers. For many, I think the following code is more readily understandable and its decrease in computational efficiency is marginal:

everytime I think I have an understanding of a nice logical approach to a problem simple or not, I seem to always be blown away by a simpler methodology.
I am completely new to python so I know these lightbulb moments are going to keep happening. Thanks for the simplification.
ps. I went straight for the if and else statements..

## Write a function

You are viewing a single comment's thread. Return to all 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:

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:

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:

Don't be redundant, be DRY.

How does the "return year%4==0..." sentence know what you are returning is a boolean? Is that defined by "def is_leap"? Thank you

because all expressions used in that return statement evaluate to booleans

thanx

thanks brother

sir,can you give a clear description about return statements

That exactly is not the case. Try to run following code in interpreter:

def is_leap(year):

Invoke it using

is_leap(1464)

It returns 64 instead of True. Thats because the expression (year%100) returns 64

to mitigate that run following code and that returns either True or False:

def is_leap(year): return (not(year%4) and (bool(year%100) or not(year%400)))

How does that contradict what that guy said? You wrote different code than what he wrote. You're exploiting "truthy/falsey" values; the other guy's just using regular booleans

Check out python truthiness

some of us do that for clarity sake for those who may have a hard time processing what is going on.

This is what i did.

Thanks for explaining

the first if statement if year%4==0: return True this will give true for 1900 year also but that it is not true

you have to give the constraint like

if year in range(1900,pow(10,5)+1): because they clearly mentioned this constraintr

I thought the constraints meant that you could write your code assuming the constraints to be true - that no test cases would include inputs that don't satisfy the constraints.

that is true

You can just reverse the order and it will be fine.

Reversing the order almost works, but you have to add an

`else`

block returning`False`

at the end to capture cases where none of the`if`

blocks execute (like for the year 1992) and also change all`elif`

statements to`if`

statements. I.e:This passes all the test cases.

i will mark your words

not passes year %100 == 0 test

Why wouldn't 1992 return true if % 4 == 0 is the first statement? it divides cleanly by 4 with no remainder, yet it gives false in the original scenario.

true

why we have to change elif statements to if statements sir??

i did the same thing but it's not working for year 2100 2100 is a leap year,but according to the compiler we should get False.

I reversed the order and it passed all the tests. But i dont know why it did. what did the reversing the order do??

Took a few tries before I understood the conditions but I got it!

Here's my solution:

That wound up failing two test cases.

how can you return heap without any function

This quote

isthe function. The post doesn't include the declaration because it's not relevant.fail in two cases

What about trying something like this:

this works for all test cases

It fails for the year 2100

It is giving runtime error

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

make the last loop as elif year%100!=0: return True

this will work for all the cases

Good Work ！ I think I ignored the "unliess" in the problem sheet. I learned the if and elif, but never used them. This is an interesting question.

I did something pretty much the same but it failed one.

This is what I did.

year = int(input())

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

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

why it is giving error on input 2100

I have the Same Error..what to do?

ran your code, 3 out of 6 test cases failed :(

ohh ... halo

where is else block

You can write it without parentheses like this:

Adding parenthesis increases readability. I might be wrong though!

what is the logic of this.....man!!!!

here, the return statement returns in boolean. where the given conditions in the problem are put.a year is a leap year if it is divisible by 4 and 400 but not divisible by 100.

You may find the

logichere.@karthik_bharadh1

Thanks for this , this page cleared everything about Leap years !

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

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

why it is giving error on input 2100

But then how does python know which one you mean: 1.y%4 AND y%100, or y%400

2.y%4, and y%100 OR y%400?

That's like saying these sentences state the same thing: 1.I will eat pie and roaches, or I will eat pasta.

2.I will eat pie, and either I will eat roaches or pasta.

Am I missing something?

Just like there is order of operations for mathematical operators, there is precedence for Python operators. AND has higher precedence than OR, so it is evaluated first.

Order of precedence: NOT > AND > OR

Thanks for this. I was having issues with If/ else statements.

plss explain this a bit clear how return works their

here solving the given condition, return turns true

can you please explain the code.

I think this is a good read, to understand the logic of a leap year. The code is straight forward.

Beautiful I just add parenthesis for me like that (year % 4 == 0) and (year % 100 != 0) or (year % 400 == 0)

good one man.you seem to have pretty good understanding of python PEP.

why parenthesis are required and we get error without them??

Associativity.

'a and b or c' reads like '(a and b) or c' to the interpreter.

'a and b' is only true every 400 years.

c is true every year except those divisible by 100, so you're marking only 3 years out of every 400 as non-leap years.

if** year % 4 == 0 and (year % 400 == 0 or year % 100 != 0)** is true the how the true boolean is affected to leap variable since we didn't you didn't make leap=something? I hope you get the idea

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

this is nice. it avoids repetition. Thanks alot.

learners need to code like this imo. I use some of these programs with my classes and they simply dont understand any inline return equation, hell some of them dont even return values properly

I agree, the multiple return statements are a tad heavy and go against DRY coding practices. However, your recommended solution also sacrifices readability, which IMO is more important than trying to massage your code into a single statement.

Be succinct but be readable. Let your bundlers handle the optimizations.

Your answer isn't really very clean code though. So while it works, it isn't very readable, which IMO is incredibly important.

IMO means..?

IMO = "in my opinion"

also there's

IMHO = "in my humble opinion" (which is often not very humble at all)

thank u

Thanks that explained it so clearly

Makes perfect sense, thank you very much for your explanation!

can you explain why you use the 'or' operator instead of 'and' for the 'year%100 != 0'? i still don't get it. thank you

I like your func very much

That's way shorter than my if and else loop's. Thanks.

Hi @kpagcha,

Thanks for your detail explanation for this problem. Could you please explaine to me the relationship of your answer and the problem context, please? Because I didn't understand well the answer.

I want to unerstand how did you formulated these steps in this form you had programmed

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

1. The year can be evenly divided by 4; 2. If the year can be evenly divided by 100, it is NOT a leap year, unless; 3. The year is also evenly divisible by 400. Then it is a leap yearThanks for advance. Fouzi

very effective buddy.... thnk u

if you want to reduce redundancy, why not also return the bool of the comparisons to avoid explicitly checking for zero equivalency..

irregardless.. you deserve an upvote

Your solution is certainly the most elegant, but when the user is first presented with the problem in the coding box, the return command has already been given the object 'year' and we are given a #Write your logic here note

aboveit. So it's at least forgivable for some of us to construct multiple 'if' blocks to solve it, wouldn't you agree?it is not passing all the test cases?

thanks :D

Might be clearer:

Hi just a question about parenthesis placement why does placing a parenthesis like below year % 4 == 0 and (year % 400 == 0 or year % 100 != 0) is correct code versus not placing parenthesis at all or writting it like ((y%4==0)and(y%100!=0)or(y%400==0)) results in error

thanks for advice, keep commenting :)

Hi, thanks for the shorter approach but, what about the condition-- 1900<= year <=10**5

Thank you sir for the advice.

Thanks for this. I've been learning for 4 days now and I really want to be a good coder that abides by the zen of python. Because of your advice I've turned the default code:

into this DRY code:

I'm sure there are ways to improve this further.

That's crazy simple. Wow. I did do if elif and elif. if year%4 == 0: leap = True elif year % 100 == 0: leap = False elif year % 400 == 0: leap = True

agree with you on "dry" . succintness is power and a bloated code is such a waste of python zen.

moreover i dislike it that the code is partially provided: it shoeboxes one into a specific pattern (priming).

the instructions werebloatedtootoomucuunnecessaryinformationpresentedunclearlyinordertoappearsiphisticatedlikethislongword.

Well, the problem is set up with variable leap = False and then return leap at the end, which surely guides the beginners to the if-else method.

how it will come can u explain

its a mistake he may copied and pasted the code..

but for 1900,its divisible by 4 but also by 100..so how is that code possible

Wow, amazing comment, thank you!

Thanks for the lesson. For other newbies here is a good article worth reading that will clarify the syntax: https://www.codesdope.com/python-boolean/

failed 2 cases

Your code is certainly shorter, but you also make a slight sacrifice in readability. Of course, the 'readability' of your code will depend on the experience level of the reader. But you won't always be working with talented programmers. For many, I think the following code is more readily understandable and its decrease in computational efficiency is marginal:

Thanks for the advice. I am new to python and this something that i would have never thought of.

Thank you for the tip. The logic is so clear when I lok at your code. For whatever reason it isn't straightfoward reading from the problem statement.

everytime I think I have an understanding of a nice logical approach to a problem simple or not, I seem to always be blown away by a simpler methodology. I am completely new to python so I know these lightbulb moments are going to keep happening. Thanks for the simplification. ps. I went straight for the if and else statements..

why to add OR for % 100 condition? Not able to understand the logic. Please help!!

Thank you, this helped

All my respectz.

Thank you for the huge help, I kept trying with if's and got nowhere.

Why:

... that's not true

Wow. I did it in a much length way with lots of ifs and elses.

Thank you for sharing this.

Indeed the way to go, here are some print statement might help understand the logic:

Thanks for your input! I'm new to programming so this is very helpful.

ok thats good...

This is a much better approach. Thanks

man can somebody tell me the right answer so i can copy it?