[MUSIC] Hi everyone. In this video clip, let me explain the concept of object oriented programming. You are making software program by assembling objects that you created. It's like a constructing or making a car. Car is simply a assembled object of many parts. So after making many parts you assemble in order to make up car. Similarly, you make many parts of the program. Software program using class, concept and then assemble them into a whole software. But building block of a software program is object. Then how can we create object? We use class. So our class is an extensible program called template for creating object. You can make many objects using class and then assemble them in order to make a software. So class is compared to a blueprint for making objects or tools or parts all your software. Because class is a blue plant, it means that you can create many objects from the same class. Sometimes if you want to modify an existing class, you inherit that class and then modify in order to accomplish your specific needs. Yeah, in python, all classes inherit on existing class or multiple classes. I'm introducing a case here, only inheriting one class. In case of inheriting multiple classes, you may encounter other problems. So it is beyond this Kyocera content. I'm only introducing a class inheriting one class. If you make your first class without inheriting any class, the class inherit object class of python, it means that object class of python is a starting point. So if you do not inherit any class, existing class, then automatically or implicitly your first class is inheriting the object class of python. Our class is composed of two parts. The first part is, state and the other part is functions. It means that it determines behavior, think about the concept of the state and functions or behavior. Here's a automobile, you have a car. A car has its own features or attributes like color, size or the number of views or the size of the car kinds of fuel. Those are features of your car. Then there should be a valuable describing that attribute that describers, the state over your car. The radio car also a variable representing state over your car. So there's all variables that describers states of an object. Sometimes it is also called attributes. So attributes are similarly used as our variables. So variable are used for describing the state of an object. If it is a tool or object, he should perform a certain function. Right? So there's also another characteristic which is functions. So class include functions, here functions. The concept of functions is what we already studied. Functions. The same functions are called method in class. So, method or functions determines the behaviors of an object or the functions of the object or the rules of the object. So all class has two components, state, variables and methods. When an object is created by a constructor of the class, the resulting object is called instance of the class. So you can make an object based on the class, then object created based on that class is called instance. Now, let me show you. A case making class, when you make a class because it becomes a tool of your software, it means that you are creating script file, because once you make a blueprint, whenever you need it, you call it in order to make a specific real object. So, I am going to show you how to make script file. In order to understand the script file, content of the script file, you better to put the first line the name of script of fire and another duck string or very simple class exercise for four arithmetic calculations. So I'm going to make for calculation dot pie script of file. That script file will contain class for calculation. Now. In order to make a class, you need to use keyword class here and then name of class follows. It is a convention, recommendation when you make name of class, in this case, the name of class is composed of two words, then use capital letter for the first character. Each word. So for has a capital letter and calculation has another capital letter. It is a convention, if you don't like the convention, you don't need to follow it. But better to follow the convention if you want to share your class with others and class and with college here. So this line is a class headline. Class headline immediately following the headline. Here's a dark string that these planes, the class or class for four arithmetic calculations. I'm writing down here that dark stream in order to check later when I forgot that class, I can check the doc string in order to understand the rules of that class. Every class has a functions. The first function is double on the bar, in it, double on the bar function. It is a special function. But it initializes is there are state variables. So reading it is a function. That's why there's a parenthesis after the name of that function, then self always put self and first and second, it means that this function takes two variables. But on top of the two vigorous us inserting self, it means that yourself is itself of that object. So I have to following the dark stream, here's all initializing variable. Self that first, self that second safe that first you see all is taking first available and self to second is taking second variable. So here self that first self the second is a kind of two variables which is taking values from first and second. Either be provided by huger right, then another survey that first serve that second to state very well is created. Why we are creating this one because internally, within class we are doing calculations based on with this internal variable self that first self that first set of that first answer that second. Here is the exception handling case because we are going to perform for arithmetic characters in addition, subtraction, multiplication and division. In case of division, if second number is zero, then the calculation cannot be done. So here's a exception handling statement. If second use equals zero, then raise the value. The number must not be zero. Right, now those are functions. So far we initialize the state variable and then with this David available, we are performing a specific function of the class. We are defining the functions of the class. So DEF add and self. It means that self here means that the object itself and then your stock stream because it is also function and return addition. You are adding to internal variable self that first and self the second. And multiplication, subtraction and division. All functions are similar, using internal variables to perform specific functions. And then at the end here percent magic, percent magic. Which is not introduced in this course. But it is a kind of convenient tool for making script five. At this time I made this command line dormant. Not working by adding hash in front of the command line. I'm going to make active when I make script file. So first I introduced all the details of this class and then let's execute in order to create class named class object named for calculation. So control enter. Then here, number one appears means that in this working memory for care creation or class object is created. Now we are constructing on object A and also it takes two values X the first is 10. Second value is three. Then object A is specific realization of blueprint. So at this time for care creation, it's okay. The class name becomes construct with specifications, then A object is created and then printing the first number. Here's method is used a dot, first a second, then two numbers will be returned and we are calling a dot add also all functions reading class are used as method then calculations are done. This is the outcome first values, 10 second value, three edition, 13. Multiplication, 30, subtraction, seven and division. And surely you can put a object reading parenthesis. Because I said it is self in class so but how can you use multiple location function of the object here? You need to use for creation class named that because it is a function reading for calculation class. So we enter this one then, sorry, multiplication. The same story is printed now what if we change three into two then zero obviously what happens here? The number must not be zero. Exception handling is activated and so we need to change again and then execute. Then no more messages. All right, no, we created object class within memory space, but in order to re use, we need to create a script file containing this. Class information. So how can we make class information or class object? Simply activate the last line. %save means that save the following file fourcalculation.py. So, %save command will create fourcalculation.py file containing the information reading this cell within this cell will be saved in this py file. You can specify lines but actually even though you specify lines, actually all the script within this cell will be saved in fourcalculation.py, then you may ask how to ignore the lines? It's easy. Go to view and click clip, show line numbers, then now you see the line numbers. Each cell has its own line numbers. So currently, a script start from line 1, it ends line 29, that's why this range is set but you may omit it. Now, how can it make a script file execute this cell. But before execute this cell, go to Kernel, go to Kernel and run, Restart Kernel and clear all our purse. First, before creating py file, clear the memory space and then extract, why? Because without clearing memory space if you make py file, all the stuff in memory space will be dumped in this py file. In that case it is different from the content reading the cell, that's why after clearing, run this cell, then bottom you can check how did outcome sobel executing the file. So, the following commands were written to file fourcalculation.py, right? And those content is printed in the file, but this line is also printed. Now let's see how does it look like here. Now we see fourcalculation.py file which has never existed here. Now you can open it by double clicking the file, then fourcalculation.py file is opened here, right? So once you make script file, you can directly change that file here too. So, if there's something you want to change, you can change it here. So automatically, coding information is included here. The coding included is based on utf-8 and here's the script that we made. And then, but this one creates script file, so we don't need any more in this py file, so we need to get rid of it, delete-, And save. How do we save this script file? Ctrl Save, Ctrl Save then the file is saved. Now fourcaculation py object is created as a file, script file. Now, we can go back to the original py python file. And then we make the command line dormant because we are not repeating making the same object. Now, because already fourcalculation class is existing in memory because I ran the first cell. So, but now because we created py file we need to import that file in order to reuse and using that blueprint, we need to make another object. So in order to remove confusion, let's clear the memory space. Now in this memory space there's nothing, there's no more fourcalculation class object. Now let's look at this way, From fourcalculation import fourcalculation. So from fourcalculation this is this script file n, right? From the script file, we are importing class fourcalculation. And then constructing object b and we are assigning different parameter at this 6 and 2 and the remaining is the same as before. So we execute this one, wow. In memory space there is no fourcalculation but at the importing fourcalculation object from the py file script file fourcalculation, we created another object called b and we- Printed the outcomes of fourcalculation here, all things done nicely. Another way you'll be importing fourcalculation class, how can we do that here? Import fourcalculation as fc, at this time we are using nickname of fourcalculation. So, this is another way of importing class object. In the previous case, we are importing specific class out of fourcalculation py file, script file. What if there were multiple classes included in fourcalculation py file? >> In that case, if you do this one, we are importing only one class fourcalculation provided in that case we used this importing method-. Then we are importing the whole content to read in fourcalculation simply we are calling that imported content as fc. Then, how can a create an object? Because fc is object name and then this is class name belonging to that fc script file and this one is constructing object c, it takes two different numbers 12 and 3. And then we execute, then the fourcalculations are done. Surely, we so far we created three objects a, b, c and we create another list object, object list and use it follow, we can extract the outcomes. At this time, we are only taking multiplication function. So each object is one by one called by follow and each one is assigned to here and then multiplicated answer is printed here. But what is the problem of this one? a is not defined. So we need to go back and at this time we need to execute this one first and a object is also done and then we run. Then we have this three multiplicated values are printed. Now before closing this video clip, let me give you a review question through forests. The object a, b and c are instances of the class FourCalculation, yes or no? Yes, because those objects are constructed by the class FourCalculation, some of them created by FourCalculation imported from fourcalculation py file.