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)
['__class__',
 '__delattr__',
 '__dict__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__gt__',
 '__hash__',
 '__init__',
 '__init_subclass__',
 '__le__',
 '__lt__',
 '__module__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '__weakref__',
 'doors',
 'enginetype',
 'self_driving',
 'windows']
car2=Car(3,4,"diesel")
print(car1.windows)
4
print(car2.enginetype)
diesel
car1.self_driving()
'This is a petrol car'
print(car2.doors)
4
car2.enginetype="diesel"
print(car2.enginetype)
diesel

Python Exception Handling

try:
    # code block where exception can occur
    a=b
except:
    print("some problem may have occured")
some problem may have occured
try:
    # code block where exception can occur
    a=b
except Exception as ex:
    print(ex)
name 'b' is not defined
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)
the user have not defined the error
 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)
unsupported operand type(s) for +: 'int' and 'str'
a=1
b='s'
c=a+b
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-15-b5351790d4cc> in <module>
      1 a=1
      2 b='s'
----> 3 c=a+b

TypeError: unsupported operand type(s) for +: 'int' and 'str'
 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)
the user has given unsupported data types for addition
try to make the data type similar
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)
enter number 1 = 12
enter number 2 = 12
print(c)
print(d)
print(e)
1.0
24
144
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)
enter number 1 = 1
enter number 2 = 2
12/0
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-28-898e9759c56e> in <module>
----> 1 12/0

ZeroDivisionError: division by zero
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")
enter number 1 = 12
enter number 2 = 0
12/0 is not defined
The execution is complete

Custom Exception

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")
Enter the year of birth1555
year range is not valid

Public Private and Protected Access modifiers

class Car():
    def __init__(self,windows, doors, enginetypes):
        self.windows=windows
        self.doors=doors
        self.enginetypes=enginetypes
audi = Car(4,5,"Diesel")
audi
<__main__.Car at 0x1ed3a2b5ac8>
audi.windows
4
audi.windows=5
audi.windows
5
dir(audi)
['__class__',
 '__delattr__',
 '__dict__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__gt__',
 '__hash__',
 '__init__',
 '__init_subclass__',
 '__le__',
 '__lt__',
 '__module__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '__weakref__',
 'doors',
 'enginetypes',
 'windows']
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)
['__class__',
 '__delattr__',
 '__dict__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__gt__',
 '__hash__',
 '__init__',
 '__init_subclass__',
 '__le__',
 '__lt__',
 '__module__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '__weakref__',
 '_doors',
 '_enginetypes',
 '_windows',
 'hp']
truck._doors=4
truck._doors
4
audi._windows
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-45-63cafd091289> in <module>
----> 1 audi._windows

AttributeError: 'Car' object has no attribute '_windows'
dir(audi)
['__class__',
 '__delattr__',
 '__dict__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__gt__',
 '__hash__',
 '__init__',
 '__init_subclass__',
 '__le__',
 '__lt__',
 '__module__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '__weakref__',
 'doors',
 'enginetypes',
 'windows']
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__doors',
 '_Car__enginetypes',
 '_Car__windows',
 '__class__',
 '__delattr__',
 '__dict__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__gt__',
 '__hash__',
 '__init__',
 '__init_subclass__',
 '__le__',
 '__lt__',
 '__module__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '__weakref__']

Inheritance

## 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
4
car.drive()
the person drive a car
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
True