Python Modules for Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello and welcome I'm Dave today we will learn how to create and access modules in Python and I'll provide links to all resources in the description below I've got vs code open and you can see I have a new lesson 14 folder over here in the file tree let's create a new file and let's call this modules dot Pi now inside of our modules dot Pi we're going to actually import something first and we've done this previously so modules can be considered small code libraries that are based on related features and one example of this is the math module and it contains functions and constant values for use in mathematical equations so we can import math and that's a built-in module in Python so python comes with this module but you have to import it to use it and now let's go ahead and access something from math and if we want to do that we can just print and then say math and then I'll use dot notation and notice what happens all of these different things that are available in the math Module come up and I'm going to scroll through here just so you can see a lot of them but we're choosing a constant and I'm going to choose Pi so we're just going to print Pi inside of our output window and I'm going to add a couple of extra lines just so that doesn't scroll up on me like that and then I'm going to go over here to our drop down and run the python file and notice we get the value of pi here in our output once again I'm going to go to view and appearance and then to panel position and move that to the right just so we can see our output over here today but now you know what I'm doing I'm either getting functions or constant values from the math module and of course we could bring in any type of data that could be in the module there may be a dictionary in there or a tuple or something else that we can access now earlier in our series we also imported a couple of other modules that we're using in our rock paper scissors game one of of those was CIS and another was random and now later in the code we were using some functions so one we used to exit the program and that was CIS dot exit and then we call that function and then another one we used was random and or random.choice actually I should say random dot choice and we pull that up and then it shows one of the characters of a string we provided like one two three so just a couple of functions that are available from other modules that we've already used now here we're importing the entire module and then we're using dot notation to pull a function or a value from that module but instead of that we can just pull out what we need and you have seen that once already in this series too because in our rock paper scissors game we're using enum and to do that we say from enum which is the module then we say import enum or we could do any of the other things that are available from that module but now we're only importing enum from the enum module and I know with the same name that's a little confusing but just for example if we didn't want to say math.pi instead of import math we could say from math so we would from math here and then we would just say import pi and then notice how vs code already realizes we're now using the wrong syntax then we would just refer to Pi as we want that output and we can run our code again and we still see the same value output here now something else we can do is create an alias so if we don't want to refer to a module by its name say random we could create an alias by just typing the word as after and then I could say RDM and then I could refer to RDM Dot and then still have all of these choices here so like the choice function that I was using before so how do we know what to use from a module well there's a few ways to find out what's inside of a module one of those ways and I'll just create an extra line here but is to use the dur function so I'm going to print here just so we can see the output and then I'll call the dur function and then inside of that which der I believe would be an abbreviation for directory if I'm not mistaken and then inside of that we can just pass whatever module we want to now I've got an alias for random so I'll just put that alias in here and this will work but notice the output when I go ahead and run the code after I save it it's got a list of everything in the module but boy is that kind of hard to read it's literally a list so list type data and then we've got everything in here and it's all jumbled together but we can make this more legible for us actually because what we'd like to do is just put one of these on each line and then see everything inside of the module so let's just Loop through it so I'll say four item in dur and then pass our random module but I'm passing in the Alias of course that we created and now I'm not going to print the directory of random here instead I'm just going to print each item as we go through the loop so I'll save this and run the code again much easier to read now and we can see everything that is inside that module now of course I already showed another way to do that and that is with DOT notation so I just type the module name or the Alias of that module and then I use dot notation so I type a period and vs code helps us out so we can see all of these different things that are available inside of this module but neither one of these ways actually tell you what any of these constants or different data types or functions are inside of the module so for that let's go to the docs quickly I'm in the python documentation now as you can see here at the top it says documentation for python 3.11.3 we're at the python module index now if you're watching this in the future and python has gone to a newer version that's okay you should still be able to find this python module index and here is an alphabetical list of all the different modules that are available and then if you go into the module so let's find one we were using like enum here I'll click this and it takes you to a page that shows all of the details for that module and it details everything and describes everything inside of the module so this is a great reference and of course I'll link this in the course resources for you to go with this lesson okay now we're back in vs code and we're going to look at a custom module that I created notice it's named Kansas dot Pi so like any other python file it ends in dot pi and then inside of the file we've got some values here so I've set the capital the bird the flower and the song for Kansas and that's the state that I'm from so you could create one of these files or modules if you will for where you're from if you want to just as an example to go along with what I'm doing but we've got a few values here and then we've got a function that says random fun fact and you can see inside of this random fun fact function I have a list that has four fun facts about Kansas and then notice I'm also importing something from another module because you can do that inside of modules that you create as well just like other python files so from random I'm importing the choice function so then down here I'm creating the index value using the choice function and choosing between 0 because remember our list would start with an index of zero so zero through three so it's going to choose one of those and this is a string so then when we use the index we need to convert it to an integer here and we're doing that with the Constructor and then we're just referencing which index it is inside of fun fact and we print that so this might surprise you but there's really nothing special inside of a module we've got data and we can have functions as well and you can use other modules inside of a module that you create and so that's what we have here let's go back to our modules file now inside of this file we can import our Kansas module or if you created one for wherever you live that's fine too so we've imported Kansas now let's use what we get from the module so I want to print the Kansas capital and notice with DOT notation I see the other things available in the module as well I'm going to choose Capital so I should print out that value now remember our function already prints so I don't need to call Print here so I'll just say Kansas and then random fun fact and we'll just call that function and it already prints if we go back you'll see at the bottom here it returns a print and we don't really need that return there even I don't think I had that before so let's just go ahead and remove the return it should still be fine without it and I think that's what I showed you before so here we'll go ahead and save and now let's run our code and see what we get from bringing in the Kansas capital and the random fun fact so here at the bottom the Kansas capital is Topeka and then we have our random fun fact Wichita is the largest city in the state but many would guess that that is Kansas City that's also true much of Kansas City is actually in Missouri okay so we're using our new custom module now and now we need to talk about one special value that every module has and that is the name value and it is surrounded by two underscores but both before the word name and after so let's look at that and we can print the name of the module that we're running so let's print and then let's just say two underscores name and two more underscores now do you think this will be modules it possibly could be but maybe it won't be let's go ahead and see what we get when we run our code again and notice the name is Main and Main also has two underscores both before and after the name main that is because this is the module we're running so we if we print the name name of the Kansas module for example we could say Kansas Dot and if we scroll through here we should find name there it is so that's available to our custom module I'll save let's run this notice the name of the Kansas module is Kansas and that's because we're importing that module so it gets the file name just like we have here Kansas dot Pi so the name is Kansas but the file that we're running modules.pi its current name is Main and that indicates that that's the file that is being ran now this can be very useful so for example I'm going to go back to the Kansas module and at the very end of the file here I'm going to put an if statement and I'm going to say if two underscores name two underscores equals and then I'm going to put in quotes main with the underscores then inside of that we're going to go ahead and run our random fun fact function so now this function is only going to run that's only going to be called into action if this is the main file oh and I only put one equal sign there we actually need 2 as we compare we're not assigning we're comparing here so that needs the two equals so if name equals Main and you'll see this in many modules because this way they'll only run this code after this if statement if they are the main file in other words this file is being called into action so let's go ahead and run our Kansas file now and see what we get we have a fun fact from Kansas Kansas is considered flat but it does have a mountain I believe we saw that one before so this file will run this function if it is the main file so that can be very useful when you create modules now if we come back to our modules here and we go ahead and run this modules dot pi we won't see anything different than what we had before we don't see the fun fact function running twice as a matter of fact I could comment out the fun fact function and now we shouldn't see it run at all if we do that and we don't we just have Topeka and then the two different name values here Main and Kansas that we printed at the bottom but it's important to do that if you want to have something called inside of your module because if we don't do that it will run every time so if I comment out the if statement here then we'll have to of course change our indentation just bring this back to the front now when we import the Kansas module this will run so we should see a fun fact executed twice because we're also doing it inside of our modules file let's go ahead and double check that I'll run the code and while we're not seeing the result here because of this long list but if we're to scroll up there's actually another random fun fact at the very top of the list and here we see it Wichita is the largest city in the state so we see that before we even print pie let me go ahead and comment these out again I'll select all of those and do control with the Slash and it will comment those out and now it won't be so far between when we see our output here we'll run the code again and notice we've got a considerable portion of Kansas city is in Missouri and oh I commented out the other Kansas information too I meant to just do the loop let me try that again I'll uncomment by doing the same thing select what you want and then control in the Slash and I uncommented both of those let's save and run the code again okay now now we actually have two fun facts running even though we're only calling it once in this file and that's because this first fun fact runs right now when Kansas is imported the Kansas module that's because we don't have this if statement another important thing is if you leave this over here it could actually to be included in the import list let's see if it is I'm not so sure if it is or not but let's check say Kansas and then we'll Dot and we have random fun fact here let's see if another one is included it's not by vs code at least knows not to do that however maybe not all tools with python do so one thing to consider is you don't want this included in what's available to import as well if another python tool were to allow that so it's much better to put this if name equals Main and then of course we need to indent this back with tab there we go and now we'll only get the fun fact that we intend to run inside of our modules file and this will not run unless Kansas is the main file so let's go ahead and run our code one more time just to prove that and yes the first thing we get is the pi value and that's up here and then we get the capital then we get the fun fact and the two names okay now that we've covered the name value let's close the terminal window let's create a new file over here and we'll call this rps7.pi now we're just going to start with the code that we finished with in the last lesson on closures which was rps6 for our rock paper scissors game so I'm just pasting this in and this is the exact code you should have finished with in the previous Lesson Four rock paper scissors now we just want to make a couple of changes here to make our RPS 7 dot Pi a module so let's go ahead and scroll to the very bottom and once we get to the bottom here of our file what we're going to do is I'm going to rename this play and I'm going to name it Rock underscore paper underscore scissors and we're setting that equal to the RPS function and that's because we're using a closure that we learned about in the last lesson now the nice thing when we make this a module is the rock paper scissors function that we want to call will be available to import now underneath this let's go ahead and delete this let's put our if statement so if two underscores name two underscores equals with two equals then two underscores main two underscores inside of this if statement we'll go ahead and call our rock paper scissors function so this will only execute if our rps7 DOT Pi is the main file so if we go ahead and run this code rock paper scissors would execute but when we import the file it will not automatically run it will allow us to call the rock paper scissors function where we want to so now inside of our modules dot Pi let's go ahead and import this and I'm going to say from rps7 and I'm going to import rock paper scissors now of course we could just import rps7 and then we would have to refer to this as rps7 dot rock paper scissors but here is our rock paper scissors function and it's ready to be used so we will call it right here at the bottom of our modules file and now after we see these different values print out the game should start so let's go ahead and run our modules file and yes we're ready for a game of rock paper scissors let's go ahead and play and python one so I'll go ahead and quit this time instead of playing until I win but now you know more about modules we've got built-in modules in python or you can create custom modules as well remember to keep striving for Progress over Perfection and a little progress every day will go a very long way please give this video a like if it's helped you and thank you for watching and subscribing you're helping my channel grow have a great great day and let's write more code together very soon
Info
Channel: Dave Gray
Views: 7,327
Rating: undefined out of 5
Keywords: custom modules in python, how to create modules, how to create modules in python, how to create python modules, how to import python modules, if __name__ == __main__ how to create a python module, import from python, modules for beginners, modules for python, modules for python beginners, modules in python, python, python built-in modules, python custom modules, python import from, python module index, python modules, python modules explained, python modules for beginners
Id: 8ArHkS70QsQ
Channel Id: undefined
Length: 18min 3sec (1083 seconds)
Published: Tue May 16 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.