{"id":37,"date":"2022-04-08T23:30:50","date_gmt":"2022-04-08T23:30:50","guid":{"rendered":"https:\/\/codecrypt76.com\/?p=37"},"modified":"2022-11-25T16:49:30","modified_gmt":"2022-11-25T16:49:30","slug":"calculate-leap-year-using-python","status":"publish","type":"post","link":"https:\/\/codecrypt76.com\/index.php\/2022\/04\/08\/calculate-leap-year-using-python\/","title":{"rendered":"Calculate Leap Year Using Python"},"content":{"rendered":"\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped\">\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1200\" height=\"630\" data-id=\"39\"  src=\"http:\/\/codecrypt76.com\/wp-content\/uploads\/2022\/04\/leap-year-366-days.png\" alt=\"Leap Year Algorithm\" class=\"wp-image-39\" srcset=\"https:\/\/codecrypt76.com\/wp-content\/uploads\/2022\/04\/leap-year-366-days.png 1200w, https:\/\/codecrypt76.com\/wp-content\/uploads\/2022\/04\/leap-year-366-days-300x158.png 300w, https:\/\/codecrypt76.com\/wp-content\/uploads\/2022\/04\/leap-year-366-days-1024x538.png 1024w, https:\/\/codecrypt76.com\/wp-content\/uploads\/2022\/04\/leap-year-366-days-768x403.png 768w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><figcaption>Calculate Leap Year Using Python<\/figcaption><\/figure>\n<\/figure>\n\n\n\n<h2>Introduction<\/h2>\n\n\n\n<p>The purpose of this article is to show how to calculate Leap Year using Python. Before delving into the code let\u2019s discuss some background information on Leap Year what and why we have Leap Year.<\/p>\n\n\n\n<h2>What is a Leap?<\/h2>\n\n\n\n<p>A&nbsp;<strong>leap year<\/strong>s a&nbsp;calendar year&nbsp;that contains an additional day added to keep the calendar year synchronized with the&nbsp;astronomical year&nbsp;or&nbsp;seasonal year. This is because it takes the Earth approximately 365.25 days to orbit the Sun \u2014 a solar year. The calendar year is usually rounded to 365. Therefore, to make up for the missing partial day, one day is added to our calendar approximately every four years (February has 29 days instead of 28). This at is a leap year.<\/p>\n\n\n\n<h2>The Algorithm<\/h2>\n\n\n\n<p>The following pseudocode is used to determine is a year is a Leap year or a common year.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p><strong>if<\/strong>&nbsp;(<em>year<\/em>&nbsp;is not&nbsp;divisible&nbsp;by 4)&nbsp;<strong>then<\/strong>&nbsp;(it is a common year)<br><strong>else if<\/strong>&nbsp;(<em>year<\/em>&nbsp;is not divisible by 100)&nbsp;<strong>then<\/strong>&nbsp;(it is a leap year)<br><strong>else if<\/strong>&nbsp;(<em>year<\/em>&nbsp;is not divisible by 400)&nbsp;<strong>then<\/strong>&nbsp;(it is a common year)<br><strong>else<\/strong>&nbsp;(it is a leap year)<\/p><cite>source: <a href=\"https:\/\/en.wikipedia.org\/wiki\/Leap_year#Algorithm\" target=\"_blank\" rel=\"noreferrer noopener\">Wikipedia<\/a><\/cite><\/blockquote>\n\n\n\n<p>The approach I took to simplify this was:<\/p>\n\n\n\n<p><strong>If <\/strong>(year&nbsp;is divisible&nbsp;by 4) <strong>and <\/strong>(<em>year<\/em>&nbsp;is not divisible by 100)&nbsp; <strong>or <\/strong>(<em>year<\/em>&nbsp;is divisible by 400)&nbsp; <strong>then <\/strong>(it is a common <em>year<\/em>)<br><strong>else <\/strong>(it is a common year).<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>def is_leap(year: str) -&gt; bool:\n    &quot;&quot;&quot;Returns True if year is a Leap Year else False&quot;&quot;&quot;\n    CASE1: bool = year % 4 == 0\n    CASE2: bool = year % 100 == 0\n    CASE3: bool = year % 400 == 0\n\n    return CASE1 and not CASE2 or CASE3\n\n\n\nif __name__ == &#39;__main__&#39;:\n    is_leap(1955)  # False\n    is_leap(1976)  # True<\/code><\/pre><\/div>\n\n\n\n<p>This code can be simplified in a single line of code.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>def is_leap(year: int) -&gt; bool:\n    &quot;&quot;&quot;Returns True if year is a Leap Year else False&quot;&quot;&quot;\n    return year % 4 == 0 and not year % 100 == 0 or year % 400 == 0\n\n\nif __name__ == &#39;__main__&#39;:\n    is_leap(2013)  # False\n    is_leap(2024)  # True   <\/code><\/pre><\/div>\n\n\n\n<p>Both code snippets will solve the <a href=\"https:\/\/www.hackerrank.com\/challenges\/write-a-function\/problem\" target=\"_blank\" rel=\"noreferrer noopener\">HackerRank Leap Year Code Challenge<\/a>. So this approach works.<\/p>\n\n\n\n<h2>Python Calendar Library<\/h2>\n\n\n\n<p>In Python,&nbsp;<strong><code>calendar.isleap()<\/code><\/strong>&nbsp;is a function provided in calendar module that can be used to determine if a year is a Leap Year or a common year. This is probably the most simplest approach. Leap Year<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>import calendar\n  \n# checking whether given year is leap or not\nprint(calendar.isleap(2001))  # True\nprint(calendar.isleap(2016))  # False<\/code><\/pre><\/div>\n\n\n\n<h2>Conclusion<\/h2>\n\n\n\n<p>Leap years are needed to keep our&nbsp;calendar&nbsp;in alignment with the Earth\u2019s revolutions around the Sun. Hopefully after reading this post the Leap Year Algorithm has been demystified and you now know how to check if a year is a Leap Year or a common year using python.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to calculate Leap Year using Python.<\/p>\n","protected":false},"author":1,"featured_media":39,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[13,4],"tags":[9,10,11,12],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/codecrypt76.com\/index.php\/wp-json\/wp\/v2\/posts\/37"}],"collection":[{"href":"https:\/\/codecrypt76.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codecrypt76.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codecrypt76.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codecrypt76.com\/index.php\/wp-json\/wp\/v2\/comments?post=37"}],"version-history":[{"count":4,"href":"https:\/\/codecrypt76.com\/index.php\/wp-json\/wp\/v2\/posts\/37\/revisions"}],"predecessor-version":[{"id":460,"href":"https:\/\/codecrypt76.com\/index.php\/wp-json\/wp\/v2\/posts\/37\/revisions\/460"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codecrypt76.com\/index.php\/wp-json\/wp\/v2\/media\/39"}],"wp:attachment":[{"href":"https:\/\/codecrypt76.com\/index.php\/wp-json\/wp\/v2\/media?parent=37"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codecrypt76.com\/index.php\/wp-json\/wp\/v2\/categories?post=37"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codecrypt76.com\/index.php\/wp-json\/wp\/v2\/tags?post=37"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}