Dive into Python modules and learn how to leverage pre-built libraries to enhance your coding capabilities. Explore how to manipulate date and time data, adjust time zones, and format outputs using Python's powerful modules.
Key Insights
- Understand the concept of modules in Python as specialized libraries or packages that extend core functionality and require importing, such as the date-time and random modules.
- Learn to use the PyTZ module to convert Greenwich Mean Time (GMT) to various local time zones, including examples demonstrating conversions for New York and Shanghai.
- Discover the use of string formatting codes through Python's strftime method for extracting and presenting date-time components like the year, month, day, and hour effectively.
Note: These materials offer prospective students a preview of how our classes are structured. Students enrolled in this course will receive access to the full set of materials, including video lectures, project-based assignments, and instructor feedback.
This is a lesson preview only. For the full lesson, purchase the course here.
Hi, welcome back to lesson three of Python for Data Science and Programming. My name is Brian McLean. We're going to pick it up with modules now.
In the last lesson, we learned about if-else logic, conditional logic, and Boolean operators. In the first lesson, variables and data types. Now we're getting into modules, which are libraries that you import.
If you recall, we have imported one module already. That was pprint
, or pretty print, which we use to be able to print in a nice way that looks nicely formatted as opposed to having a long print statement that you had to scroll sideways for. And in this lesson, we're going to systematically look at some more modules.
And then going forward from here on, we'll be importing modules with just about every lesson. All right, so I'm going to open up, double-click on the prog file, which is a copy of the start file, and dive right in. A module is a library, also called a package, that is code that you import.
Modules have their own methods and properties, and they go beyond the core Python capabilities. So kind of think of it this way. You can do a lot right out of the box with Python, right? You can print, you can get data types, there's all these array list methods you can call, you have int and string and input and all this other right-out-of-the-box functionality.
So think of it kind of as like tools that you don't have to go too far to get to. Most people have a junk drawer in their kitchen where you open it up and you're going to have a pair of scissors and a roll of tape in there. But if you need something more specialized that you don't use every day, maybe a drill or something, maybe you have to go to the toolbox.
The idea here, the analogy is for a module, that would be the kind of thing where you have to import it. You have to maybe go out to the garage and get it out of the toolbox. So because you don't use it as often, you're not going to leave the drill lying around on your coffee table.
So for those pieces of functionality that we don't use as often as say print and type, we're going to import them. So the syntax, as you may recall from that pretty print module, is you write the word import and then you write the name of the module. And you can also further give it an alias.
We imported pretty print as pp. We're going to import date time as dt for short. And then we're going to import something called random and run that.
And we can check the data type of a module. Either one of them will say print type random. Random is for generating random numbers, right? And the class is module.
So the data type of a module is a module. It's a package. It's a pre-built piece of functionality specialized in its nature.
So the random module has all these different methods and properties having to do with generating random numbers. The date time has methods and properties pertaining to generating the current date, the hour, the minute, the year, and so on. Now there's also a time zone.
If we print the date, to instantiate the date, to get the current date, in other words, we're going to declare a variable. We'll call it date time, then underscore. And we're going to set it equal to dt.
And the dt module, which is the little alias we gave it, has a date time property in it. So the module has a property by the same name in it. And then in there, there's this method called now, which returns the entire date time.
But this is in GMT time. So that would be five hours or four hours after Eastern time. So GMT.
So it's not the time off the user computer the way it would be if you do it with JavaScript. It's this Greenwich Mean Time, which will be plus four or minus three, wherever you are located. And then what we're going to do is feed it a time zone with another library so that we can adjust the time zone locally for us.
But before we do that, let's just print the time zone, the date time, and see what we get. And there it is. March 18th, 2025.
It is 10,22,22,11. 22,22,22. Look at that.
10,22 p.m. So it's right now it's 6,22 p.m. In New York, and that would be 10,22. It actually just changed a few days ago. The time went an hour later.
So last week it was five hours later. Now it's four hours. We're four hours earlier than Greenwich Mean Time.
So Greenwich Mean Time right now is 10 p.m. Here at 6 p.m. So the 21st, 22 is the hour. That's the minute. That's the second.
And that six-digit float, that's the microsecond, the millionth of a second. Pretty accurate. Now what we'd like to do is adjust the time for our time zone.
So what we're going to do is come up here and import another module. This is a much lesser-known module, not used anywhere nearly as much as time zone. It's called pytz, Python Time Zone.
And it allows you to adjust the raw GMT time zone for your local time zone. So what we're going to do is instantiate the pytz object here. We're going to say my time zone.
We're going to make a variable called my time zone. We're going to set it equal to pytz, which is the module we just imported, dot time zone, which is a method or function of that property. We have an object called pytz, and it's got a method, a function.
You call on it with dot. Remember, a method is a function with a dot and an object in front of it. And then you pass it your time zone.
And I'll show you soon how you can get a list of all the time zones. So eastern time zone is the eastern coast time zone. So then what we do then is we're going to adjust the time zone.
We're going to say my date time now. So this is a GMT date time. But we're going to say my date time is going to equal DT again, date time once again, dot now.
We're going to call the exact same method, the now method, on the date time on the DT, except this time we're passing it this my time zone object that we instantiated off the pytz object. Long story short, we now have my date time as opposed to GMT date time. Let's change this to GMT date time and run.
We'll say we've got the GMT date time, and we've got my date time, which would be adjusted for New York. Let's print them out. Yep.
So GMT date time, there's your 10 o'clock, 2200 hours, and here it is, my date time adjusted for New York, four hours earlier at 1800 hours. So that would be 625 PM. And you can also try different time zones.
Let's say we'd like to do, let's go my date time for Shanghai. Run that. No, that's not what we want to do.
We want to do this. We want to say my time zone rather. And then we're going to re-instantiate my date time.
Okay. Now we're looking at tomorrow in Shanghai, 319 instead of 318, and it's six in the morning, right? That makes sense. Shanghai being 12 hours later.
I'm going to turn that one off and keep the East Coast time zone. And you've got all these different ones, US Pacific, that would be the West Coast, United States, and so forth. So here we are adjusted again for New York.
So take a look at that. And to do that, you'd have to know your time zone code, right? So let's come on down here. Let's print out all the time zone codes.
So there's this property of the pytz object, and it's called all_timezones. It's a property, it's not a method, it just doesn't have parentheses. It's not a function.
It's just, you're just reading a property. We're going to say, we're going to print the length of it. And then we're going to print, let's first print the length of it.
596, there are 596 time zones, because that is a list. In fact, let's print the type of that. What is all time zones? If it's got a length of 596, it must be a list, right? It's called a lazy list.
All right, what if we listify it? Pass it into the list method. Now it's a real list. See what we did here? It came up with this weird lazy list thing.
It's kind of like a special kind of list. But if you pass that into the list method, it will listify that. Now what we're going to do is print all the time zones, we'll listify all the time zones, and then say, you know what, we just want the first 20.
If you recall, you've got this syntax where you can take a slice of a list from the first one to five, that's what that is. And we could say we want to have 350 to 355. Remember, there's 596 items, so we can go over to the middle here and get another five.
And if we come up here, let's grab import ppprint as pp. And now let's print more. Let's print some more time zones, but have it pprinted.
So it looks nice, won't go sideways. Now we could say, let's get 350 to 375. So that's time zone 350 to 375.
Here's 450 to 475. And that's what you do. I mean, you could print them all and just look through or Google it, you know, get a list of them.
So here are time zones, all of them listed out. So that's how you instantiate the time. Now that we have our time, we can go in and get, of course, we don't want this raw blob of time, right? We'd like to go in and grab individual pieces of time, individual units, right? Hours and minutes, and maybe be able to output the time.
And do other, do other, we could even use if-else logic, which we're going to do. We're going to look to see if it's time to say good morning or time to say good afternoon. So let's get some individual date time values.
Let's get the year. We'll say year equals my date time dot year. That is a property.
It doesn't have, by property, I mean it doesn't have parentheses. It's not a method. There you go.
And the year is 2025. Let's get the month as an integer. We'll say mo month int equals my date time dot month.
The mo int being March ought to be a three. Yep. Three for March.
And of course, we might want the word March or the short version Mar, right? The long or the short version of a month or a day for that matter. So here are some codes. These little stringy format codes with percent signs that will give you, that you feed into this date time method called strftime, string format time.
So you've got your date time object and then you say dot strftime and you feed in one of these symbols and it'll return you the date, the date object, the date unit as a string or however you want. If you'd like the month, the long month, right now it's March. If you want March spelled out, you would pass the percent uppercase B. So let's say, let's call this the long month and that will be my date time and dot strftime, string format time with a string of five percent B. And that should return you a March and it works.
And then the short one, you want the abbreviated month, why don't we call it short mo, maybe we'll abbreviate the word month. And that would just be a little b, that's clever, right? The long, the big, you know, the long month is a big B and the little month is a short b, a short b, a lowercase b. Likewise, we can get the long day and the short day. So let's get the long day, the full day, spelled out.
Today is Tuesday, so we'll say long day as opposed to two T-U-E. That's going to be another strftime, my date time, strftime, and you're going to pass it a, percent big A. There it is for long day, Tuesday. And let's print the long day.
It's been a long day. All right, look at that. And now let's do the short day.
We'll say short day. Let's say short day, yeah. Short day, that's just a little a. There you go, two.
Now to get the date, that would be the date written out as today's the 19th of, 18th of March would be like a three slash 18 slash 20,25. We'll say today's date. That is going to be another my date time object, strftime.
And this time it's taking percent D, big D, or today's date as month, date, year with the year spelled out. That'd be the mo-int, right? It's not going to say the name of the month. It's going to be the, that would be the full year too, four digit year.
Let's run it. Yep. 3,18,25.
It worked. Good. Put a comment here.
That's what you can expect to come out. So a lot of similarity, right? It's the date object that you instantiated that you made a variable out of off of a module. That's what instantiation means.
You have this object that when you call a constructor function, as it's known, it'll return you an object. If you back up a minute, we've got this dt.dateTime.now, which takes a time zone that you instantiated here. And it's going to return you this, my date time object that you can then call all these methods and properties on.
And right now we've been tapping the properties as a, that year, month, no, no parentheses. Those are properties, but strftime's a method because it's got parentheses and takes an argument. Takes a string code argument that returns you the long month, the short month, the long day, the short day, the day, you know, the month slash date slash year.
And the little d will give you the date number as a string, just the date number. Not a string, right? That should be an int. Let's find out.
I think that would have to be an int, wouldn't it? Yeah, it's not going to give you strings, is it? Let's find out. All right. So today is the 18th.
We will say, let's just get the date as an int. And we'll say my date time, little d, and let's get the type of it, make sure it's not a string. Oh, wow.
Oh, date time, date time, my date time dot strftime. I forgot the strftime. Oh, it is a string.
It is a string. It's not an int at all. It's a string.
So, but if you'd like it to be an int, we can make it an int, right? We could say int my date time, strftime, right? Just intify it, right? We've done that. Wrap a stringy number in int. Ah, now it's an int.
Like you could do math with it. You could, you know, calculate how many more days till a certain date or something. So it is a string.
Yeah, yeah, yeah. I should have not left it alone. All right.
Let's get the current hour. We're going to say hour equals my date time again dot hour as a property, no parentheses. If it were a method without parentheses, this is just the word.
We're looking up a property. Let's check the data type of it. It's probably a string.
Oh, it's an int. As int, so the hour is an int. Makes sense.
The minute would probably be an int as well, I would imagine. Let's get the current minute. We'll say min, I don't, well, there's, min means minimum.
I don't like to say, I would not, I mean, you could do it, but I don't like, I wouldn't want to declare a variable called min because min is a, like there's a method called min for minimum. How about mini? So mini for a minute or minu, or minuto, how's that? Minuto, who says it has to be English? That's going to be minute and it is an int. And the segundo, the second, we'll say second, that's going to be myDateTime.second, sec, print that.
I don't know if I want, sec is too much like secant, like there's a geometric, right? Co-secant, tangent. Let's just go with segundo. And that is an int as well.
That's how you get the various time units.