Python Class- Tutorial


In this article, we give a tutorial on classes in Python.

We go over things, such as how to create a class, how to create an instance of class, how to give attributes to a class, and so on.

How to Create a Class

So, the first thing we go over is how to create a class in Python.

This is done using by using the keyword class followed by the name of the class and a colon.

The following code below creates the class called animals.

Classes can also be created using parentheses after the name of the class.

The code below creates the class animals, just as above.

Both lines of code do the same exact thing.

Next, we'll explaining defining attributes in classes.

Defining Attributes in Classes

So, once you've created a class, you want to specify attributes for the class such as the type of animal, the color of animals, etc.

We create and define the type and color attributes for the class animals.

So, all you have to do to create and define attributes of classes is to specify the attribute and set it equal to a value, just like when creating a variable and initializing it.

So, the animals class has a type attribute of "land" and a color attribute of "green".

Thus, if we create an instance of a class, they will all inherit these attributes.

How to Create an Instance of a Class

We now go over how to create an instance of a class in Python.

This is done after you first create the class.

Then to create an instance, or object, of a class, you create the instance name and set it equal to the class name with parentheses.

In our code, we create an instance of the animals class and name the instance, animal1.

Now an instance of the animals class has been created called animal1.

If you specified animals1 in the command terminal, you would get this result.

You can see how it says that animal1 is an object.

If you were to type in animals in the command window, you would get the following result, shown below.

So, you can use that as a debugging or verification tool in programming.

Because animal1 is an instance of the animals class, it inherits all atttributes from the animals class.

In the animals class, we defined 2 attributes, type and color.

The type was "land" and the color was "green".

Thus, if you ran the following code, you would get the following.

Thus, you can see that the animal1 object has inherited all of the attributes of the animals class, which is one of the chief principles of classes in a programming language. Any object of a class inherits all of the attributes of animals class.

Of course, if you want, you can override the value of attribute of an object that differentiates it from the value assigned in the class.

For example, if one of the animals, say animal10, were orange, the following code would make the color attribute orange.

So, this is the basics of classes in Python.

Of course, there is a lot more but this is just an introduction to classes in Python.

Related Resources

How to Randomly Select From or Shuffle a List in Python

HTML Comment Box is loading comments...