Numbers are most common entities you will use when interacting with the computer. We represent numbers using numerical text in the editors, but store the numbers in variables.
Python is a dynamically typed language. We do not have declare a type of a variable before assigning any value to it.
Problem and Solution.
Let's start with a simple exercise of finding if a year is a leap year
A normal year has 365 days
A leap year has 366 days.
A Leap year is a year
That is exactly divisible by 4. (2016, 2020, 2024)
** If divisible 100, then it is not. (2100, 2200, 2300)
* If it is divisible by 400, then it is. (2000, 2400)
Refer to Maths is Fun for more information on leap years.
def test_is_leap_year():
> assert is_leap_year(2016) == True
E NameError: name 'is_leap_year' is not defined
numbers/leap_year_1.py:3: NameError
=======================================
Fix tests - 1
The NameError means that we have not defined our method yet. The error message clearly says that is_leap_year is not defined. So, let us define it.
There are ways in which we could short-circuit the conditions. In this book, I want to emphasize that refactor for code-repeatability, but keep the code as you understand it best. If you the conditions reflect what you are looking for the code is complete.
Final code
Here is the final code for the test leap year.
defis_leap_year(year:int) ->bool:""" :param year Given year to test, whether it is a leap year :return: True if leap year, False otherwise """# divisible by 4, Trueif year %4==0:# divisible by 10, Falseif year %100==0:# divisible by 400, Trueif year %400==0:returnTruereturnFalsereturnTruereturnFalsedeftest_is_leap_year():assertis_leap_year(2016)==Trueassertis_leap_year(2020)==Trueassertis_leap_year(2024)==Trueassertis_leap_year(2100)==Falseassertis_leap_year(2200)==Falseassertis_leap_year(2300)==Falseassertis_leap_year(2000)==Trueassertis_leap_year(2400)==Trueassertis_leap_year(2019)==False