WEBVTT 1 00:00:01.960 --> 00:00:06.230 let's start talking now about the Python Standard Library now this standard 2 00:00:06.230 --> 00:00:10.610 library that ships with Python provides a wide range of modules that we 3 00:00:10.610 --> 00:00:13.990 can use so it includes everything built into the language that we can use 4 00:00:13.990 --> 00:00:18.869 without explicitly importing anything plus whole lot of other useful modules 5 00:00:18.869 --> 00:00:22.869 such as shelve and random which of course we've used in the course previously now 6 00:00:22.869 --> 00:00:28.180 I did say without explicitly importing anything because the core built in modules 7 00:00:28.180 --> 00:00:32.720 are imported for us automatically so I'm gonna create a new file this is the 8 00:00:32.720 --> 00:00:36.699 same project from the previous video so I'm gonna create a new Python 9 00:00:36.699 --> 00:00:43.320 file and will call this one example so when useful command that's gonna let us see 10 00:00:43.320 --> 00:00:47.920 is dir command or dir function and thoes that uses will note that this is not 11 00:00:47.920 --> 00:00:52.260 related to the dir command you can use from a command prompt to print out a 12 00:00:52.260 --> 00:00:55.870 list of directories so to see what is imported for us automatically we can 13 00:00:55.870 --> 00:01:01.710 print the output from dir so we could do something like.... 14 00:01:01.710 --> 00:01:08.200 to call the dir method run that and right 15 00:01:08.200 --> 00:01:13.700 click Tim and click run example I remember this time and you can see when I've done that 16 00:01:13.700 --> 00:01:17.670 we got a whole heap of information 17 00:01:17.670 --> 00:01:20.840 as you can see there, now we're gonna be covering this in a lot more detail later 18 00:01:20.840 --> 00:01:24.450 but it's worth noting that anything whose name starts with an underscore 19 00:01:24.450 --> 00:01:28.820 here is something that we shouldn't be changing without good reason so if the 20 00:01:28.820 --> 00:01:32.490 name starts with two underscores like these two and then not intend to be 21 00:01:32.490 --> 00:01:37.130 changed at all so in fact there's no such thing as private 22 00:01:37.130 --> 00:01:41.280 variables in Python and everything is available everywhere but the convention 23 00:01:41.280 --> 00:01:46.039 is to use underscores to intention so starting a name 24 00:01:46.039 --> 00:01:50.700 with an underscore indicates that it's private to its module but it's up to you 25 00:01:50.700 --> 00:01:54.590 as the programmer to inspect that convention because Python itself doesn't prevent you 26 00:01:54.590 --> 00:01:57.319 from changing and that's done by design 27 00:01:57.319 --> 00:02:02.630 but getting back to the dir as you can see on the screen here now we are not going to look at most of the entries as they are 28 00:02:02.630 --> 00:02:05.840 to do with the internal running of Python but a good one here is this 29 00:02:05.840 --> 00:02:10.450 first one underscore underscore builtins underscore underscore this is the module containing 30 00:02:10.450 --> 00:02:15.000 the Python built-in identifiers and there is actually a list that I'm going to put on the 31 00:02:15.000 --> 00:02:19.040 screen and I'll share this link in the Resources section and will just open a 32 00:02:19.040 --> 00:02:26.790 browser and go that URL now that's a decent list of all the built-in functions from or based on 33 00:02:26.790 --> 00:02:31.180 that module the built-in modules but with that said that's one way of 34 00:02:31.180 --> 00:02:35.900 doing it but we can also use the command to find out a bit more as well 35 00:02:35.900 --> 00:02:45.079 so come here on the next line ill put.. 36 00:02:45.079 --> 00:02:57.750 ...and if we run that that gives us a whole heap of 37 00:02:57.750 --> 00:03:02.290 stuff and obviously you want to scroll that but this is Python so we can easily 38 00:03:02.290 --> 00:03:05.950 display that of course in a more readable format as we've done this before so 39 00:03:05.950 --> 00:03:09.770 let's just comment that line out and use our 40 00:03:09.770 --> 00:03:29.500 handy for so.....so run again and I need a colon there so run again and now we got a decent list here so we got 41 00:03:29.500 --> 00:03:36.600 a whole heap of errors at the top but also just scrolling down the list you will see there is the false one 42 00:03:36.600 --> 00:03:44.980 there is none we seen that before and true down here as well that just 3 and.... 43 00:03:44.980 --> 00:03:52.670 if you go a bit further you can see the dir hex input 44 00:03:52.670 --> 00:03:53.500 and bin up here 45 00:03:53.500 --> 00:03:58.500 slice and so on so essentially you should be able to recognize quite a few of these other 46 00:03:58.500 --> 00:04:01.840 entries and even things like list is there on the screen 47 00:04:01.840 --> 00:04:07.700 map open print etc of course that's more interesting than actually useful but if 48 00:04:07.700 --> 00:04:11.500 we import shelve we can have a look at what that provide so lets close this down 49 00:04:11.500 --> 00:04:18.739 so just delete the codes here and start again so what we can do is import shelve and 50 00:04:18.739 --> 00:04:31.180 have a look at what it actually provides so... 51 00:04:31.180 --> 00:04:41.000 ....and run that and you can see we got a whole heap of bits and pieces there 52 00:04:41.000 --> 00:04:46.370 relating to shelve and the first list shows you shelve on the list because we imported 53 00:04:46.370 --> 00:04:51.889 it that's now coming up in our list when we print dir and printing dir on the shelve as you see on the 54 00:04:51.889 --> 00:04:57.139 third line there has shown the objects and methods available from the shelve module 55 00:04:57.139 --> 00:05:01.110 of course we've used the open function before to open a shelve and this returns 56 00:05:01.110 --> 00:05:05.139 a shelve object so you may notice that close doesn't appear in the list and I'll just run it again 57 00:05:05.139 --> 00:05:15.950 so looking at the list when we use open there is the open method in the end and when we do 58 00:05:15.950 --> 00:05:20.550 that it returns a shelve object one of these but you will notice that close doesn't 59 00:05:20.550 --> 00:05:23.770 appear in the list and that's because it's a method of the shelve 60 00:05:23.770 --> 00:05:27.820 class we're not interested in the underscore method so we can filter out to see what 61 00:05:27.820 --> 00:05:36.599 a shelve object provides just to show you how that works so we can do something like, will leave the shelve import there and will put... 62 00:05:42.460 --> 00:06:00.830 and I will put a test here to ignore underscore so..so let us run that and you can see now 63 00:06:00.830 --> 00:06:05.319 that apart from pop and popitem we've seen all these other methods either when 64 00:06:05.319 --> 00:06:09.000 looking at shelve or when discussing dictionaries and in face Intellij provides 65 00:06:09.000 --> 00:06:13.150 a similar list plus a lot more in order of completion we type the full stuff after shelve 66 00:06:13.150 --> 00:06:24.520 so if we type something here going back to the code so if we type....you can see there is 67 00:06:24.520 --> 00:06:27.870 a big list of things that IntelliJ will bring up automatically 68 00:06:27.870 --> 00:06:31.560 IntelliJ is quite smart that way that it can present all this information to us as well 69 00:06:31.560 --> 00:06:35.900 now some of the standard modules are written in C these are generally the 70 00:06:35.900 --> 00:06:40.020 ones that provide access to system functionality such as File I/O but many of 71 00:06:40.020 --> 00:06:42.889 them are actually written in Python and we can examine the source code from 72 00:06:42.889 --> 00:06:46.810 IntelliJ holding down the Ctrl key and clicking on the module name shelve 73 00:06:46.810 --> 00:06:51.580 in this case so we can come over here and we can I believe its control 74 00:06:51.580 --> 00:06:56.389 on a PC but on a MAC it is command click on a command hold command down and click on 75 00:06:56.389 --> 00:07:01.919 shelve that's gonna give us access to the source code for the shelve.py and 76 00:07:01.919 --> 00:07:06.979 you can just scrolling down there is some actual code that is part and parcel of Python 77 00:07:06.979 --> 00:07:10.320 so until we have covered functions and classes the code you can see on the screen 78 00:07:10.320 --> 00:07:13.219 probably looks a bit horrible to you so we are gonna leave it there and have a look at some of the 79 00:07:13.219 --> 00:07:17.570 standards library code later in the course so on the next few videos we are gonna look at 80 00:07:17.570 --> 00:07:21.159 some of the modules that come with Python but will finish this video by saying how the 81 00:07:21.159 --> 00:07:25.530 help function can be useful to work out how to use some of the 82 00:07:25.530 --> 00:07:26.960 various modules 83 00:07:26.960 --> 00:07:33.539 so let us just close this file off we only got shelve imported so what I'm gonna do is delete that and on 84 00:07:33.539 --> 00:07:39.580 line 3 I'm gonna type.... 85 00:07:39.580 --> 00:07:43.680 if I run that you can see that actually gives us quite a bit of information 86 00:07:43.680 --> 00:07:49.180 about , quite a detailed amount of information about shelve how to use it 87 00:07:49.180 --> 00:07:54.199 description of what it is the various parameters interfaces and so on and also a link to the 88 00:07:54.199 --> 00:07:55.180 documentation 89 00:07:55.180 --> 00:07:58.919 so you can see here there is a link we can get and from their we can click on that goes straight 90 00:07:58.919 --> 00:08:11.319 to documentation in a browser so copy that go back to the browser and press enter 91 00:08:11.319 --> 00:08:14.050 generally following the link is better because you get the object or output rather 92 00:08:14.050 --> 00:08:19.210 you got this way of looking at the data or coming back to a browser and seeing it in that format 93 00:08:19.210 --> 00:08:23.590 to me I would prefer to use this format the browser I think it's a lot easier to 94 00:08:23.590 --> 00:08:27.860 read but it's personal preference and you got the ability to choose either way 95 00:08:27.860 --> 00:08:31.129 when you actually choosing a documentation and we do suggest that you 96 00:08:31.129 --> 00:08:34.800 do take some time in read this documentation you don't need to know 97 00:08:34.800 --> 00:08:39.240 or understand it word for word but it can be a good way to help solidify your 98 00:08:39.240 --> 00:08:42.880 understanding of how some of the internals work within Python it is really really 99 00:08:42.880 --> 00:08:48.360 handy and you can also request help on individual functions so for 100 00:08:48.360 --> 00:08:55.199 example we could change that to import random and here we could type... 101 00:08:55.199 --> 00:09:00.410 .....and we can get some help just on that again it is quite short but it does give you the 102 00:09:00.410 --> 00:09:04.490 information relating to the parameters and so forth for that particular function 103 00:09:04.490 --> 00:09:07.930 so I'm gonna end this video here in the next one we're going to continue our discussions 104 00:09:07.930 --> 00:09:09.610 of modules and imports