10 Python Classes Examples with source code & output

Here we discuss almost 10 python classes examples with source code and with output. If you want to learn completely must visit the python tutorial page and if you want to prepare for a job interview here you get 200+ Python Interview Questions And Answers.

10 best Python Classes Examples

1. A basic “Person” class with name and age properties:

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

person = Person("Pramod Kumar Yadav", 27)
print(person.name) # Output: "Pramod Kumar Yadav"
print(person.age) # Output: 27

2. A “Dog” class with the breed and name properties, and a “bark” method:

class Dog:
    def __init__(self, breed, name):
        self.breed = breed
        self.name = name

    def bark(self):
        print("Woof woof!")

dog = Dog("Golden Retriever", "Buddy")
print(dog.breed) # Output: "Golden Retriever"
print(dog.name) # Output: "Buddy"
dog.bark() # Output: "Woof woof!"

3. A “Calculator” class with add, subtract, multiply, and divide methods:

class Calculator:
    def add(self, x, y):
        return x + y

    def subtract(self, x, y):
        return x - y

    def multiply(self, x, y):
        return x * y

    def divide(self, x, y):
        return x / y

calculator = Calculator()
print(calculator.add(2, 3)) # Output: 5
print(calculator.subtract(5, 2)) # Output: 3
print(calculator.multiply(4, 5)) # Output: 20
print(calculator.divide(8, 2)) # Output: 4.0

4. A “Car” class with make, model, and year properties, and a “drive” method:

class Car:
    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        self.year = year

    def drive(self):
        print(f"The {self.make} {self.model} is driving.")

car = Car("Toyota", "Camry", 2020)
print(car.make) # Output: "Toyota"
print(car.model) # Output: "Camry"
print(car.year) # Output: 2020
car.drive() # Output: "The Toyota Camry is driving."

5. A “Book” class with title, author, and pages properties:

class Book:
    def __init__(self, title, author, pages):
        self.title = title
        self.author = author
        self.pages = pages

book = Book("To Kill a Mockingbird", "Harper Lee", 281)
print(book.title) # Output: "To Kill a Mockingbird"
print(book.author) # Output: "Harper Lee"
print(book.pages) # Output: 281

6. A “Student” class with name, age, and grade properties, and a “study” method:

class Student:
    def __init__(self, name, age, grade):
        self.name = name
        self.age = age
        self.grade = grade

    def study(self):
        print(f"{self.name} is studying for their {self.grade}.")

student = Student("Pramod Kumar Yadav", 27, B-Tech)
print(student.name) # Output: "Jane Smith"
print(student.age) # Output: 27
print(student.grade) # Output: CSE
student.study() # Output: "Pramod Kumar Yadav is studying for their  B-Tech."

7. A “MusicAlbum” class with artist, title, and songs properties:

class MusicAlbum:
    def __init__(self, artist, title, songs):
        self.artist = artist
        self.title = title
        self.songs = songs

album = MusicAlbum("Taylor Swift", "Reputation", ["...Ready for It?", "End Game", "I Did Something Bad"])
print(album.artist) # Output: "Taylor Swift"
print(album.title) # Output: "Reputation"
print(album.songs) # Output: ["...Ready for It?", "End Game", "I Did Something Bad"]

8. A “Movie” class with title, director, and year properties:

class Movie:
    def __init__(self, title, director, year):
        self.title = title
        self.director = director
        self.year = year

movie = Movie("Inception", "Christopher Nolan", 2010)
print(movie.title) # Output: "Inception"
print(movie.director) # Output: "Christopher Nolan"
print(movie.year) # Output: 2010

9. A “ShoppingCart” class with items and total_price properties, and an “add_item” method:

class ShoppingCart:
    def __init__(self):
        self.items = []
        self.total_price = 0

    def add_item(self, item, price):
        self.total_price += price

cart = ShoppingCart()
cart.add_item("apple", 0.5)
cart.add_item("banana", 0.3)
print(cart.items) # Output: ["apple", "banana"]
print(cart.total_price) # Output: 0.8

10. A “Weather” class with temperature and conditions properties, and a “forecast” method:

class Weather:
    def __init__(self, temperature, conditions):
        self.temperature = temperature
        self.conditions = conditions

    def forecast(self):
        print(f"The temperature is {self.temperature} degrees and the conditions are {self.conditions}.")

weather = Weather(72, "sunny")
print(weather.temperature) # Output: 72
print(weather.conditions) # Output: "sunny"
weather.forecast() # Output: "The temperature is 72 degrees and the conditions are sunny."

This “Weather” class can be used to store and display information about the current weather conditions, such as the temperature and whether it is sunny, rainy, etc.

  1. What is class in Python with real life example?

    A class in Python is a template for creating objects (i.e. instances), which defines their properties and methods. For example, a “Car” class could have properties such as make and model, and methods such as “drive” and “stop”. In real life, a car is a class, a specific car is the instance.

  2. What is class in oops with example Python?

    OOPS(Object Oriented Programming) is a programming paradigm where everything is represented as an object. A class in OOPs is a blueprint for creating objects (i.e. instances) that have certain properties and methods. For example, in Python, a “Car” class could have properties such as make and model, and methods such as “drive” and “stop”.

Pramod Kumar Yadav is from Janakpur Dham, Nepal. He was born on December 23, 1994, and has one elder brother and two elder sisters. He completed his education at various schools and colleges in Nepal and completed a degree in Computer Science Engineering from MITS in Andhra Pradesh, India. Pramod has worked as the owner of RC Educational Foundation Pvt Ltd, a teacher, and an Educational Consultant, and is currently working as an Engineer and Digital Marketer.

Leave a Comment