– Hey what’s up guys Aaron here from clever programmer again So today I just want to go over this cool little module I found called calendar in Python It’s pretty nifty I actually haven’t seen it before but I stumbled across it I just want to share it with you guys today This might be a shorter video I just thought it was really cool What could it achieve So yeah let’s jump right into it (upbeat music) First let’s go to my rebels. Helping our rebel I see as usual Creating new rebel. I guess the button is up here now It used to be a little red plus sign on the bottom right but they seem to have changed that Good on the python Is this Python 2.0? Have I been using Python 2.7 all this time? Where’s Python 3? Hold up you guys Python. Here we go All right Unacceptable, unacceptable Let’s just call this calendar module So this should be Python 3 If it specifies Python 2.7, then yeah If it doesn’t say, then is Python 2.7 But if it says just Python, then it’s safe to assume that it’s Python 3 because that is the standard nowadays so make sure you click on Python Not Python 2.7 like a dumbo. Like I did, okay? Let’s continue on. Create this rebel in Python 3 And now we can get started So this little module that I found is called calendar Literally just calendar A-R-A-R-A-R. Got it I can never spell that word right but that’s how you import it, okay? Import calendar. And with this, you can do a lot of cool little function calls I actually think this ties into a couple of other modules later on that I will explain But for now let’s just go through this module and then I’ll mention those at the end again then maybe you can guys can go do your homework and tie some things into it Play around a little bit. Learn that way That’s the best way to learn after you watch these highly entertaining videos to a very small subset of people So let’s just start with calling some functions So this is a bunch of little cool functions So let’s start here Let’s go calendar dot week Calendar dot week header. OK He’s going to call this function with two All right So what this will do is it gives us the headers of a week So a week is just like you have a Monday, Tuesday, Wednesday all the way through Sunday, right? Python starts the week on a Monday So we’re just going to assume that a week starts on Monday here. Okay guys? So when I run this what should pop out are the week headers after calendar is downloaded As you can get all this stuff here is just I should know what this is. Just ignore it Whatever. But down here is the output, okay? This is what we wanted The week header’s here been printed out nicely for a week Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday These two here that I put in signified the length of these headers So actually if I put a three in here then we hit run, then suddenly What is going on here? Available OK. This seems to be working just fine Just ignore all this stuff Sometimes computers complain a lot Like somebody who bickers a lot But yeah So you see down here how now each of the week headers actually is of length three instead of length two If you put length one, does that work? Apparently so yeah. You just have the capital letters here But I’m going to use three because I like it being more descriptive It’s just more appealing to me So that’s the first little function that you can do with Python I was gonna add a new line there or 90 New line. Bam Kaboom OK The next one is calendar dot first weekday So this one’s kind of useless but I’m just gonna throw it in there because why not All this is going to do is going to tell us what the first week day is So Monday is zero Tuesday is one. Wednesday’s two This is three, four, five, six So the first weekday is Monday So this is just gonna print out to zero This could actually be useful later on I realized now because there’s some other functions where you need the numbers of the day instead of the actual string to do different things So this might actually be useful for that and then you can just add numbers So if you know first weekday’s Monday, then you can just add one, two, three, four or five to it to get the other days Let’s just print this out see what happens. As you can see zero prints out because Monday is the first weekday Monday has the index of zero So nice. Easy there What’s wrong? Wrong editor, of course Set another new line Empty print statement We’ll just have an empty line there so you can have some nice spacing here so it’s not all clumped together

But yeah. Let’s continue on So now things get really really cool It almost seems like you’re a magician here Like typing in magic So the next function that I want to go over is month So all you do is you literally just call calendar dot month OK. Calendar dot month That’s it. All you have to do is put in a year So let’s put in this year: 2019 And the month. So it is Yes, I’m counting with my fingers But it’s March. The third month So it’s put three here So it’s March right? Yes it’s March If we print this out, this will actually print out the current month So let’s see how this looks Bam, would you look at that Says March 2019 You have the Monday, Tuesday, Wednesday, Thursday, Friday, Saturday and then you have all the days here of the month. Pretty cool right It just spits this out in a nicely formatted string for you automatically. OK I believe there’s actually some other variables Let me find it for you real quick Print calendar. We’re actually I’ll leave that up to you guys to be an exercise You can actually find the python documentation on month And actually go in and try to find out how, what variable There’s another parameter here you can add here because if you notice all these weekday headers are only two letters long but I wanted them to be three letters long You could actually go in there find the correct one it might be like M or something or C Like you would put something like that in but go practice finding it in the documentation From there you can figure out how to read documentation which is a very very useful skill because there’s no way you’re going to memorize everything in any language or any framework You’re always looking at documentation and googling stuff and figure out You’re gonna be constantly forgetting, constantly learning but getting good at knowing how to figure stuff out That’s the skill you want to develop So go look in the documentation Find out how to fix that Fix it for me, okay? Yeah. Don’t make me do everything all right? Because it’s probably just not a good idea So that’s there, figure out how to make all of these three letters long instead of two So that’s the month function Now, another cool thing we can do is actually get this month in matrix form So let’s see what that would look like Calendar. The function I believe is Month calendar. OK Just like that. And then you just stick in the same parameters So yeah the only change here is you literally just change the function Call it a month calendar rather than just month But that should be it there Let’s run this see what happens And as you can see down here this major’s printed out So this might not mean anything to you but if you look closely this is actually a matrix, a two dimensional array I believe of this exact number array here So these four zeros is these four empty spots here This Monday, Tuesday, Wednesday, Thursday at the beginning, it doesn’t have anything in it And then this one, two, three continues here And then the second list in the one dimentional array is actually this And then the third one is actually this See how that works So you can actually instead of just getting the string like how would you get all the data out of the string You can actually just get it automatically in an array Nice and clean like that Just by calling this function month calendar instead of month Very useful if you’re actually to be doing calculations on dates stuff which is something you’re plugging off to do if you ever want to schedule something like schedule your computer to run this thing on like schedule your computer to run this python script to automate this thing, to launch this program, to do this thing, to hack the system, to text your friend, to do something You know I mean you can do whatever the heck you want but you’re going to have to know calendars and times and what not to schedule those tasks on your computer And so that’s where I think something like this would be very useful and come in handy like that So that’s why I’m touching on this now Kind of ties into a new product we have coming out called “How to automate stuff with Python” More on that in the other video but that is that for the month and the month calendar functions Now let’s go on, okay? Another cool one is what if I just want to print out the entire freaking year? Ta-da! You can do that too Well, assuming you get these words I wish there was a better auto complete on this editor here because I can’t type correctly a lot of the time But anyways It’s not going to be a year as I thought it would be but apparently if you look up on the documentation which will tell you what it actually is, you’re actually just gonna call calendar dot calendar OK. Just like that And then we don’t need the month because we’re like trying to get the entire year not just a month So you just put in just the year just like that And then when I hit run, you will see magic happen, okay? Click run Kaboom. Look at that The entire freaking years printed out super prettily You got the year at the top. 2019 You got January. You got February

You got the entire month headers The week headers here Monday through Sunday. You got all the days of the year All of 365 or 364 If it’s a leap year, I don’t know how that works but hey I think that’s the day that I fell off the bike in front of my crush. Yeah February 27th. anyways What else? What else was I saying? Off track here Print. Yeah printing out the whole year So this is very very handy Now you might be wondering, “Okay if we had it we could put out the entire month, we could get the matrix for the for the month, then can we get the matrix for the entire year?” No. I thought the same thing I was like, “OK cool so you can get like a really really big like three dimensional array of all of this crap.” But apparently you can’t for some reason No I don’t know what that was Some weird prop popped up Just ignore that. Somebody was hacking me I think I said Rafi so I think cause he’s trying to take over this tutorial from a remote location over there but anyways you can’t actually do that but what you could do is you could iterate through every month and then get the model multi-dimensional array yourself and then construct something like this for an entire year on your own That’s another little exercise you can do So why not go and try it I haven’t tried it yet but I’m sure if I put five, ten, 15 hours into it, I can figure it out. (chuckles) But I’m sure you guys can do it in five, ten, 15 minutes If you look up the calendar That month calendar one that gives you the array And then just try to iterate, make a for loop, And iterate over every month That would be cool little exercise OK. Moving on What else do we have here? You can also get There’s a cool function here called day of the week but the function actually called No that’s the variable name I gave it because that makes more sense So day of the week. OK That’s what I want my variable to called Remember, self documenting code turns the ladies and gents on. All hot and bothered You know if they’re into that kind of stuff But this function is just going to be called calendar dot weekday And then you just chuck in the year, and then you chuck in the month, and then you also chuck in the day of the month So today is the eighth Today’s eighth, right? Eighth. Okay You pop that in there so month, year, month and day and what we’re going to get here is remember how I set up here when we have the first weekday here, Monday became zero? What is gonna print out is actually an integer that corresponds to the current whatever weekday this is So today is actually a Friday so that’s gonna be Monday is zero So again counting on my fingers. (chuckles) Laugh at me if you like. (sniffs) Monday. So Tuesday will be one. Wednesday. Thursday It will be four. Should be integer four that prints out from here. Okay? Let’s hit run And I did not print it I make this mistake all the time Apparently I never learn. (grunts) Now let’s run it And boom. Four right there Exactly what I thought Four actually means Friday So we can actually put any years Let’s put in freaking year 3000. See what happens Is this still going to be a four? It’s gonna be a five So apparently in year 3000, March 8th, it’s going to be a Saturday I mean we’re all not gonna be there We’re probably in Mars, Pluto and stuff Thank you Elon Musk. (chuckles) You’re awesome But yeah that’s how that function works What else Again probably a useful function for when you actually have a date and then you want to know the weekday to schedule it like, “Oh I want to schedule every Friday” or something or every Wednesday, you could use just to filter out the integer somehow and figure out the dates, you’d have to problem solve and think about it I don’t even know what problem I’m trying to solve with it but if you’re trying to schedule things on a calendar, this seems like it would definitely be useful I’m sure it is because it’s in the library And most things in library are useful Not everything but I mean most things But moving on there’s a few more So there’s also a function that will tell you if a year is a leap year which is pretty cool So let’s go calendar dot is leap and then what is this year? 2019 I don’t think it’s a leap year So we get… Oh I caught myself, you guys I need to print this, “is_leap.” I didn’t screw up. I’m learning just like you guys Learning just like you guys So apparently 2019 is not a leap year because it comes out false but I think 2020 is So let’s run that Hey what do you know. Yeah So every four years, there’s a leap year If you guys didn’t know that Which just means there’s one less day in February for some weird cosmic reason Like the astral gods got pissed off and decided to troll us

or something. I don’t know But I mean every four years, there’s a leap here So this can tell you, if a certain year is a leap year which is important for calculating days because those will add up Every four years, you’re off by a day You don’t want to be off by a day, okay? You don’t even want to be off by a second When you’re coding, you want to be very precise with everything So it’s very important to be very clear about what you’re trying to do and get done and then get it done 100 percent clean because even 99 percent, there’s a little room for error, and then it’ll break up It’ll break sometimes You don’t want that You’re trying to always go for a hundred percent as best you can It’s not always possible but that’s at least the idea OK guys. So yeah a little trick here Everybody knows, well, I know that 2000 was a leap year So you can kind of calculate every four years 2008 2004 2008 everything up there If it’s a factor of four then you know that it’s a leap year from year 2000. Cool stuff Now moving on I think there is only one more function that I saw that was cool to actually play with everything else is kind of nitty gritty They might be useful in weird cases if you’re actually trying to problem solve But now I didn’t feel like showing it on a video on YouTube You know like I’ll dig it up if I’m actually doing a project and it’s useful to me but it’s just boring Like it would just bore you guys more than I am right now So the last one is how many leap days So this is exactly what I was talking about how you don’t want to be off by one day every four years if you’re trying to calculate over a long period of time whatever you’re trying to do especially if you’re doing Actually I had an internship where there was a guy who did a lot of He was an astronomer. He went to some big school I forget. This was years ago but he was doing astronomy Did a lot of coding for big astrological things I don’t know. Big cosmic thing Whatever the word is. I’m not an astronomer. Sue me But basically when he’s doing calculations on that scale like years in millenniums or millenniums those leap years really really add up So you actually need to know how those little tiny little things change to be that precise again Because when you’re on that scale even one day will screw you up. So very important But this little function here is how many leap days And then all that it does pretty much is it tells you how many leap days are in a range of years So let’s say Let’s collapse this a little bit so we can see So from year 2000 to year 2001 This might be exclusive or inclusive I’m not exactly sure we’ll test it out That’s usually the best way to figure it out It’s one of the other. Just try both and then you know the answer Like it’s almost fashion in googling sometimes just like “Oh try it.” Try something else, then you know So year 2000 was a leap year Remember that little 2000 trick I use 2000 was a leap year 2001 is not a leap year Because it only happens every four years So they should print out as one, okay? When I print this Hey guys I’m learning. I didn’t forget it again How many leap days. Let’s print this very last one out Click run And it says one So cool it picked up that year 2000 is a leap year 2001 is not. So in this range from 2000 to 2001, there was only one leap day that was skipped or added. (chuckles) I don’t know if they add a day or subtract a day I think it’s add a day. It doesn’t matter It’s irrelevant but you get the point Let’s see if this is inclusive or exclusive So the way I would test something like this is actually put in a year that I know is a leap year So 2004. So I actually know in my head that 2000 and 2004 are both leap years So there are two leap years in this range but that’s only assuming if this is inclusive It might ignore this year and actually only go 2000 up to 2003 So now when we run this if we get one or two it’s going to actually answer our question of if this function is inclusive or exclusive on the the second parameter here. This 2004 So we hit run And it’s 1 So it turns out that it is exclusive So actually it ignores 2004 So if you want to include 2004, You’d actually have to put 2005 Just like that or any bigger one So now I should print out, too, right guys? Right? Right? All right. (chuckles) So hey look, two. Right? We’re all right. We’re all right. All right All the time. Always right. Right. right Then if you wanted to put in maybe like up to 2020 Remember, exclusive so it’s not gonna include that Hit run. Then we’ll probably get four or five or something Yeah. That sounds about right And year 3000 should be about 250 Because there’s a thousand in there 243. Close enough Interesting. Actually maybe there isn’t a leap year every four years. Maybe every now and then it skips

because it should have been closer to 250 It’s 243 though. That’s interesting Maybe every now then, there’s actually a leap year every five years Google it from you guys Post in the comments That’s a cool little fun fact for you guys Well yeah that’s pretty much it for the calendar module I found this morning I just wanna make a quick or long video on it Long video (chuckles) but yeah It’d be very useful for scheduling things or whatnot There’s also a module called DateTime Time which I have used before Import date time And import time These libraries So what these are is you can actually I think this gives you a date and a time so you might actually be using this a lot more in our “How to automate stuff in Python” course coming out in a month or two Date time. But calendar would also be useful for making it more legible, Ib believe There’s probably some way you can leverage it And I’ll find a way to leverage it If I don’t then I’ve failed and I’ll go jump off a curb or something. (chuckles) But yeah So date time and time are also things you can look into And there’s actually some functions in calendar that actually interact with these other two modules that make them interoperable That’s a term in programming where you can have two different frameworks and two different programming languages that are interoperable You can actually embed Java code in Python programs and embed Python codes in Java programs which is pretty cool so you could actually say that Python and Java are interoperable They have full interoperability I use a lot of big words because they’re normal to me but I’m just gonna say them and when I catch myself then I’ll explain them so you guys know what the heck I’m talking about, okay? Because it’s just jargon that we use in this community because we want to feel smart when none of us are raw human We’re all done by default So never feel bad about that, okay? But yeah. So check out those other two modules That’s pretty much it I was gonna mention we do have a new course coming out called “How to automate stuff with Python.” I probably said that three times already but it’s a really cool course about how to automate things on your computer using Python So you can make Python work while you sleep. It’s doing stuff for you while you sleep You could have it send e-mails for you ten days from now or every Monday send an email to these sets of people You could be like, “OK I want to fire this person 27 days from now His name is Aaron. And I’m gonna go and call his accountant and I’m going to fire him 27 days exactly because I don’t have to deal with it so I’m gonna go on a trip for 27 days, schedule it, and then boom he’s gonna get the e-mail He’s just gonna be gone. Great It’s perfect. So stuff like that You could make little personal (chuckles) schedules to do whatever you want But that’s the kind of the things we’re gonna be working about and all of this stuff with python And that’s pretty much it for this video, guys Please stay tuned. I will see you guys next time I love your faces And I will (laughs) Yeah I’ll see you guys next time. That’s it Thanks for watching. Good bye Get the hell out of here, Qazi – [Qazi] You’re fired! – (laughs) Leave that in Bye guys (upbeat music)