class Phone:
def __init__(self, brand, model_name, price):
self.brand = brand
self.model_name = model_name
self.price = price
@property
def full_name(self):
return f"This phone is {self.brand} {self.model_name}"
def make_a_call(self, number):
return f"Dialling {number}..."
class Smartphone(Phone):
def __init__(self, brand, model_name, price, ram, internal_memory, rear_camera):
super().__init__(brand, model_name, price)
self.ram = ram
self.internal_memory = internal_memory
self.rear_camera = rear_camera
S1 = Smartphone("OnePlus", "8 Pro", "75000", "8GB", "256GB", "64MP")
print(S1.full_name)
Tag: coding
Implementing static method in Python
class Laptop():
discount = 10
def __init__(self, brand_name, model_name, price):
#initializing instance varaibles
self.bn = brand_name
self.mn = model_name
self.p = price
def apply_disc(self):
after_disc = self.p - (self.p * (Laptop.discount/100))
return after_disc
def apply_special_disc(self, disc):
after_spcl_disc = self.p - (self.p * (disc/100))
return after_spcl_disc
@classmethod
def from_string(cls, string):
brand, model, price = string.split(',')
return cls(brand, model, price)
def full_brand(self):
return f"The name of the brand is {self.bn} {self.mn}"
@staticmethod
def intro():
print("Welcome to Laptop Shop")
L1 = Laptop('Lenovo', 'ABCD', 40000)
L2 = Laptop('Dell', 'EFGH', 50000 )
L3 = Laptop('HP', 'IJKL', 60000)
L4 = Laptop.from_string('HP,MNOP,70000')
Laptop.intro()
print(L4.full_brand())
Class method as constructor in Python
class Laptop():
discount = 10
def __init__(self, brand_name, model_name, price):
#initializing instance varaibles
self.bn = brand_name
self.mn = model_name
self.p = price
def apply_disc(self):
after_disc = self.p - (self.p * (Laptop.discount/100))
return after_disc
def apply_special_disc(self, disc):
after_spcl_disc = self.p - (self.p * (disc/100))
return after_spcl_disc
@classmethod
def from_string(cls, string):
brand, model, price = string.split(',')
return cls(brand, model, price)
def full_brand(self):
return f"The name of the brand is {self.bn} {self.mn}"
L1 = Laptop('Lenovo', 'ABCD', 40000)
L2 = Laptop('Dell', 'EFGH', 50000 )
L3 = Laptop('HP', 'IJKL', 60000)
L4 = Laptop.from_string('HP,MNOP,70000')
print(L4.full_brand())
Implementing class method in Python
class Laptop():
count_instance = 0
def __init__(self, brand_name, model_name, price):
#initializing instance varaibles
self.bn = brand_name
self.mn = model_name
self.p = price
Laptop.count_instance+=1 #counting the number of instances
def apply_disc(self, disc):
after_disc = self.p - (self.p * (disc/100))
return after_disc
@classmethod
def countInstance(cls):
return f"You have called {cls.count_instance} instances of class {cls.__name__}"
L1 = Laptop('Lenovo', 'ABCD', 40000)
L2 = Laptop('Dell', 'EFGH', 50000 )
L3 = Laptop('HP', 'IJKL', 60000)
print(L1.apply_disc(20))
print(Laptop.countInstance())
Counting number of instances of a class in Python
class Laptop():
count_instance = 0
def __init__(self, brand_name, model_name, price):
#initializing instance varaibles
self.bn = brand_name
self.mn = model_name
self.p = price
Laptop.count_instance+=1 #counting the number of instances
def apply_disc(self, disc):
after_disc = self.p - (self.p * (disc/100))
return after_disc
L1 = Laptop('Lenovo', 'ABCD', 40000)
L2 = Laptop('Dell', 'EFGH', 50000 )
L3 = Laptop('HP', 'IJKL', 60000)
print(L1.apply_disc(20))
print(f"Number of instances called = {Laptop.count_instance}")
Using instance variable and class variable wisely in Python
class Laptop():
discount = 10
def __init__(self, brand_name, model_name, price):
#initializing instance varaibles
self.bn = brand_name
self.mn = model_name
self.p = price
def apply_disc(self):
after_disc = self.p - (self.p * (Laptop.discount/100))
return after_disc
def apply_special_disc(self, disc):
after_spcl_disc = self.p - (self.p * (disc/100))
return after_spcl_disc
L1 = Laptop('Lenovo', 'ABCD', 40000)
L2 = Laptop('Dell', 'EFGH', 50000 )
L3 = Laptop('HP', 'IJKL', 60000)
print(L1.apply_disc())
print(L1.apply_special_disc(45))
How to write and use class variable in Python
class Circle:
pi = 3.141592653589793238
def __init__(self, radius):
self.r = radius
def calc_circum(self):
return 2*self.r*Circle.pi
def calc_area(self):
return Circle.pi*self.r*self.r
C1 = Circle(5)
print(C1.calc_area())
print(C1.calc_circum())
Basic Class and Object implementation using Python
- The init() method gets called depends upon how many objects we create of that class.
- init() method always starts with self keyword according to naming convention. It is not always necessary to write self.
- init() is a method as well as a constructor in Python.
class Person:
def __init__(self, first_name, last_name, age):
print("Init method / constructor gets called here.")
self.fn = first_name
self.ln = last_name
self.a = age
p1 = Person('Sayan', 'Dey', 21)
p2 = Person('Dey', 'Sayan', 21)
print(p1.fn)
print(p2.ln)
Sort Colors in Python without using sort() – Google Interview Question
def sort_colour(arr):
red = 0
blue = 0
for i in range(len(arr)):
if arr[i] == 1:
red+=1
elif arr[i] == 2:
blue+=1
return [1]*red + [2]*blue + [3]*(len(arr) - red - blue)
a = [1, 2, 3, 1, 3, 2, 1, 2, 3, 1]
print(sort_colour(a))
Sieve of Eratosthenes in Python
from math import sqrt
def sieve_prime(num):
prime_arr = [1] * (num+1)
prime_arr[0] = 0
prime_arr[1] = 0
for i in range(2, int(sqrt(num))+1):
if prime_arr[i] == 1:
j=2
while i*j<num+1:
prime_arr[j*i] = 0
j+=1
return prime_arr
print(sieve_prime(15))