In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
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.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.