0% found this document useful (0 votes)
5 views4 pages

Python Class Examples and Inheritance

Python note

Uploaded by

kabhikabirsingh
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)
5 views4 pages

Python Class Examples and Inheritance

Python note

Uploaded by

kabhikabirsingh
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

In [4]: class computer:

def __init__(self):
self.__maxprice = 900

def sell(self):
print(f"Selling Price : {self.__maxprice}")

def setmaxprice(self,price):
self.__maxprice = price

c = computer()
[Link]()

c.__maxprice = 1000
[Link]()

[Link](1000)
[Link]()

Selling Price : 900


Selling Price : 900
Selling Price : 1000

In [5]: class computer:


def __init__(self):
[Link] = 900

def sell(self):
print(f"Selling Price : {[Link]}")

def setmaxprice(self,price):
[Link] = price

c = computer()
[Link]()

[Link] = 1000
[Link]()

[Link](1000)
[Link]()

Selling Price : 900


Selling Price : 1000
Selling Price : 1000

In [9]: class Polygon:


def render(self):
print("Rendering Polygon...")

class Square(Polygon):
def render(self):
print("Rendering Square...")

class Circle(Polygon):
def render(self):
print("Rendering Circle...")

s1 = Square()
[Link]()

c1 = Circle()
[Link]()

Rendering Square...
Rendering Circle...

In [11]: class Person:


def __init__(self,name,age):
[Link] = name
[Link] = age

class Professor(Person):

def isProfessor(self):
return f"{[Link]} is a Professor"

sir = Professor("Vipeen",40)
print([Link]())

Vipeen is a Professor

In [12]: from os import name


class Animal:
name = ""
def eat(self):
print("I can eat")

class Dog(Animal):
def display(self):
print("My name is ",[Link])
def eat(self): #Overwrtting
print("I like to eat bones")

d1 = Dog()
[Link] = "Tommy"
[Link]()
[Link]()

My name is Tommy
I like to eat bones

In [15]: class SubClass1:


n = 3

class SubClass2:
n2 = 5

class SubClass3(SubClass1,SubClass2):
def addition(self):
return self.n + self.n2
obj = SubClass3()
print([Link]())

In [17]: class Parent:


str1 = "VJTI"

class Child(Parent):
str2 = "VIPEEN"

obj = Child()
print(obj.str1,obj.str2)

VJTI VIPEEN

In [21]: class SuperClass:


x = 3

class SubClass(SuperClass):
pass
class SubClass2(SubClass):
pass

obj = SuperClass()
y = SubClass()
z = SubClass2()
print(obj.x)
print(y.x)
print(z.x)

3
3
3

In [26]: class one:


def display(self):
print("parent class")

class two:
def display(self):
print("sec parent class")

class three(two):
def display(self):
print("child class one")

class four(one,three):
def f4(self):
print("child class two")

In [24]: class x:
num = 10
class a(x):
pass
class b(x):
pass
class c(a):
pass
class d(a):
pass
obj = d()
print([Link])

10
This notebook was converted to PDF with [Link]

You might also like