UNIT-V_OOP_Python
UNIT-V_OOP_Python
PYTHON
Introduction
• Python allows the users to program in procedural, functional and object-
oriented style.
• Why OOP in python?
Structuring a program so that properties and behaviors are bundled into
individual objects to reuse the code.
Class
Objects
OOPs Polymorphism
Concepts
in Python Encapsulation
Inheritance
Data Abstraction
Python Class
• A class is a collection of objects.
• A class contains the blueprints or the prototype from which the
objects are being created.
Python Class
• A user-defined prototype for an object that defines a set of attributes that
characterize any object of the class.
• The attributes are data members (class variables and instance variables) and
methods, accessed through dot notation.
Python
Class
Python Class
• The argument self refers to the current instance of the class.
• Python adds the self argument automatically when we call the methods.
Python Object
• An object has two characteristics:
• attributes
• behavior
class Dog:
def __init__(self, name, age):
Attributes
self.name = name
self.age = age
# class attribute
name = ""
age = 0
# access attributes
print(f"{parrot1.name} is {parrot1.age} years old")
print(f"{parrot2.name} is {parrot2.age} years old")
• The first method __init__() is a special method, which is called class
constructor or initialization method that Python calls when we create a new
instance known as object of this class.
INHERITANCE
• What is inheritance? In terms of programming
• Why inheritance? • Attributes
• properties • Methods
• characters
• behaviors
INHERITANCE
• A class’s ability to inherit/transfer methods Base/super
and/or characteristics from another class is
known as inheritance.
Derived/subclass
Syntax
class BaseClass:
Body of base class
class DerivedClass(BaseClass):
Body of derived class
Types of Inheritance
Single inheritance
Multiple Inheritance
Multilevel inheritance
Hierarchical Inheritance
Hybrid Inheritance
Single inheritance
• single or simple inheritance, a child class inherits from a single parent class.
There is one child class and one parent class.
Multilevel inheritance
class Dog(Animal):
# Overriding the 'sound' method
def sound(self):
# Creating objects
a = Animal()
d = Dog()
# Creating an object
Method
calc = Calculator()
Overloading
NameError: This exception is raised when a variable or function name is not found in the current
scope.
IndexError: This exception is raised when an index is out of range for a list, tuple, or other sequence
types.
ValueError: This exception is raised when a function or method is called with an invalid argument or
input, such as trying to convert a string to an integer when the string does not represent a valid integer.
Exception
AttributeError: This exception is raised when an attribute or method is not found on
an object, such as trying to access a non-existent attribute of a class instance.
IOError: This exception is raised when an I/O operation, such as reading or writing a
file, fails due to an input/output error.
ImportError: This exception is raised when an import statement fails to find or load a
module.
Python try...except Block
try:
# code that may cause exception
except:
# code to run when exception occurs
Example - 1
Example - 2
Example - 3
try:
k = 5//0
print(k)
except ZeroDivisionError:
print("Can't divide by zero")
finally:
print('This is always executed')
Libraries in Python
• A library is a collection of precompiled codes that can be used
later in a program for some specific well-defined operations.
Python - GUI
• A graphical user interface (GUI) is a desktop interface that allows you to
communicate with computers.
• Tkinter
• wxPython
• PyQt
• PyGTK
• PySimpleGUI
• Pygame
Python GUI – tkinter
• Python tkinter is the fastest and easiest way to create GUI applications.
• To create a tkinter Python app:
• Importing the module – tkinter
• Create the main window (container)
• Add any number of widgets to the main window
• Apply the event Trigger on the widgets.