Python Tutorial for Beginners 23 - Classes and Objects in Python (OOP)

 


hey guys welcome to the next blog on python tutorial for beginners in this blog I'm going to show you how you can create your first class in Python now if you haven't seen my last blog in which I have discussed what is object-oriented programming and what are classes I will highly recommend you to watch that blog because this will be a continuation of that blog so let's create a class in Python so for that I will create a new project so let's create a new project in PyCharm so just click on file here and then click on new project and then the project name I'm going to give here is oops and then I will just click on create and I will choose this option which says open in current window and also this option which is added to currently open the project which is going to create this project in this window itself which is already open so now you can see we have our empty project here and inside this project let's create a Python file so right click on the project and then new and then we are going to create a new file and let's create a class called car so I'm going to name the file name as car because we are going to create the car class now in order to create a class in Python you use a keyword class and then you give the name to your class so the class name in our case is car and then you give the schoolin and for example I write here a keyword called pass now when you write this keyword after the Declaration of a class this means that it is a empty class you can also use the space keyword to create an empty method so this keyword pass is used to create an empty class or an empty method now here after the class declaration let's see how we can create an instance of a class so it's really easy to create an instance of a class so let's create a first object and I'm going to name it as Ford which is a car brand and then you use your car class name and use these parentheses here okay so this here Ford is an object or you can also say it's an instance of that last car in a similar way we can create multiple objects for example Honda is another car brand and you can create the Honda object using this class car once again you can create a new object for example Audie and then you can once again use this car class to create this object so what we have done till now we have created three object from that last car now in the previous blog I told you that you can associate some data with your object so let's associate some data so for example Ford we can associate attribute called speed right so we can assign the speed for example 200 here for a Honda we can assign speed let's say 220 and for Adi we can assign the speed let's say 250 so speed here is called the attribute and whenever you create an empty class using this pass keyword you can add these attributes on the fly so you can see we have added these attribute after the Declaration of the class and after the creation of the objects let's add some more attribute to these instances or the object so a car can have the color so I'm going to just write color and the Ford has let's say the my color and let me just copy and paste here and let's say the Honda have the blue color and we have the ADI of black color so once again color is an attribute here if you want to print these attribute you can use this print method and then for example we want to print the speed of Ford and the color of Ford then we can do it like this and let's run the code so in order to run this code first of all when you create a new project and a new python file you need to right click on this file and then click on run the file name whatever is your file name my file name was car so you can see now the result is printed here first is the speed and second is the color of an instance Ford similarly you can print the color and speed of Honda and Audi objects also now if you want to change some attribute it's also possible so let's say I want to change the speed of Ford objects so I'm going to use Ford dot speed once again and this time I want to assign 300 speed to this attribute let's say we also want to change the color of the food objects so I'm going to just use 4 dot color and this time I want to use the color blue here and let's once again we will try to print the color and speed of the object Ford and now you can see this result so before the speed was 200 and the new speed is 300 for the object Ford and before the color was red and the new color is blue for this object Ford now if you have followed the last blog this speed and color is the data so speed and color are the variables which holds some data inside them but still we haven't added any behavior or methods to our class car and that we will do in the next blog now let's create a new class because understanding classes in any language is difficult so let me give you another example so this time I'm going to create a new Python class and I'm going to name this python class as a rectangle and then press ok which is going to create this class and to create a class you already know you use the keyword and the name of the class and then the colon and we will also make this class an empty class using this path keyboard and in order to create an instance I'm going to just write correct one which is the first object using the rectangle class and then I'm going to create rect 2 which is the second object using this rectangle class and now what are the attributes which we can relate to the rectangle a rectangle have a width and the height so let's add the here but and height so let me use the first object and let's say height is 24 the rectangle 1 and then we are going to use the second object and once again we are going to add the height for the second rectangle let's say the height of second rectangle is 30 similarly we can add the width to the rectangle so width is equal to let's say it's 40 and correct to object have the width let's say 10 so what we have done here we have created a rectangle class and then we have created two instances of the rectangle class correct one and rag2 which are also called objects and then we have added some attributes to our objects which are height and with now let's say you want to calculate the area of these two rectangles I can use the print method and then what is the area of the rectangle it's the multiplication of the height and the width right so I can use this rectangle one object and then I can call the height and width of this object and this will give me the area of the rectangle one using this multiplication operator similarly I will do the same for the object to here and let's run the code and if you remember what we need to do if we create a new Python file we need to right-click on the file and then click on run right so once the file is running you can choose from here which file you want to run but at least once you need to right-click on the file and just click on run whatever filename you have so you can see the area of rectangle one is 800 and the area of rectangle two is 300 so this is how you can create a very simple class in Python in the next blog I'm going to show you how to add behaviors to your class that means how to add methods to your class and how to use this method to manipulate this data which we have provided here in the form of attributes so stay tuned and please keep reading these blog's and I will see you in the next blog 

Post a Comment

Previous Post Next Post

Recent in Technology News