Python 13
Python 13
The except block catches and handles any errors that occur in the try block.
The else block runs only if no exceptions were raised in the try block.
Examples:
try:
print(x)
except:
print("An exception occurred")
Multiple Except:
try:
print(x)
except NameError:
print("Variable x is not defined")
except:
print("Something else went wrong")
With Else:
try:
print("Hello")
except:
print("Something went wrong")
else:
print("Nothing went wrong")
Using Finally:
try:
print(x)
except:
print("Something went wrong")
finally:
print("The 'try except' is finished")
Raise an exception
As a Python developer you can choose to throw an exception if a condition occurs.
To throw (or raise) an exception, use the raise keyword.
x = "hello"
if not type(x) is int:
raise TypeError("Only integers are allowed")
Encapsulation is the process of bundling data and methods that operate on that data
within a single unit — typically a class. It helps in hiding internal state and requiring all
interaction to be performed through an object’s methods.
Benefits of Encapsulation:
Improves modularity
Prevents accidental modification
Makes code easier to maintain and debug
class Employee:
def __init__(self, name):
self.Name = name
def set_salary(self,salary):
self.__Salary = salary
def get_salary(self):
return self.__Salary
emp1 = Employee("Mohan")
emp1.set_salary(25000)
emp1.age= 23
print(emp1.get_salary(),emp1.age)
Using @property
class Employee:
allowance = 10000
def __init__(self, name):
self.Name = name
def set_salary(self,salary):
self.__Salary = 10000 + salary
@property
def get_salary(self):
return self.__Salary
emp1 = Employee("Mohan")
emp1.set_salary(25000)
emp1.age= 23
# print(emp1.Name()) #error
print(emp1.get_salary)
# print(emp1.get_salary(),emp1.age)
def set_salary(self,salary):
self.__Salary = 10000 + salary
@property
def set_age(self):
return self.__age
@set_age.setter
def set_age(self,a):
self.__age = a
emp1 = Employee("Mohan")
emp1.set_salary(25000)
print (emp1.set_age)
emp1.set_age = 27
print(emp1.set_age)
# print(emp1.Name()) #error
print(emp1.get_salary)
# print(emp1.get_salary(),emp1.age)
Abstraction:
Abstraction means hiding complex implementation details and exposing only the
necessary parts of an object or function.
Benefits of Abstraction
● Simplifies interface
● Reduces complexity
● Improves reusability
Use Case:
● Building APIs
● Game development
● Data analysis tools
● GUI applications
ABCs ensure that subclasses implement specific methods. If they don’t, Python will raise
an error.
ABCs help define a blueprint for other classes. This is useful in large systems where
consistency matters.
from abc import ABC, abstractmethod
class animal(ABC):
@abstractmethod
def speak(self):
print("animal Speaks")
class dog(animal):
def speak(self):
print("Dog barks")
super().speak()
class cat(animal):
def speak(self):
print("cat meow")
def sound(obj):
obj.speak()
d =dog()
c= cat()
sound(d)
sound(c)
sound(animal())
EOFError Raised when the input() method hits an "end of file" condition (EOF)
NotImplementedError Raised when an abstract method requires an inherited class to override the
method
RuntimeError Raised when an error occurs that do not belong to any specific exceptions
StopIteration Raised when the next() method of an iterator has no further values