Skip to main content

Python Class and Object

In this tutorial you will learn:

  • Classes and Objects
  • Class in Python
  • Object in Python
  • Alter an Object

Classes and Objects

Object Oriented Programming Languages works with two main features Class and Object. In this programming paradigm we create containers that contains different properties and methods. These methods are used to modify the properties in some way and we name these containers as Classes. Objects are instances of a Class and they have access to the properties and methods of that class.

Class in Python

Python is also an Object Oriented Programming Language. We can create different containers with the keyword “class” and add properties and methods to it.


Creating a class in Python is simple. We use “class” keyword followed by the name of the class and then we write its properties and methods below.

  1. class Vehicle:
  2. car = "Tesla"
  3. truck = "Cyber Truck"
  4. bike = "Cyber Bike"

In Object Oriented Languages whenever we create a class a function is called to initialize the class and that function is known as a constructor. We can initialize the class with some default values. In python constructor is defined by the keyword “init”. Let’s use that function in our vehicle example.

  1. class Vehicle:
  2. def __init__(self, model,vehicleType, color):
  3. self.model = model
  4. self.vehicleType = vehicleType
  5. self.color = color

Object in Python

As already mentioned objects are instances of a Class and they have access to those properties and methods. In order to create objects in Python we can have a look at the example below.


  1. class Vehicle:
  2. def __init__(self, model, vehicleType, color):
  3. self.model = model
  4. self.vehicleType = vehicleType
  5. self.color = color
  6. myVehicle = Vehicle(2020,"Truck", "Red")
  7. print("** Vehicle Details **")
  8. print("Model", myVehicle.model)
  9. print("Vehicle Type", myVehicle.vehicleType)
  10. print("Color", myVehicle.color)

Adding a vehicle details function in the class to print the vehicle details.

  1. print("\n\nRewriting Vehicle class with __init__ constructor")
  2. class Vehicle:
  3. def __init__(self, model,vehicleType, color):
  4. self.model = model
  5. self.vehicleType = vehicleType
  6. self.color = color
  7. def vehicleDetails(self):
  8. print("\n\n-- Vehicle Details using behicle details function --")
  9. print("Model", self.model)
  10. print("Vehicle Type", self.vehicleType)
  11. print("Color", self.color)
  12. mySecondVehicle = Vehicle(2010, "Car", "Blue")
  13. mySecondVehicle.vehicleDetails()

Alter an Object

We can alter an object in different ways. We can delete an object and set values to its properties. Deleting an Object is very simple. We just need to use the del keyword followed by the variable name.

del myVehicle

In order to alter the properties we can simply do that by using by calling the class property with the object and assign the updated value

myVehicle.color = “Green”

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.


1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.