 Hello everyone. Welcome to Python tutorials. Myself, Samprit Patel, assistant professor, department of computer science and engineering, Vaalchand Institute of Technology, Sholapur. In today's video, we are going to learn about constructors in Python. At the end of this video, you'll be able to write a Python script in object-oriented programming style. So before we start with hands-on, let's just see what are constructors. So a constructor is a special type of method or a function which is used to initialize the instance member of the class. So as in C++ or Java, the constructor has the same name as its class, but it treats the constructor differently in Python. In Python, it is used to create an object. So we have basically two types of constructors. First is parameterized constructor and second is non-parameterized constructor. We'll go through these in hands-on with some examples. Constructor definition is executed when we create the object of the class. Constructor also verifies that there are enough resources for the object to perform any startup task. So let's see how to create a constructor. So in Python, the method underscore underscore init underscore underscore simulates the constructor of a class. So this method is called when the class is instantiated, that is when the object of the class is created. It accepts the self keyword as a first argument, which allows accessing the attributes or methods of the class. So we can pass any number of arguments at the time of creating the class object depending on the constructor definition. So it is mostly used to initialize the class attributes. Every class must have a constructor even if it simply relies on the default constructor. So let's see an example. So let's see an example of creating a constructor. So we'll have a class employee, employee, okay, which will have a constructor. So see, this is how we define a constructor. Like a function, we'll have a keyword def followed by underscore underscore init underscore underscore. And it will have a self parameter as the first argument of the constructor. Now here we need two other arguments that is name of the employee and ID. Okay, so here we'll access the name and ID as self dot name is equals to name and self dot ID is equals to ID. Okay, so this will pair or match the arguments of the constructor. Now we have a function to display the name and ID of your employee. So here we'll print, we'll print say ID is %d because it is a number and name of the employee, name of the employee will be %s, okay, %s. So here self dot ID, which will be given to the %d and self dot name, self dot name will be given to %s. Okay, so this will help us to display. Now we'll create an object of a class that is say EMP1 is equals to employee and we'll pass the arguments for name and ID. So the first argument is say ABC is the name of the employee and ID will be 101. Similarly, we'll create one more object EMP2 is equals to employee, employee where the name of the employee here will be PQR and the ID will be 102. Okay, so let's see. Now once you have assigned the values we need to display, we need to display. So using the same objects EMP1.display to display the details of first employee and EMP2.display to display the details of second employee. So yes, this is the first employee ID is 101 and name is ABC and this is the second employee ID is 102 and name is PQR. So this is how we have created a constructor using underscore underscore init underscore underscore. So here the constructor you don't need to specifically call the constructor with the object. So when you create an object, the constructor is called automatically. So let's see another program which will tell you how the constructor is called. So we'll just see create account and create number of objects which will tell us the increment of the count as we create the object without calling the constructor. Okay, so say class student, okay class student and we'll take account is equals to zero. Now we'll create a constructor underscore underscore init underscore underscore. Okay, it will have a self parameter and this will increment the count of the constructor student dot count is equals to student dot count is equals to student dot count plus one. Okay, so yes. Now we'll create an object of class student S1 is equals to student. So this is the first object S2 is equals to student the second object and S3 is equals to student. This is the third object. Okay, so now we are not calling giving a call to a constructor separately. We'll just print, we'll just print the number, the number of students. Okay, and we'll pass student dot count. Okay, so let's run this and see. So the student number of student is three. Now see, whenever we have created an object, it automatically calls the constructor and execute student dot count is equals to student plus one. Okay, I hope this is clear. So, primarily what the example we saw was an example for creating a constructor. Second was to see how the constructor is called automatically. Now, let's see non parameterized constructor. Okay, so let's see an example for the same. So we'll have the example student and here in construct in a constructor, what we'll do is we'll just print, we'll just print say this is double code. So this is non parameterized constructor. Okay, this is non parameterized constructor. And so see once you have created this constructor, we don't have to pass any parameters to this will just create an object. I'll also delete this code and I'll execute it. So see, this is non parameterized constructor. Okay, let's see the example of parameterized constructor with the same class. So this will print as this is a parameterized constructor. This is a parameterized constructor. But the constructor will have two parameters, one will be the self and second will be name. Okay, and after printing, we'll just get self dot name is equals to name self dot name is equals to name. And now what we'll do is we'll define another function show, which will print, which will print hello, and it will have self dot name, it will have self dot name. Now when we create an object, when we create an object, as it is a parameterized constructor, we need to give a constructor say will give the name john, okay, and we'll call the function show. So s one dot. Okay. So hello, john. Okay, hello, john is printed with the example. Okay, so let's try to answer this question. Can we pass different arguments to a constructor apart from self? Yes, we can pass the different arguments, given that the self should be the first parameter. Okay, self should be the first parameter of the constructor. Okay. So these are some references I've created to create this video programming in Python three and core Python programming by PSN publication and dream tech press respectively. Thank you.