Quick Introduction Python Classes

This page only provides quick introduction to Python classes.  By definition, a python classes are blueprints for creating objects. Objects are instances of a class that encapsulate data (attributes) and behavior (methods).   

In the example below, you enter three values (car make,  model, and year), they get transferred into a class called "Car", then a string multiplication function in the class is called to performs an action.


class Car:

    def __init__(self, make, model, year):

        self.make = make

        self.model = model

        self.year = year


    def accelerate(self):

        print("The",self.year,self.make,self.model,"is accelerating!")


if __name__ == '__main__':


    carMake  = input("Enter car make: ")

    carModel = input("Enter car model: ")

    carYear  = input("Enter car year: ")

    my_car = Car(carMake, carModel, carYear)

    my_car.accelerate()


For example, when you run the code, you will get the following results for the input:


$ python3 example1.py

Enter car make: Chevrolet

Enter car model: Camaro

Enter car year: 2025

The 2030 Chevrolet Camaro is accelerating!


Class in Python are very powerful, this demo is only meant to provide quick and simple introduction to concept of Python classes so that you can appreciate what they can do for you.

Looking to expand your Python knowledge? I recommend checking out the book "Mastering Learning Python 3: In Five Minutes." This resource offers a beginner-friendly approach with short, focused lessons that can help you grasp the fundamentals of Python 3 programming. (*** Now available on Amazon ***)