Want to check the process of whether the given year is a leap year or not using the modulo operator in python? This is the best tutorial for all python beginners and developers. Here, we have explained using modulo to check for leap year in python along with other functions. Just take a look at the covered modulo (%) operator in previous tutorials and then follow the process that has been given here in this tutorial of Using Modulo Check Leap Year Python.
This Tutorial Leap Year Program in Python using Modulo covers the following stuff:
- Python Modulo Operator
- How to determine whether a year is leap or not in programming?
- An Example of leap year with modulo operator in Python
- Getting leap year with modulo in another way
- Python Program to Check Leap Year
- Python program to check if Year is a leap year or not
- Pseudo-code for Program to check if a given year is Leap year
- Examples of Leap Year Program in Python
Python Modulo Operator
The modulo is an operator that is utilized to determine the remainder of one number divided by another. The symbol used to get the modulo is the percentage mark i.e. ‘%’. Hence, in case you have an equation such as 9 % 3, the answer would be 0, because 3 fits into 9 evenly, and there is no remainder. The key to understanding the modulo is that it isn’t used to calculate the answer to the division of two numbers, but to calculate the remainder. So in the example of 10 % 3, the answer would be 2, because 3 goes into 10 3 times, with a remainder of 2.
An excellent way to utilize this modulo operator is to calculate whether or not a given year is a leap year. As you apparently know, leap years happen every 4 years, and it’s always on years that have multiples of 4, for example, 2000, 2004, 2008, 2012, and 2016 were some of the most recent leap years. Even though leap years are always even years, not every even year is a leap year, because it occurs every 4 years instead of every 2. For this reason, the code to check whether a number is even or odd won’t work for this particular purpose.
- How to Use Python to Convert Fahrenheit to Celsius | Python Program to Convert Fahrenheit to Celsius & Vice Versa
- Quick Tip: Using Python’s Comparison Operators | Chaining Comparison Operators in Python
- Python Programming – Creation, Initialization and Accessing The Elements In A Dictionary
In Python, the modulo ‘%’ operator operates as regards:
- Firstly, the numbers are converted into the common type.
- It is a ZeroDivisionError exception, in case the right argument is zero.
- The arguments may be floating-point numbers.
- The result of using the ‘%’ operator always generates the same sign as its second operand or zero.
- Moreover, the absolute value for the result is rigorously smaller than the value of the second operand.
Also Read: Using Python Display Calendars
How to determine whether a year is leap or not in programming?
To check if a number is a leap year, we have to see if it is divisible by 4. To achieve this, we apply the modulo operator & see if a number divided by 4 has a remainder of 0. If it does, then it must be a leap year. If it doesn’t, then it can’t be one. Check out the code below to see how it works:
year = 2020 if (year % 4) == 0: print("{0} is a leap year") else: print("{0} is not a leap year")
This simple code should work with any year you input to determine whether or not it’s a leap year. If you try it with 2017, 2018, or 2019, you’ll see that neither of those years are leap years. However, if you try it with 2020 like in the example above, you’ll find that it is, in fact, a leap year. So the output of the above code is:
2020 is a leap year
Also, check out the following video tutorial to examine whether the given year is a leap year or not using python programming:
An Example of leap year with modulo operator in Python
#Example of modulo usage year_input = int(input("Enter a year: ")) if year_input % 4 == 0: print("Remainder is 0 so ", year_input, "is a leap year!") else: print(year_input, "Not a leap year!")
Output:
Getting leap year with modulo in another way
In the following instance, we have utilized the filter function and modulus operator. A list of years is given and filter function will use the % operator and leap years will filter out as shown below:
#Example of modulo usage with filter/lambda Year_List = [1984, 1990, 1996, 2002, 2008, 2014, 2018] L_Years = list(filter(lambda leap_yrs: (leap_yrs%4 == 0) , Year_List)) print("Following are leap years in the list: " ,L_Years)
Result:
Following are leap years in the list: [1984, 1996, 2008]
Python Program to Check Leap Year
A leap year is accurately divisible by 4 but for century years (years ending with 00). The century year is a leap year particularly if it is absolutely divisible by 400. For instance,
2017 is not a leap year 1900 is a not leap year 2012 is a leap year 2000 is a leap year
Python program to check if Year is a leap year or not
# Python program to check if year is a leap year or not year = 2012 # To get year (integer input) from the user # 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))
Output:
2012 is a leap year
Pseudo-code for Program to check if a given year is Leap year
The following is pseudo-code:
if year is divisible by 400 then is_leap_year else if year is divisible by 100 then not_leap_year else if year is divisible by 4 then is_leap_year else not_leap_year
Examples of Leap Year Program in Python
- Example #1 – Using Conditional Statement(if Statement)
- Example #2 – Using Elif
- Example #3 – Using Nested If
Python Program to Check Leap Year using If Statement
Code:
input_year = int(input("Enter the Year to be checked: ")) if (( input_year%400 == 0)or (( input_year%4 == 0 ) and ( input_year%100 != 0))): print("%d is Leap Year" %input_year) else: print("%d is Not the Leap Year" %input_year)
Output:
Please Enter the Year Number you wish: 1200 1200 is a Leap Year >>> Please Enter the Year Number you wish: 1700 1700 is Not the Leap Year
Python Program to Check Leap Year using Elif Statement
Code:
# Python Program to Check Leap Year using Elif Statement year = int(input("Please Enter the Year Number you wish: ")) if (year%400 == 0): print("%d is a Leap Year" %year) elif (year%100 == 0): print("%d is Not the Leap Year" %year) elif (year%4 == 0): print("%d is a Leap Year" %year) else: print("%d is Not the Leap Year" %year)
Result:
Please Enter the Year Number you wish: 2022 2022 is Not the Leap Year
Python Program to Check Leap Year using Nested If Statement
Code:
input_year = int(input("Enter the Year to be checked: ")) if(input_year%4 == 0): if(input_year%100 == 0): if(input_year%400 == 0): print("%d is Leap Year" %input_year) else: print("%d is Not the Leap Year" %input_year) else: print("%d is Leap Year" %input_year) else: print("%d is Not the Leap Year" %input_year)
Output:
Enter the Year to be checked: 2016 2016 is Leap Year