Program to determine leap year from the Christian era [Python]

In this article A program to determine if it is a leap year from the Christian era I will write.

What is a leap year?

A leap year (leap year, intercalary year) is a leap year. On the other hand, a year that is not a leap year is called a common year (English: common year). leap year-Wikipedia

How to find

There are three conditions to determine if it is a leap year.

  1. A year in which the year is divisible by 4 is a leap year (in principle)
  2. However, the year in which the year is divisible by 100 is (in principle) a normal year, not a leap year.
  3. However, any year in which the year is divisible by 400 is a leap year.

I will write the program under this condition.

Write code

** Conditional branching in the function ** Pass n as an argument to a function called “is_leap_year” which means leap year. n is the Christian era.

The result is returned as a leap year for True and a non-leap year for False.

leap_year.py


def is_leap_year(n):
    if n % 4 != 0:
        return False
    if n % 400 == 0:
        return True
    if n % 100 == 0:
        return False
    return True

** Get data and display results ** Print the result with print ().

If True “The year XX is a leap year.” If False, "The year XX is not a leap year." Is displayed.

leap_year.py


year = int(input("Please enter the year in the Christian era →"))
if is_leap_year(year):
    print(year, "The year is a leap year.", sep="")
else:
    print(year, "The year is not a leap year.", sep="")

Overall picture

leap_year.py


def is_leap_year(n):
    if n % 4 != 0:
        return False
    if n % 400 == 0:
        return True
    if n % 100 == 0:
        return False
    return True

year = int(input("Please enter the year in the Christian era →"))
if is_leap_year(year):
    print(year, "The year is a leap year.", sep="")
else:
    print(year, "The year is not a leap year.", sep="")

Execution result

Enter the year in the Christian era → 1859
→ 1859 is not a leap year.

Enter the year in the Christian era → 2000
→ 2000 is a leap year.

Summary

This time, Program to determine if it is a leap year I wrote.

I think it's very easy to do Please try it.

Thank you very much.

Recommended Posts

Program to determine leap year from the Christian era [Python]
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
[Python] Determine leap years
[Python] How to remove duplicate values from the list
Python practice Convert the input year to the Japanese calendar
The wall of changing the Django service from Python 2.7 to Python 3
Learn Nim with Python (from the beginning of the year).
Output the time from the time the program was started in python
I wanted to use the Python library from MATLAB
From buying a computer to running a program with python
The first step to getting Blender available from Python
Specifies the function to execute when the python program ends
Post from Python to Slack
Cheating from PHP to Python
How to run a Python program from within a shell script
Write a python program to find the editing distance [python] [Levenshtein distance]
Anaconda updated from 4.2.0 to 4.3.0 (python3.5 updated to python3.6)
Switch from python2.7 to python3.6 (centos7)
Pass OpenCV data from the original C ++ library to Python
How to start the program
Extract the value closest to a value from a Python list element
Determine the date and time format in Python and convert to Unixtime
How to determine the existence of a selenium element in Python
How to get followers and followers from python using the Mastodon API
Simple code to call a python program from Javascript on EC2
Read a file in Python with a relative path from the program
[Python + heroku] From the state without Python to displaying something on heroku (Part 1)
Terminal association from the server side to Amazon SNS (python + boto3)
From the introduction of JUMAN ++ to morphological analysis of Japanese with Python
[Python + heroku] From the state without Python to displaying something on heroku (Part 2)
Change the active version in Pyenv from anaconda to plain Python
[Python] A program that rotates the contents of the list to the left
Call Matlab from Python to optimize
Leave the troublesome processing to Python
Create folders from '01' to '12' with python
Post from python to facebook timeline
[Lambda] [Python] Post to Twitter from Lambda!
Existence from the viewpoint of Python
In the python command python points to python3.8
How to get the Python version
[Python] How to import the library
Connect to utf8mb4 database from python
Python (from first time to execution)
Post images from Python to Tumblr
Use the Flickr API from Python
How to access wikipedia from python
Python to switch from another language
[Python] Change the alphabet to numbers
Did not change from Python 2 to 3
Update Python on Mac from 2 to 3
[Python] I tried to reproduce the emergency escape program to return from the world to return from the modified world of "The disappearance of Haruhi Suzumiya"
What skills do I need to program with the FBX SDK Python?
Tips for Python beginners to use the Scikit-image example for themselves 9 Use from C
[Python] A program that calculates the number of socks to be paired
Wikipedia goes from the era of writing to the era of creation ~ Automatic generation from Twitter
Python code to determine the monthly signal of a relative strength investment
I made a program to check the size of a file in Python
[python] Send the image captured from the webcam to the server and save it
[Python] Fluid simulation: From linear to non-linear
From Python to using MeCab (and CaboCha)