Transcript for:
Python Modules & Importing

hey guys welcome back this is the 16th video in my Python Programming series and today we're going to be talking about python modules and import statements so this is something new that I haven't talked about yet but python is a modular programming language meaning that we can use multiple files together to create one program and you'll understand what I mean later in the video when I show some examples um but yeah so pretty much in Python at the beginning of all your Python scripts what you typically do is you write something like this it says import and you can see this highlights an orangey yellow meaning that it is a keyword in Python now there are many built-in modules or what they called um in Python that contain a list of functions and classes that we can use in our python script so for example the math module import math this allows me to do things like do square root or it's the square root function which I believe is like this and then you can put something inside of it um so yeah there's plenty of cool things like that there's another module it's called pame Uh we're going to be doing this later I'm going to be talking about game development with python if you want to see that leave a comment down below and I'll get it started soon then we have import we could also import something called OS and this gives us uh opportunity to do things like file paths and lots of different things like that we can import images we can import tons of different things into our files um in Python so we're just going to use the math module today for demonstration purposes but pretty much what happens is there's a bunch of built-in modules in Python so this one is known as a built-in module math uh when you download and install python it pretty much you download that module the pame module that I was talking about is not a built-in module that means you actually have to go to the internet you have to find it and install it on your computer before you can import it into your Python program and now with really cool about python is that it's an open-source language meaning that you can actually create your own modules um and they contain functions classes and you can use them in many of your different programs or you can also send them to a friend maybe or post them online for other people to be able to use so let's get right into it and uh let's do some things here so if we want to use this math module we have to first start by typing math so the name of our module here okay so math and then afterwards we're going to do the name of a function or a class in that um that math module so you see a bunch of things pop up there uh in that little box so if I do the dot again and wait a second it should pop up yeah so there we go and you see we have cosine sign tangent all that stuff uh degrees factorial all these things inside of our math module so these are new things that we wouldn't be able to use before so I'll show you if we do math. pi and I print that to the screen well we should get an endless number of digits like this math. Pi uh is it running there we go so it just had to print a bunch of numbers so math.pi it didn't print all of them I think it only went to 10 decimal points um so yeah and then I'll show you if I comment out this uh this import math what'll happen if I try to do this we get an error because well math is not defined we didn't import it at the top of our program so we'll uncomment this now now I can do things like math. degrees uh degrees simply turn something that is in radians into degrees so if I did math. Pi in here for example and we print that to the screen we get 180° like that uh they also have the math. radians I believe which does the opposite so it turns a degree into a radian so if we did 60° here that and we get 1.47 which is about pi over 3 uh radians like that so yeah there's lots of cool things that we can do with these modules now I'll actually show you an example of how we can use our own modules and how we can import our own modules into our function so I'm going to just start by typing import I'm going to type my module just like this okay um now I haven't actually created this module yet we're going to do that in just a second now I'm going to go and I'm going to find the directory that my main script is in so it's in tutorials up here I've got this open and remember uh how we had the file in here as well because it has to be in the same directory so I'm going to create a new file in that directory so file new file I'm going to save this one as my module now spelling is very important again capitals do matter uh and it has to be in the same directory as our main script which it is like that now inside of this module I'm going to write a very basic function if you haven't seen that video yet go check that out first um we just talked about some basic functions and what they do in the use of them so I'm going to Define I'm just going to call it my funk like this it's going to take a parameter called X and all it's going to do is return x + 5 so very basic function like that but just for demonstration purposes here okay and now I have my module imported like this so I can actually use my module so so I'm going to do print my module dot my funk because that's the name of my function and then inside I need to give it a number so what number do we want to use maybe we'll use six and we print this out and you see we get 11 so by simply all I did was create a new file put it inside of my um same directory as this script here and easy enough we were able to use it now I can actually create multiple functions in here here so I can create Define another Funk like this that'll be the name this one will take X again and inside here I'm going to return X integer division five and then again we could use that one so instead of my funk we'll use another Funk like this and let's just type in a big number like that and see what we get there we go so again it's working perfectly fine so that's how uh modular programming is what it's called Works in Python now obviously if you're going to be doing modular programming it's usually for larger uh programs and bigger programs where you want multiple files to keep things organized say maybe if you're doing a flight simulator you may have one module that includes all of your physics functions so you know that when you need to do physics you're going to import your physics function and then you can or module sorry and then you can use the functions from there and the great thing about this is that these uh these modules are reusable in other python files I can import them and I can reuse these functions I don't have to constantly rewrite them inside of my script like this so this has been a very basic tutorial uh just an introduction to modular programming I showed the math module a little bit if you want to learn about some of the built-in uh sorry modules in Python then just go ahead and go to their website and you can see a bunch of the different ones that they have another one that we may be using later is called o OS so the statement for that is just import OS and if you want to get started with game development you can look into pame which is this cool thing that I'm going to be doing a tutorial series on later so yeah that's been the video for today we talked a little bit about modular programming if you enjoyed please like And subscribe and I will see you again tomorrow with more content