We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2
So modules are very useful constructs in Python.
A module basically refers to a file that
contains Python statements and definitions, right? So for example, if you have a Python code in a file called ABC Py. Py stands for Python file. Then you can load this, you can call this module by just using its name, which is ABC. So what it does, imagine if you're writing a big project where lots of people are writing code, right? Then each of you could write your code in your own module and others who want to use your code can reuse their code, right? Like I can reuse the code that is written by somebody else. And modules are extremely important because for most of machine learning and data science, there are a lot of modules. There are very, very high quality modules which are written by experts in machine learning and data science, and we will reuse them so that we don't have to write the code ourselves from scratch, right? So let's understand how modules work and what are some of the important operations we're just going to look at very, very simple operation, very, very simple modules, right? So you can import a module, which means you can actually start using a module in your code by using the import statement. When I say import example, it's basically going to import all the functions, all the variables, everything that we have in example in this module called, this is the module name. This is basically the module name. So we are going to use all the functions, all the variables, all the constants that we have here and we can use it in your code. Right? Now, for example, I could say example, add ten comma, 20, right? So add is a function. Add is a function that is defined in example module, which I'm using here. Now, Python has lots of modules, by the way, and you can find the list of standard modules available in Python in this location. So this is Python three s, all the modules that are available for you to directly use. Having said that, let's look at some more interesting and useful modules, especially for machine learning and data science. There is a module called math, right? If I just say import math, all the functions, all the constants, all the variables that I have in this math file can now be imported to my code. I could just say print math PI. So I don't have to remember what PI is, I just have to say import math and print math PI. So because in math PI, someone else has written the actual value of PI and stored it in a variable called PI. Similarly, there is a module called data time. Sorry, date time, sorry, not data time, sorry. The moment I see dat, I think of data. I think it's because I've worked with data so much. So this is called a date time module. So in daytime module, we have a very interesting function which is datetime. Datetime. Nuff. What it returns us is basically, it tells us exactly what is the time right now, right? That's what the date time module literally does, right? Of course, if you run it right now, you'll not get this date, because this is a date probably in the past when I was doing this recording. Right? Now, having said this, there are also other ways to import, to import a module. Instead of just importing math, I can say import math as M. So wherever I have to say math something. Now, wherever I have to say math PI. Because I'm importing math as m, I could just write m PI, right? So this way sometimes it's useful, like for example, if you have a long name. So there is actually a library called matplotlib. Every time, instead of writing matplotlib, I could just write import matplotlib as MP. Now, wherever I have to say matplotlib something wherever I have to say matplotlib. Let's say some function xyz. Instead of calling it, I can just say Mp xyz, right? This will make writing code easier. That's it. There's nothing more fancy about it, right? So you can import with renaming. This is called renaming. I'm renaming math as M. Now, there are other types of simple import functions. For example, if you do not want to import the whole module itself, but you want to import only one function or one variable or one type of data type from your whole module, you can do it. So you can say from date time module, import date time, right? Because you don't want. So sometimes modules can be very large. Sometimes modules can be large. Modules can be large. In such a case, why load the whole module? All you need is probably one small part of a module. So just load that small part, right, for calling the datetime now function, I don't have to import the whole date time, whole date time module. I just need this. I just need this function or this code or this class in this case, right? So similarly, I can also say I can import all the names, for example, from math. Import star basically says this is equivalent to import math. This is literally equivalent to import math, right? Because I'm saying from the math package, import everything else. So I'm using a star here, which means import everything. Now, the other very useful function here is a dir function, right? So dir function. For example, if you have a module called example, if you type dir example, what you get here is basically all the functions and all the names that are defined within this module. So dir helps you list, it helps you list or print all the names in a given module inside a module, right? Inside a module. That's what Dir does. It's very useful. Now again, by the way, you can use doc strings, as we learned earlier. Now if you want to see the doc string, for example, add. See, we didn't define this, but someone else who actually created this add function must have written the doc string, right? So I can just print the doc string here. Again, example add underscore, underscore doc underscore, underscore. Now, when I do this, what I get here is this program adds two numbers and returns the result. So even for programs which are in different modules, not written by you, suppose this is a module ABC, and here you are importing ABC. Suppose if you're importing ABC here, and let's assume there is a function f here, right? If somebody has done the doc string for f here, I could just say ABC f dot underscore, underscore doc string. Now this will tell me what exactly f is doing. Even I have not created f, I've not created ABC. Someone else has done it. So when you have to use others code, when you have to use others programs, it's very, very useful to write docs so that when you write your code, also please try to use doc string so that others can understand what you're doing.