@worldmappin: Congratulations, your post has been added to ! ๐ this post and your own profile on the map. Be part of the Worldmappin Community and join to get in touch with other travelers, ask questions or just be updated on our latest features.
#Learn #Python: Modules & Packages #Tutorial | #Beginner #Friendly by @techcoderlabz on 3Speak
โถ๏ธ Watch on 3Speak --- Learn Python Basics Together ๐ | Day 5 Welcome back to our beginner-friendly Python learning series! In today's video, we learn: โ Modules and Packages in Python โ Importing...
Tags: #python #learning #basics #video #tutorial #coding #students #cbse #icse #hivesuite
Hello, welcome back to my YouTube channel. So in today's video, we are going to cover more topics, the last two topics which are pending, which is modules and packages and input output. So output, I guess we've already covered in the first video how to print values. We will learn about a function which would help us understand how to take inputs from the user. But before we jump into input and output, let's get started with modules and packages. Of course, this page has several topics or several sections out of which we're not going to cover all of them. We'll cover some basics because, you know, this is beginner course. We will cover those in advanced course. So let's start with this. Over here, in the modules and packages, you can see that this is how you can define a function, and this is something which we will talk about very soon. So you can define a function and then you can import the file into the different file. So instead of writing functions for playing the game, how about we add functions which are related to, let's say, match basic operators, addition, subtraction, division, and multiplication. So that's what I've already done over here. I have added a file over here, which is match.py, and in that you can see very basic four functions over here, which is add. which would take two parameters and then it's going to return the addition of those two values, just like that subtraction of those two values, and then the multiplication of those two values. For you, the exercise would be returning the remainder, like, you know, over here, remainder. So you can call the frame function and return the remainder of the values which are supplied. Okay, so now that I've added this, let's go to the new file which I've created over here, which is completely empty. those functions which are already defined, let's say how do we use it in another one. So we are going to use that as a package. So that's how modules and package is. So we're going to call it a module for now, and we are going to import that module over here. So with this, you can see that we have imported the math over here. And I think I forgot to do one more thing over here. In this file, let me go ahead and do some extra stuff also, testing maths. add function. Over here I'm testing it, so percentage D, and in that I'm supplying two values over here, so for that I will write 10 and 20. So this is how basically we are doing testing of this file. So when I run this file, what will happen? It's going to print a statement, and in that it's going to execute the add function. So if I click and run this file, you can see that it is showing the addition of those two values which we have supplied over here. So yes, Addison function is working as expected. Okay, now I'm going over here, I've added the parsay, imported, the match over here. Now let's say I want to use the function, just like we have used it within that file. Now we are outside that file and we want to use that function. So from another file we are calling Add Function. Okay, over here, and then percentage D for the integer or a digit value, and then over here, in order to use the functions which are available in that, I'm going to say math, dot, add, oh, my bad, it is, okay, there's a type over here, the file name is math, so I'm going to use maths, and then over here, maths, and now you can see the functions which we've created over there, so I'm going to use add function, and in that I will supply, let's say, 30 and 40 value, so I'm expecting to have 70 as a result. So let's try running this file now. So when I run this file in terminal, you can see that, yes, we're getting the 70 value, but on top of that, it is also printing 30 value test function or test line that we've added over there is also getting executed. So even before it's getting imported over here, the piece of code that we have written, it's getting executed. So this is not something we want, right? So that's why, in the example, you can see over here that only if the main is defined, and there's some statements available in that, we will execute it. So this is the syntax, I will, I will explain, give me a minute. So I'm going to go ahead and copy this for now, and before that, let me put this print statement inside the defined. main define. So we are putting this inside the main function and now we are executing this. So with this, what we are trying to say over here is only if this file is executed, in that case we execute the main function. So right click if I now try to run this, so you can see that it is printing this one, but now if I run this one, then go and increase the size a bit over here and clear all of this. Now over here, if I try to run this, You can see that the test language was getting printed, it's no longer getting printed. So only if this file is getting executed, then the main function would be defined and available. But if we are importing it, the main is not over there, and then the main function wouldn't be executed. So that's the importance over here. So only if we are trying to execute. manually, directly accessing that file, the main would be executed, otherwise it will not be executed. So with this we have covered the first section. You can see that how to define a module, which we've already done over here, we have to find the match module, and we have imported it. Apart from that, you can see that there's a different way of, you know, importing objects meaning that we have four different functions and maybe tomorrow we may have 100 functions but out of which let's say we just want one function out of it over here in this file we are importing the whole maths module but we just want one function of it which is add function so what we can say is instead of import maths what we can say is from math import add when we do this the maths is no longer available but the add function is directly available so with this what we are stating is we only need add from maths. So right click, and if I run this, it's going to work now. So this is how we have imported only add and the other objects are not imported. Other functions are not imported. But let's say if you want to import all of them, we just say import star, which is this. So I'm going to say star over here, and with this, now you can see that all of the functions are available. So over here now I'm going to say subtraction over here, and over here I'm going to write 100. and then over here, yeah, of course I need to set this as subtraction, division, over here let's try division, and 50 by 25, and last one which we want to try is multiplication, so for that MOL, and in here, yeah, 50, 25 multiplication is also fine. Now if I try to run this, we would be able to get exact answer based on the maths module, which we've imported, all of those functions are available. So earlier, when we simply write import maths, then we have to explicitly specify that match dot add, match dot, meaning that the function from the match module, the function, which is subtraction function from the match module. But if I write this, that... import everything from match so those functions would be directly accessible, those objects would be directly accessible, I no longer have to. But sometimes, what if, you know, I have another add function over here, what will happen in that case? Okay, so it's going to consider the local one and not the one which is over there, or how about the exercise? You go ahead and give it a try and see what happens, okay? Because this is important. trying it out and figuring it out on your own is one of the thing that you should do and with that you would be able to learn and remember okay so go ahead and give this a try what will happen when you do this okay are you getting compilation error are you getting errors over here is it saying something else you tell me in the comment section okay now because now we have the naming conflict over here in order to avoid this like you know there is another way which is custom import name so I can say import Maths module. So over here, import Maths as I can give it custom name in order to avoid the name conflict, which we did. Like, you know, when I started recording the video, by mistake, we did Math, and it says, you know, a little bit confusing. So over here, I'm going to import it as My Max Functions. Okay, so with this, now I basically have given a custom name over here, and over here, whenever I'm reading. I get clarity, yes, these are my math functions, not the math, which is like default, module or a package which is available. So with this, now you can see, we have updated the code. So now it is more readable and understandable that, yes, this is the package, holding my functions, defined functions which are, sorry, the functions which are defined by me. All right, so this is it. With this, we have covered... modules and packages, yes, there are more, but we're not going to cover them, we can cover them later in advance section. So the other thing that I wanted to cover in this video was about the input. Now in the input, it's straightforward, we use input function, and with that we get the input, and we can also type cast it into integer or into float. So this is something what we can do very easily. For example, let's say A equals integer and then I'm gonna say input over here B equals integer and then input over here and I will supply A and B over here. Alright, so that's it for this video, see you in the next one, bye bye
@worldmappin: Congratulations, your post has been added to ! ๐ this post and your own profile on the map. Be part of the Worldmappin Community and join to get in touch with other travelers, ask questions or just be updated on our latest features.