Classes Examples with Python

In Python, a class is an outline that describes the data stored in an object and defines the operations that can be performed on the object. In this article, I’ll show you some examples of classes with Python.

What are Classes in Python?

Python classes

Python, like all object-oriented programming languages, allows programmers to define their classes. A class definition is an instruction composed of a header and a body. The class header contains the class keyword followed by an identifier used to name the class.

Also, Read – 100+ Machine Learning Projects Solved and Explained.

The body of the class definition contains one or more method definitions, all of which must be indented to the same level of indentation. Suppose we want to define a class to represent a point or coordinate in the two-dimensional Cartesian coordinate system.

The objects will need to store two values, one for the x coordinate and one for the y coordinate. We will also have to decide what operations we want to be able to perform on the objects created from the new class.

Classes Examples with Python

In this section, I will take you through 4 examples of classes with Python:

class car(object):
	def __init__(self,name,model,year,condition,kilometers):
		self.name=name
		self.model=model
		self.year=year
		self.condition=condition
		self.kilometers=kilometers
	def display(self,showAll):
		if showAll:
			print("This car is a",self.name,self.model,"from",self.year,"it is",self.condition,"and has",self.kilometers, "KM highest speed")	
		else:
			print("This car is a",self.name,self.model,"from",self.year)
whip=car("BMW","M5",2019,"New",300)
whip.display(True)
This car is a BMW M5 from 2019 it is New and has 300 KM highest speed.
class Employees:
	def __init__(self,emp_id,emp_name,emp_salary):
		self.emp_id=emp_id
		self.emp_name=emp_name
		self.emp_salary=emp_salary
	def __repr__(self):
		return repr((self.emp_id,self.emp_name,self.emp_salary))
emp1=Employees("Emp101","AMAN",100000)
print(emp1)
emp2=Employees("Emp102","ASHISH",58000)
print(emp2)
emp3=Employees("Emp103","ANKIT",34000)
print(emp3)
emp4=Employees("Emp104","AKASH",90000)
print(Employees)

(‘Emp101’, ‘AMAN’, 100000)
(‘Emp102’, ‘ASHISH’, 58000)
(‘Emp103’, ‘ANKIT’, 34000)

class father:
	def __init__(self):
		super().__init__()
		self.f_age=39

class son:
	def __init__(self):
		super().__init__()
		self.s_age=5
class family(father,son):
	def __init__(self):
		super().__init__()
f=family()
print(f.f_age,f.s_age)
39 5
class Person:   #start a class
    def __init__(self, name, job=None, pay=0):    # Constructor takes three arguments
        self.name = name    # Fill out fields when created
        self.job = job     # self is the new instance object
        self.pay = pay
    def lastname(self):
        return self.name.split()[-1]
    def giveraise(self, percent):
        self.pay = int(self.pay * (1 + percent))
    def __repr__(self):    # Added method
        return '[Person:%s, %s]'% (self.name, self.pay)    #String to print

class Manager(Person):
    def giveraise(self, percent, bonus=.10):
        Person.giveraise(self, percent + bonus)

if __name__ == '__main__':
    john = Person("John Cena")
    aman = Person("Aman Kharwal", job='Programmer', pay=500000)
    print(aman)
    print(john)
    print(john.lastname(), aman.lastname())
    aman.giveraise(.10)
    print(aman)
    alex = Manager("Alex Hales", "Manager", 40000)
    alex.giveraise(.10)
    print(alex.lastname())
    print(alex)
[Person:Aman Kharwal, 500000]
Cena Kharwal
[Person:Aman Kharwal, 550000]
Hales
[Person:Alex Hales, 48000]

I hope you liked this article on Classes with Python. Feel free to ask your valuable questions in the comments section below.

Aman Kharwal
Aman Kharwal

I'm a writer and data scientist on a mission to educate others about the incredible power of data📈.

Articles: 1498

Leave a Reply