Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to judge a leap year in Python

Shulou Source: shulou.com Published: 2022-06-02 08:00:20 10月01日 Update

This article is to share with you about how to judge leap years in Python. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article.

Train of thought analysis

1. Ordinary leap year: the Gregorian calendar year is a multiple of 4, usually a leap year. (for example, 2004 is a leap year)

2. Century leap year: the Gregorian calendar year is a full hundred and must be a multiple of 400 to be a leap year (for example, 1900 is not a century leap year, 2000 is a century leap year).

Code implementation

# the whole century divisible by 400 is the leap year

# the non-whole century divisible by 4 is a leap year

Year = int (input ("enter a year:")

If (year% 4) = = 0:

If (year% 100) = = 0:

If (year% 400) = = 0:

Print ("{0} is a leap year" .format (year))

Else:

Print ("{0} is not a leap year" .format (year))

Else:

Print ("{0} is a leap year" .format (year))

Else:

Print ("{0} is not a leap year" .format (year))

The result of running the code:

Enter a year: 2004

2004 is a leap year

Program analysis

1. The first line of the program first prompts the user to enter the year to be judged through input, assigns a value to year, and then determines whether the input year is a leap year through the rules.

2. If (year% 4) = = 0 to determine whether the remainder of year divided by 4 is equal to 0, which means there is no remainder and can be divisible by 4, and then continue to judge, otherwise the output will not be a leap year.

3. After being divisible by 4, if (year% 100) = = 0 is used to judge whether the remainder of year divided by 100is equal to 0, which means there is no remainder and can be divisible by 100. otherwise, if it is divisible by 4, but not divisible by 100, the output is a leap year.

4. After divisible by 4 and 100, the output is not a leap year if if (year) = = 0 is used to judge whether the remainder of year divided by 400is equal to 0, which means there is no remainder and can be divisible by 400. otherwise, the output is not a leap year.

The above is how to judge leap years in Python. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Tags: Leap year remainder year input output Century Program Code multiple Gregorian Calendar that is more knowledge articles Analysis practical ordinary one Line work meeting ideas Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn OPPO Reno MySQL Shulou Tech Info NVidia