Chapter folders renamed

This commit is contained in:
Karan Solanki
2021-08-13 11:44:51 +05:30
parent d9f3f5b159
commit 1eb709f83a
211 changed files with 0 additions and 0 deletions
+25
View File
@@ -0,0 +1,25 @@
#abstraction1.py
from abc import ABC, abstractmethod
class Vehicle(ABC):
def hello(self):
print(f"Hello from abstract class")
@abstractmethod
def print_me(self):
pass
class Car (Vehicle):
def __init__(self, color, seats):
self.i_color = color
self.i_seats = seats
"""It is must to implemented this method"""
def print_me(self):
print( f"Car with color {self.i_color} and no of seats {self.i_seats}")
if __name__ == "__main__":
#vehicle = Vehicle()
#vehicle.hello()
car = Car ("blue", 5)
car.print_me()
car.hello()
+10
View File
@@ -0,0 +1,10 @@
#carexample1.py
class Car:
pass
if __name__ == "__main__":
car = Car ()
car.color = "blue"
car.miles = 1000
print (car.color)
print (car.miles)
+13
View File
@@ -0,0 +1,13 @@
#carexample2.py
class Car:
c_mileage_units = "Mi"
def __init__(self, color, miles):
self.i_color = color
self.i_mileage = miles
if __name__ == "__main__":
car = Car ("blue", 1000)
print (car.i_color)
print (car.i_mileage)
print (car.c_mileage_units)
print (Car.c_mileage_units)
+25
View File
@@ -0,0 +1,25 @@
#carexample3.py
class Car:
c_mileage_units = "Mi"
def __init__(self, color, miles):
self.i_color = color
self.i_mileage = miles
if __name__ == "__main__":
car1 = Car ("blue", 1000)
car2 = Car("red", 2000)
print("using car1: " + car1.c_mileage_units)
print("using car2: " + car2.c_mileage_units)
print("using Class: " + Car.c_mileage_units)
car1.c_mileage_units = "km"
print("using car1: " + car1.c_mileage_units)
print("using car2: " + car2.c_mileage_units)
print("using Class: " + Car.c_mileage_units)
Car.c_mileage_units = "NP"
print("using car1: " + car1.c_mileage_units)
print("using car2: " + car2.c_mileage_units)
print("using Class: " + Car.c_mileage_units)
+15
View File
@@ -0,0 +1,15 @@
#carexampl4.py
class Car:
c_mileage_units = "Mi"
def __init__(self, color, miles):
self.i_color = color
self.i_mileage = miles
def __str__(self):
return f"car with color {self.i_color} and mileage {self.i_mileage}"
if __name__ == "__main__":
car1 = Car ("blue", 1000)
print (str(car1))
print(repr(car1))
print(dir(car1))
+25
View File
@@ -0,0 +1,25 @@
#carexample5.py
class Car:
c_mileage_units = "Mi"
__max_speed = 200
def __init__(self, color, miles, model):
self.i_color = color
self.i_mileage = miles
self.__no_doors = 4
self._model = model
def __str__(self):
return f"car with color {self.i_color}, mileage {self.i_mileage}, model {self._model} and doors {self.__doors()}"
def __doors(self):
return self.__no_doors
if __name__ == "__main__":
car = Car ("blue", 1000, "Camry")
print (car)
"""These lines are only for illustration and not best practice"""
print(Car._Car__max_speed)
print (car._Car__doors())
print (car._model) #possible but against the protected definition
+30
View File
@@ -0,0 +1,30 @@
#carexample6.py
class Car:
__mileage_units = "Mi"
def __init__(self, col, mil):
self.__color = col
self.__mileage = mil
def __str__(self):
return f"car with color {self.get_color()} and mileage {self.get_mileage()}"
def get_color(self):
return self.__color
def get_mileage(self):
return self.__mileage
def set_mileage (self, new_mil):
self.__mileage = new_mil
if __name__ == "__main__":
car = Car ("blue", 1000)
print (car)
print (car.get_color())
print(car.get_mileage())
car.set_mileage(2000)
print (car.get_color())
print(car.get_mileage())
+32
View File
@@ -0,0 +1,32 @@
#carexample7.py
class Car:
__mileage_units = "Mi"
def __init__(self, col, mil):
self.__color = col
self.__mileage = mil
def __str__(self):
return f"car with color {self.color} and mileage {self.mileage}"
@property
def color(self):
return self.__color
@property
def mileage(self):
return self.__mileage
@mileage.setter
def mileage (self, new_mil):
self.__mileage = new_mil
if __name__ == "__main__":
car = Car ("blue", 1000)
print (car)
print (car.color)
print(car.mileage)
car.mileage = 2000
print (car.color)
print(car.mileage)
+25
View File
@@ -0,0 +1,25 @@
#carwithinnerexample1.py
class Car:
"""outer class"""
c_mileage_units = "Mi"
def __init__(self, color, miles, eng_size):
self.i_color = color
self.i_mileage = miles
self.i_engine = self.Engine(eng_size)
def __str__(self):
return f"car with color {self.i_color}, mileage {self.i_mileage} and engine of {self.i_engine}"
class Engine:
"""inner class"""
def __init__(self, size):
self.i_size = size
def __str__(self):
return self.i_size
if __name__ == "__main__":
car = Car ("blue", 1000, "2.5L")
print (car)
print (car.i_engine.i_size)
+30
View File
@@ -0,0 +1,30 @@
#methodsexample1.py
class Car:
c_mileage_unit = "Mi"
def __init__(self, color, miles):
self.i_color = color
self.i_mileage = miles
def print_color (self):
print (f"Color of the car is {self.i_color}")
@classmethod
def print_units(cls):
print (f"mileage unit are {cls.c_mileage_unit}")
print(f"class name is {cls.__name__}")
@staticmethod
def print_hello():
print ("Hello from a static method")
if __name__ == "__main__":
car = Car ("blue", 1000)
car.print_color()
car.print_units()
car.print_hello()
Car.print_color(car);
Car.print_units();
Car.print_hello()
+13
View File
@@ -0,0 +1,13 @@
#snippet.py
class Name:
def __init__(self):
self.first = "John"
class Name:
#class attributes
c_name_length = 80
#constructor
def __init__(self, first, last):
self.i_first = first #i_first is an instance attribute
self.i_last = last #i_last is an instance attribute
+32
View File
@@ -0,0 +1,32 @@
#composition1.py
class Seat:
def __init__(self, type):
self.i_type = type
def __str__(self):
return f"Seat type: {self.i_type}"
class Engine:
def __init__(self, size):
self.i_size = size
def __str__(self):
return f"Engine: {self.i_size}"
class Car:
def __init__(self, color, eng_size, seat_type):
self.i_color = color
self.engine = Engine(eng_size)
self.seat = Seat(seat_type)
def print_me(self):
print(f"This car of color {self.i_color} with {self.engine} and {self.seat}")
if __name__ == "__main__":
car = Car ("blue", "2.5L", "leather" )
car.print_me()
print(car.engine)
print(car.seat)
print(car.i_color)
print(car.engine.i_size)
print(car.seat.i_type)
+17
View File
@@ -0,0 +1,17 @@
#ducttype1.py
class Car:
def start(self):
print ("start engine by ignition /battery")
class Cycle:
def start(self):
print ("start by pushing paddles")
class Horse:
def start(self):
print ("start by pulling/releasing the reins")
if __name__ == "__main__":
for obj in Car(), Cycle(), Horse():
obj.start()
+32
View File
@@ -0,0 +1,32 @@
#inheritance1.py
class Vehicle:
def __init__(self, color):
self.i_color = color
def print_vehicle_info(self):
print(f"This is vehicle and I know my color is {self.i_color}")
class Car (Vehicle):
def __init__(self, color, seats):
self.i_color = color
self.i_seats = seats
def print_me(self):
print( f"Car with color {self.i_color} and no of seats {self.i_seats}")
class Truck (Vehicle):
def __init__(self, color, capacity):
self.i_color = color
self.i_capacity = capacity
def print_me(self):
print( f"Truck with color {self.i_color} and loading capacity {self.i_capacity} tons")
if __name__ == "__main__":
car = Car ("blue", 5)
car.print_vehicle_info()
car.print_me()
truck = Truck("white", 1000)
truck.print_vehicle_info()
truck.print_me()
+30
View File
@@ -0,0 +1,30 @@
#inheritance2.py
class Vehicle:
def __init__(self, color):
self.i_color = color
def print_vehicle_info(self):
print( f"This is vehicle and I know my color is {self.i_color}")
class Engine:
def __init__(self, size):
self.i_size = size
def print_engine_info(self):
print(f"This is Engine and I know my size is {self.i_size}")
class Car (Vehicle, Engine):
def __init__(self, color, size, seat):
self.i_color = color
self.i_size = size
self.i_seat = seat
def print_car_info(self):
print(f"This car of color {self.i_color} with seats {self.i_seat} with engine of size {self.i_size}")
if __name__ == "__main__":
car = Car ("blue", "2.5L", 5 )
car.print_vehicle_info()
car.print_engine_info()
car.print_car_info()
@@ -0,0 +1,17 @@
#methodoverloading1.py
class Car:
def __init__(self, color, seats):
self.i_color = color
self.i_seat = seats
def print_me(self, i='basic'):
if(i =='basic'):
print(f"This car is of color {self.i_color}")
else:
print(f"This car is of color {self.i_color} with seats {self.i_seat}")
if __name__ == "__main__":
car = Car("blue", 5 )
car.print_me()
car.print_me('blah')
car.print_me('detail')
@@ -0,0 +1,31 @@
#methodoverriding1.py
class Vehicle:
def __init__(self, color):
self.i_color = color
def print_me(self):
print(f"This is vehicle and I know my color is {self.i_color}")
class Car (Vehicle):
def __init__(self, color, seats):
self.i_color = color
self.i_seats = seats
def print_me(self):
print( f"Car with color {self.i_color} and no of seats {self.i_seats}")
class Truck (Vehicle):
def __init__(self, color, capacity):
self.i_color = color
self.i_capacity = capacity
def print_me(self):
print( f"Truck with color {self.i_color} and loading capacity {self.i_capacity} tons")
if __name__ == "__main__":
vehicle = Vehicle("red")
vehicle.print_me()
car = Car ("blue", 5)
car.print_me()
truck = Truck("white", 1000)
truck.print_me()