How to Create a Python Class

Abrar Hussain
A free video tutorial from Abrar Hussain
Programmer, Student
3.9 instructor rating • 1 course • 28,205 students

Lecture description

We're going to get started with object-oriented programming by covering how to create classes in Python. The attached document resource includes some of the main terminology that is involved for this section.

Learn more from the full course

Learn Python: Python for Beginners

Python introduction for beginners. Learn complete Python from scratch!

01:15:32 of on-demand video • Updated November 2018

  • Create fully functional Python programs
  • Understand user input
  • Learn about loop structures and conditionals
  • Correctly execute operations in Python
  • Work with Python file handling
  • Create and modify data structures in Python
  • Manipulate strings and data
English [Auto] Hey guys and what can lecture. This is the first lecture we're all going to cover how classes and a bit of object oriented programming works in Python. Python has been an object oriented language since it existed here. We're not going to work extensively on the details of object oriented programming so I've included a separate file of intellect resources with some terminology to bring up to speed. Continuing from here I'm going to assume you've read over or know the material of the attached resource document. So let's get on to covering how to create a class. The class is going to be a prototype from which you create separate object instances a class keyword is used to create a new class definition here will type employee colon. Normally the classes that a class inherits from would be included inside of parentheses beside the name like so we just want our class inherit from the default object class so we won't have that all right to continue with our Employee class. We first want a constructor Python use the special method in net with two underscores on each side we pass in the parameter self and then other parameters which would be important for an employee. Our employee is going to have a name and a salary so let's add that in. Now we want to make the name and salary as instance variables. So let's use self-terminate equals name and self-taught salary equal salary. We use self here so that we know we're talking about a specific instance of the class meaning a single employee. All right now that we have that set up let's create a method that displays the employees information as I see method here and not function because the code is going to be associated with an object which in our case is an instance of employee. All right so have def display employee with self as a parameter. Then we want to print name and salary using self-taught name and self-taught salary as the associated values were here. I've used the percent operator for string formatting where each of the present values get replaced by the expected value in the tuple here. All right so let's create two more methods one of them will let us give our employees a raise and the other will print the total number of employees. So we have def give raise which takes self and Cent as parameters we want to change self-taught salaried plus equal self-taught salaried times per cent and that's if that method. But how exactly would we count the number of employees. This is part of the class definition so we can't just read over a list which includes different employee instances. Well we know that the count for the number of employees gets increased by 1 each time we instantiate an employee object. But this information has to be independent of any instances of the class. Here's where we can use a class variable outside of the methods we define. Count equals zero. We know that creating a new instance should increase the employee count. So instead of net we have employee dight and count plus equals 1. You can note that I used employee instead of self. So the code refers to something that is part of the class and not an instance of the class because its value is part of the class. It stays consistent among all of the classes instances OK. So now that we're counting employees correctly we can create def display count with self and print the number of employees. For the string formatting the D refers to a decimal while the s from the other method string formatting refers to a string. All right with that method our entire class is done. Let's review what we've learned. We learned how we can create our class using the class statement we learned about the net method as the constructor as well as how to instance and class variables using methods we learned about how to get information about instance of a class and we learned about how we can change that information. All right that's it for this lecture and I'll see you guys next time.