Python + MYQL FREE Sessions

Learn the difference between Python modules and libraries, how to import them, and what built-in modules like math, random, and os do. The session covers pip installation, aliasing imports, and popular third-party libraries like NumPy and pandas.
Chapters
Yeah, am I audible to everyone? Alright, so let's get started. So in the last session we discussed about file handling and the exception handling. That if there is any error, then how you have to handle that. And if you are getting multiple errors, this is going to stop your program to run. So in that case, you have to use the exception handling. But in this video, we are going to discuss about modules, libraries in Python. So as we all know that Python is simple and versatile language. Why we say it versatile? Because we In Python, we have so many libraries, so many modules that helps us in making our task easy. These libraries these days are helping developers to reuse the codes, or you can say to integrate more functionality. This is going to also solve your complex problems efficiently. Okay, so there are two things. One term is module, another term is library. The first, which is module— is module means in Python a file that contains Python code. It means in, in .py file, if you have written functions, classes, variables, data types, all these things in one Python file, so we call it module. And module can be reused in other programs. So suppose I will give you an example from these, uh, this folder itself. So let's say I have created day1.py. So this can act as a module for your next Python file. Suppose if you are writing something in this file day1.py, then you can reuse it anywhere in your next files. So this also allows you to organize it and manage your code. Suppose you have written one of the specific logic in this .py file and then you are creating the second one then you can reuse it. This is what we called as user-defined module. Just like we have user-defined function, same way we have user-defined modules. So within this file, if you are going to write anything, then what you have to do is you have to go to the next file. Suppose I wanted to reuse this day1.py, okay? For now I haven't written something meaningful in this, but just for an example, if I have written something in this and I wanted to reuse it Then what I have to do is I have to move to the another file in which I wanted to use it, and then simply write a command import day1.py. What is day1.py? day1.py is the name of the file. So in this case, when you are calling your file, you don't have to write .py, it's just day1 you will type, and it will import that file for you in this particular notebook. It depends how you have named that file, and that file should be .py file. If you want your module to act as a module and you, you want reusability, you have to create .py file and simply use import statement to import it. There are two types of modules. First is built-in module, second is user-defined module. Modules are like the small version of library or a single Python file, as I said. So if I name some of the built-in modules, or like you can say math library, we have to perform mathematical stuff. We have operating system library, which we call as OS library. Then we have sys library, which is systems library. So these are few built-in modules name, okay? Modules means single page library, you can say. And libraries are the collection of modules. It means when you have written something that can perform more complex tasks or that can perform more specific tasks, then that is called as library. So module means single page Python file, and library means collection of modules that provides pre-written functionality to solve any specific task, you can say. Okay, so just like pandas and NumPy— pandas and NumPy are the libraries, math and all these things are the modules. Any question up till this, anyone? Now let's talk about how exactly we have to How exactly we have to import and install the library? To install any library in Python, we have pip install command. So when you wanted to import any library, it is NumPy, pandas, random, any library, then you have to write down pip install, pip install, and then you have to mention the library name. So if you wanted to use pandas, you will write pandas. If you wanted to use NumPy, you will write NumPy. And if you wanted to import more different, different libraries, then you keep on adding the code cells and write down pip install pandas, pip install NumPy, and so on. It depends upon the use case that how and what you exactly wanted to perform. This is how you can install it. But when it comes to import, because this library which you are going to install will be in your Python kernel, now to use it in this particular file, what you have to do is you have to write down import command. So you will say import and then you will mention the library name. If so, let's suppose the library name is NumPy, so you will say import NumPy. And now if you will run, this will import the NumPy. Let me just interrupt this. Let's restart the kernel. Okay, now let's import the NumPy. So when you will say import NumPy, this is going to import the NumPy library for you in your Python notebook that you have created. Now there are many popular libraries and they all are for different purposes. Like if you want to perform some numerical computation or related to numbers, you wanted to go with— like you may wanted to perform some mathematical stuff, or you wanted to create array, create matrix, creating the data structures, then at that time you have to use NumPy library. When you wanted to do data analysis, data manipulation,, then you have to use Pandas. If you wanted to perform data visualization, then we have the library called data— uh, Matplotlib for data visualization. When you wanted to go for like— when you wanted to create models, then we have the library called scikit-learn. scikit-learn, or you can also call it sklearn library. So using sklearn library, you can create any machine learning models. Linear regression model, or any other models if you wanted to create, then you have to learn the sklearn library. If you wanted to go for website development, you wanted to create websites, then there are two libraries which you should focus on. One is Flask, another one is Django. So these are few libraries that you should focus if you wanted to aim for that particular thing. So modules and libraries are within Python. You just have to install it, import it, and then you have to use the internal functionality of that particular library. So let's take an example of math library, okay? So if you wanted to use math library, you simply have to say import math. Now this is one way of importing. Now let's say whenever we want to use something from the library, then we have to use the method math. and then you will write what you want, which function you wanted to use. So suppose you want square root, so we have sqrt. Okay, if I wanted to use this, I have to write math.sqrt. sqrt is the function name, and then I will write down, let's say, 2. So now this will calculate the square root of 2. If I'm going to say 4, then it will calculate the square root of 4. So it means every time I have to repeat this word math. If you wanted to avoid this, then what you can do is you can provide a nickname to this library so that whenever you are going to call this library for function, basically for internal function, then you can use the nickname provided to that. So let's say I'm saying import math as m. So it means now I don't have to write a complete math. I can just simply say m.sqrt and then it will perform the calculation for me. Any question in this? Suppose you want to get— just to print the value which is the fixed value of pi, then you have to just say m.pi. So this will print the value of pi. I need not to write down it as a function because this is the constant value. So just write this. Now we are getting the value of pi in the output. So anything related to maths, if you wanted to perform, like you want the value of Euler number or you want the value of infinity, or like anything related to maths, then you can use this. Math library. Now there is one more cool library which is random library. Random library can help you work like if you wanted to generate random numbers or something related to this, then you have to use the random library. So same way, you have to write down the statement import random as, and then you can give it a nickname as well. Random library is basically used to generate random numbers. It can be integer numbers or it can be float numbers. You can also shuffle some sequences using random module. Yeah, am I audible to you now? Yes, ma'am. Okay, so yeah, I'm saying that random library is for generating random numbers. You can shuffle the sequence, you can also make random selection. So there are some text-based games which you can create using random module. So this random module can also help you create distribution like normal distribution if you want. If you want uniform distribution, then also you can use, right, random library for. So let me just show you the few use case of the random library. If you're saying import random, then random dot random will generate one random number for you. So you can see 0.77. This is every time if you are going to run this, it will generate one random number every time, but that number will be in between 1 to 0 to 1. This is random.random. Now let's say you wanted to generate one random integer number. So what you can do is you can use another function which is there in the random library. So let's say random.randint. So this int means you have— you wanted to generate an integer value. So you can also mention the range, that what should be the starting and the endpoint. So if let's say if I am saying 1, 10, it means this is going to generate one random number from 1 to 10. So every time if you will run this code cell, you will get new numbers generated. So now from 1 to 10, you can see every time it is generating one new random number for you. Then we also have random— like if you wanted to add steps, then also you can use random_range function. So random.randint function, and then you can mention starting point, end point, and step. In this you can also use steps. Step is optional, but if you wish you can add. So let's say if I'm saying 0, 10, then it will generate one random number out of these two values. Now let's say you wanted to generate random floating point number. Floating point number means random decimal numbers. If you wanted to generate, then we have random.uniform, random.uniform, and then you have to mention the range. So let's say I want the value from 1.5 to 2.5. So this is going to generate the random number within this particular range. Is this clear to you? Now let's talk about how we can shuffle anything or how we can print something out of a list. So let's say I have a list of colors is equal to, let's say, red, green, yellow, and I wanted to generate something from this. So random. Choice out of colors. Now this is going to print one random selection out of it. So you can see, every time if you are going to run this, it is going to generate one random choice out of this particular list. Now when you will be working as a data analyst or on the data analysis projects, then you will be required to create some random samples just to overview the data randomly so that you will have a good understanding of which type of a data you are going to use for making analysis. So at that time you have to use random.sample. For an example, let's say I have L1, which is list 1, is equal to 1, 3, 5, comma, just adding some random numbers. And then random.sample bracket L1. So L1 means it is the list 1 from which I want you to generate samples. Now, how many numbers do you want as a sample? You have to mention the number as well, that how many values you want in a sample. So let's say if I'm saying 3, yeah, so now you can see it will randomly generate a sample of 3 values. 3 items. Now, as we know that the list items are indexed, it means every item which we are storing in the list will be— will have index values. So when it comes to index values Then you— if you don't want to go with the index values, you just wanted to shuffle it, then at that time you can use the .shuffle method. So random.shuffle will help you. Get it shuffled. So I just need to add print statement so that will display. So now I will just print L1. So now you can see this was our original list. Now this is the updated one, which is the shuffled one. Any question in this? No, ma'am. Okay, so using libraries is pretty easy. You just have to get like what, which function do you want to use. Okay, so you can use random libraries for generating random events or actions in games, you can say. Or you can also create dice rolling activity like kind of game in this using Python. You can also create shuffling cards one. So there are multiple use cases in which you can use random module. Now if anyone have any question in this, can ask the question. So let's say if you wanted to use os library, then you can simply say import os, os.path. So os is a built-in library which can help you basically create, or you know, you can navigate to any file within your system. You can work on that file. This is similar to file handling also. So let's say I wanted to get the current directory path like in which I am working right now. So let's just use that first before going with the path one. So we are using os, so os.getcwd. So this is my current directory in which I am working. So whenever you wanted to use it as a command prompt, then you can use this os library. Now let's say you wanted to change the correct directory, like you don't want to work on this particular folder, you wanted to work on any other folder, then you can switch also to another folder. So the method is like you will write os. and then chdir. It means we wanted to switch from this library to, uh, this directory to another directory. If you wanted to list all the files or all the directories that you have in the specified path, then you can use listdir. So these are all the files which I have in this particular folder which I have opened on Visual Studio Code. Any question, anyone in this? So for today, your task is to revise all the modules and the libraries. All right.










