Python-Advanced
correct way to initialise a Class
class Car:
def __init__(self,window,door,enginetype):
self.windows=window
self.doors=door
self.enginetype=enginetype
def self_driving(self):
return "This is a {} car".format(self.enginetype)
car1=Car(4,5,'petrol') # by default this init method is called
dir(car1)
car2=Car(3,4,"diesel")
print(car1.windows)
print(car2.enginetype)
car1.self_driving()
print(car2.doors)
car2.enginetype="diesel"
print(car2.enginetype)
try:
# code block where exception can occur
a=b
except:
print("some problem may have occured")
try:
# code block where exception can occur
a=b
except Exception as ex:
print(ex)
try:
# code block where exception can occur
a=b
except NameError as ex1:
print("the user have not defined the error")
except Exception as ex: # this always needs to be written on the lst
print(ex)
try:
# code block where exception can occur
a=1
b='s'
c=a+b
except NameError as ex1:
print("the user have not defined the error")
except Exception as ex: # this always needs to be written on the lst
print(ex)
a=1
b='s'
c=a+b
try:
# code block where exception can occur
a=1
b='s'
c=a+b
except NameError as ex1:
print("the user have not defined the error")
except TypeError as ex2:
print("the user has given unsupported data types for addition")
print("try to make the data type similar")
except Exception as ex: # this always needs to be written on the lst
print(ex)
try:
a=int(input("enter number 1 = "))
b=int(input("enter number 2 = "))
c=a/b
d=a+b
e=a*b
except NameError as ex1:
print("the user have not defined the error")
except TypeError as ex2:
print("the user has given unsupported data types for addition")
print("try to make the data type similar")
except Exception as ex: # this always needs to be written on the lst
print(ex)
print(c)
print(d)
print(e)
try:
a=int(input("enter number 1 = "))
b=int(input("enter number 2 = "))
c=a/b
d=a+b
e=a*b
except NameError as ex1:
print("the user have not defined the error")
except TypeError as ex2:
print("the user has given unsupported data types for addition")
print("try to make the data type similar")
except Exception as ex: # this always needs to be written on the lst
print(ex)
12/0
try:
a=int(input("enter number 1 = "))
b=int(input("enter number 2 = "))
c=a/b
d=a+b
e=a*b
except NameError as ex1:
print("the user have not defined the error")
except TypeError as ex2:
print("the user has given unsupported data types for addition")
print("try to make the data type similar")
except ZeroDivisionError as ex3:
print("12/0 is not defined")
except Exception as ex: # this always needs to be written on the lst
print(ex)
else:
print(c)
print(d)
print(e)
finally:
print("The execution is complete")
class Error(Exception): # inheriting the exception class
pass
class dobException(Error):
pass
year = int(input("Enter the year of birth"))
age = 2021-year
try:
if age<=30 & age>20:
print("Valid age")
else:
raise dobException
except dobException:
print("year range is not valid")
class Car():
def __init__(self,windows, doors, enginetypes):
self.windows=windows
self.doors=doors
self.enginetypes=enginetypes
audi = Car(4,5,"Diesel")
audi
audi.windows
audi.windows=5
audi.windows
dir(audi)
class Car():
def __init__(self,windows, doors, enginetypes):
self._windows=windows
self._doors=doors
self._enginetypes=enginetypes
class Truck(Car):
def __init__(self,windows, doors, enginetypes, hp):
super().__init__(windows,doors,enginetypes)
self.hp=hp
truck=Truck(4,2,"Petrol",720)
dir(truck)
truck._doors=4
truck._doors
audi._windows
dir(audi)
class Car():
def __init__(self,windows, doors, enginetypes):
self.__windows=windows
self.__doors=doors
self.__enginetypes=enginetypes
audi=Car(4,2,"petrol")
dir(audi)
## Car Blueprint
class Car():
def __init__(self, windows, doors, enginetype):
self.windows=windows
self.doors=doors
self.enginetype=enginetype
def drive(self):
print("the person drive a car")
car = Car(4,5,"diesel")
car.windows
car.drive()
class audi(Car):
def __init__(self,windows,doors,enginetype,enableai):
super().__init__(windows,doors,enginetype)
self.enableai=enableai
def selfdriving(self):
print("Audi Supports Self driving")
audiQ7=audi(5,5,'diesel',True)
audiQ7.enableai