KEMBAR78
Snippets | PDF
0% found this document useful (0 votes)
14 views2 pages

Snippets

The document contains code snippets demonstrating the use of classes in Python, including constructors, methods, and inheritance. It covers examples such as a Student class, a BankAccount class with private attributes, and an Employee class inheriting from a Person class. Additionally, it illustrates the use of properties and composition with a Car class that utilizes an Engine class.

Uploaded by

praveenrao528
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views2 pages

Snippets

The document contains code snippets demonstrating the use of classes in Python, including constructors, methods, and inheritance. It covers examples such as a Student class, a BankAccount class with private attributes, and an Employee class inheriting from a Person class. Additionally, it illustrates the use of properties and composition with a Car class that utilizes an Engine class.

Uploaded by

praveenrao528
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Snippets:​

1. ​
class Student:
def __init__(self, ________, ________):
self.name = name
self.branch = branch

def ________(self):
print(f"Name: {self.name}, Branch: {self.branch}")

s1 = Student("Anjali", "CSE")
s1.________()



2.
class BankAccount:
def __init__(self, balance):
self.________ = balance # Private attribute

def deposit(self, amount):


self.__balance += ________

def withdraw(self, amount):


if amount > self.__balance:
print("________!")
else:
self.__balance -= amount

def ________():
return self.__balance

acc = BankAccount(1000)
acc.deposit(500)
acc.withdraw(2000)
print("Balance:", acc.get_balance())

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

class Employee(Person):
def __init__(self, name, employee_id):
________.________(name)
self.employee_id = employee_id

def show(self):
print("Name:", self.name)
print("ID:", self.________)

emp = Employee("John", 101)


emp.________()

class Product:
def __init__(self, name, price):
self.__name = name
self.__price = price

@property
def price(self):
return self.__price

# Usage
p = Product("Watch", 1500)
print(p.________)

class Engine:
def start(self):
print("Engine started")

class Car:
def __init__(self):
self.engine = ________()

def start(self):
print("Car is starting...")
self.engine.________()

# Usage
mycar = Car()
mycar.________()

You might also like