Classes in Python – A Quick guide with examples

classes in python

Python is a super simple and powerful programming language, you can easily adopt it and you can build something you love with python. So always the question was asked by my students and my youtube followers were why we learn python and what we do with python..?

Here is my detail answer for you, check the link

In this tutorial, you will know how to create classes in python. Creating classes in python is damn easy, you don’t need to take a heavy load on you. Believe me, you definitely love this tutorial.

Ok, enough! let’s get into the topic of the day.

Classes in python

Python is an object-oriented programming language, almost everything in python is an object. Let me answer a few FAQs.

Q1. What are Classes in python?

Ans: Class is a blueprint for the object, it has multiple methods and attributes.

Q2. What is the object in python?

Ans: Object is a key to access the class. we can access the methods and attributes of a class by creating objects.

Q3: How to define a class in python?

Ans: Like functions in python, we create classes in python by using the “Class” keyword. we will discuss other terms later.

Classes in python can help us to create different functionalities and attributes of a particular thing. we can access everything in a class by creating an object.

How to create classes in Python

Class: we use the class keyword for defining the class.

__init__: It is a method that is used for initializing the variables or attributes in a class. It is just like a constructor in other programming languages.

Self: Self is a keyword that refers to the attributes of the class. whenever we want to access class attributes in methods we use the self keyword.

methods: Methods are just like functions in python.

Example of creating the class in python

In this example, I am going to create a simple Car class, which has a brand, tiers and car details method.

Every car has its own brand and also has 4 tiers or wheels. These are common for our Car class.

Class variables: Class labels common for the entire class.

Instance variables: instance variables are used for different methods that are taken different users or objects.

__init__: Used for initializations of instance variables in a class.

self: It is a reference to variables of that particular class.

class Car():

    brand =' Jp' #class variables
    tiers = 4

    def __init__(self,color,engine,registration): #initializer
        self.color = color
        self.engine = engine   #Instace variables 
        self.registration = registration


    def car_details(self):
        print('our all cars have {} tiers.'.format(self.tiers))
        print('Car Brand:',self.brand)
        print('Car engine type:',self.engine)
        print('car registration:',self.registration)



c1 = Car('Yellow','Petrol','AP')

c1.car_details()

print()

c2 = Car('Green','desiel','North keria')
c2.car_details()

This is a simple Car class, I hope you will definitely get the topic clearly. If you want to get more ideas on python, please subscribe to our newsletter. Thank you for reading..!

Leave a Reply