Reliving the past? Predicting the future? TIME TRAVEL!!!
Hello my lovely readers! For June 2021, I'm going to reveal my second project related to leap years 😁 Last month, I made a leap year CHECKER that let's you know whether the input was a leap year or not. This time, I made a leap year GENERATOR 💪 A lot of the codes here have taken heavy influence from the May 2021 post, BUT they have different purposes. (If you either haven't read the May 2021 post, want to re-read it, or need a reminder as to what a leap year is, click here for the post.)
All right... How did you make a leap year generator?
It was actually a pretty simple code! A lot of the code is similar to my May 2021 code, EXCEPT that I've swapped out the "while" loop for a "for" loop.
Leap Year Generator - All the Years: Please download the PDF from the link or by looking at the image posted here.
The for loop is used to repeat sequences from a set of numbers, letters, words etc. When using a for loop from a range of numbers, we have to use the range() function so that code will know what sequences to repeat. In this code, range(2022) was used to specify that we want to see all the leap years from Year 0 until Year 2021.
Wait... Year 2021??? Why not Year 2022, if range(2022)?
Yes, 2021 is correct. That's because range(2022) means "up to 2022." Range(2022) would then count numbers like 0, 1, 2, ..... , 2019, 2020, 2021. Likewise range(2021) would mean 0, 1, 2, ... , 2019, 2020.
(I would normally put an image here, but the list was too long to put in one picture 😂😂😂 It spans 11 pages on the PDF 😅)
It's great that we can now see all the leap years that ever existed since we started using the AD calendar...
But what if I just want to know the leap years from... say... the year 1990 until 2021? I don't want to have to read through 11 pages just to find that out?!
Don't worry, we won't have to read through 11 pages to find out leap years within a more specific range. We can easily modify the code to set the years to range from 1990 to 2021 instead. In fact, I'll show you the code that can do just that.
Leap Year Generator - 1990 to 2021: Please download the PDF from the link or by looking at the image posted here.
You'll notice that instead of range(2022), there's now range(1990, 2022) instead. Adding the 1990 sets the range from "0 to 2022" to "1990 to 2022." See? Simple!
Here's a demonstration of the code which shows all the leap years from 1990 to 2021.
Leap Years 1990 to 2021: Please download the PDF from the link or by looking at the image posted here.
According to the leap year generator, the years 1992, 1996, 2000, 2004, 2008, 2012, 2016, and 2020 are all the leap years from 1990 to 2021. No need to go through a file with many pages to find them. Just modify the code a bit to change the range, and we'll find the leap years we want.
Ooooooo Can we change the code to find leap years in the future then?
Of course. Likewise, just change the ranges and we'll see the leap years in the future too. (Unless the calendars and criteria for leap years change sometime in the future, then at that point we'll probably have to change the equations as well...)
As an example, I made a code that will generate the leap years from 2021 to 2050.
Leap Year Generator - 2021 to 2050: Please download the PDF from the link or by looking at the image posted here.
I just changed the range() from range(1990, 2022) to range(2021, 2051). Remember that 2051 means "up to 2051."
Here's a demonstration of the code so we'll see what the leap years will be in the near future!
Leap Years 2021 to 2050: Please download the PDF from the link or by looking at the image posted here.
Despite the purpose of the Leap Year Generator summoning a lot of numbers at once, it's actually a pretty simple code to make! I hope you make your own versions and have fun figuring out leap years 😇
Final messages
As always, thank you for reading my blog! I have to admit that while loops are more versatile, but for loops are really useful if you need to pull items from a set range. The for loops are also widely used in data science as well, so if you're interested in pursuing computational skills, I highly recommend learning for loops. Please feel free to leave a comment or send me a message. Contact information can be found on the Contact Me page.
I'm thinking of doing a story time for the next post. Don't exactly know what about, but I'm 90% sure I'm posting a story. Please check out the archives section for my past posts until then 💗
No comments:
Post a Comment