Leap year Python is the Python code through which it can check whether the year input by the user is a leap year or not. A leap year is a year that comes in every year. The year is recognised by having 366 days instead of 365 days. The one extra day is added in the month of February, making it 29 days long.
A leap year can be checked by dividing the year by 4. If the year is completely divisible, then it is a leap year. A century year on the other hand should be exactly divisible by 400 to be a leap year.
To check if any century year is a leap year, then instead of checking its divisibility by 4, first the number is divided by 100. Then, the quotient obtained is again divided by 4. If the remainder thus obtained is 0, then the century year will be considered a leap year.
Source Code
The source code for leap year Python will use the modulus or remainder operator that is represented by ‘%’. When divided by 4, if the remainder provided is zero, then it means that the year is a leap year.
Along with the operator, if…else condition will be used to check if the condition mentioned above is true or false.
year = int (input (“Enter any year that is to be checked for leap year: “))
if (year % 4) == 0:
if (year % 100) == 0:
if (year % 400) == 0:
print (“The given year is a leap year”)
else:
print (“It is not a leap year”)
else:
print (“It is not a leap year”)
else:
print (“It is not a leap year”)
In the source code, the program first checks whether the given year is divisible by 4. If the condition is true, it will then check if the year is divisible by 100. Again, if true, it will then check the divisibility of the year with 400. If these conditions are satisfied, then the program will provide the output that displays that the given year is a leap year.
Note that here the equal to operator, that is, == is used and not the assigning operator. The assigning operator, represented by = assigns a value to a variable and is different from the mathematical = sign.
Frequently Asked Questions
How do you do a leap year in Python?
A leap year Python is found by checking the divisibility of the year with 4 and 400. If a year is perfectly divisible by 4, then it is a leap year. However, if it is a century year (ending with 00), then it will be checked with 400.
Why is 2020 not a leap year?
2020 is a leap year. It had 366 days with a February of 29 days. Also, the year 2020 is perfectly divisible by 4. It is, thus, a leap year.
Is Python a leap?
Python does not have an in-built function to check whether a year is a leap or not. One will need to use the modulus operator to test a year for a leap.
Is 2032 a leap year?
When dividing the number 2038 by 4, we obtain 508 as quotient and 0 as remainder. Thus, it is perfectly divisible by 4, making it a leap year.
Leave a Reply