Python Unit 4
Python Unit 4
Python Classes/Objects
Python is an object-oriented programming language. Almost everything in Python is an
object, with its properties and methods. A Class is like an object constructor, or a "blueprint" for
creating objects.
Create a Class
To create a class, use the keyword class.
class MyClass:
x=5
Create Object
Now we can use the class named MyClass to create objects
p1 = MyClass()
print(p1.x)
class MyClass:
x=5
p1 = MyClass()
print(p1.x)
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person("John", 36)
print(p1.name)
print(p1.age)
John
36
class Person:
def myfunc(arg):
print("Hello my name is " + arg.name)
p1 = Person("John", 36)
p1.myfunc()
p1.age = 40
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def myfunc(self):
print("Hello my name is " + self.name)
p1 = Person("John", 36)
print(p1.age)
p1.age = 40
print(p1.age)
36
40
Hello my name is John
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def myfunc(self):
print("Hello my name is " + self.name)
p1 = Person("John", 36)
print(p1.age)
del p1.age
print(p1.age)
del p1
print (p1)
36
Inheritance
Inheritance is a way of creating a new class for using details of an existing class without modifying
it. The newly formed class is a derived class (or child class). Similarly, the existing class is a base
class (or parent class).
Inheritance is used as given below in Python:
# parent class
class Bird:
def __init__(self):
print("Bird is ready")
def who(self):
print("Bird")
def swim(self):
print("Swim faster")
# child class
class Penguin(Bird):
def __init__(self):
print("Penguin is ready")
# call super() function
super().__init__()
def who(self):
print("Penguin")
def run(self):
print("Run faster")
peggy = Penguin()
peggy.who()
peggy.swim()
peggy.run()
Penguin is ready
Bird is ready
Penguin
Swim faster
Run faster
Python Programming Unit-2 Notes
In the above program, we created two classes i.e. Bird (parent class) and Penguin (child class). The
child class inherits the functions of parent class. We can see this from the swim() method.
Again, the child class modified the behavior of the parent class. We can see this from
the whoisThis() method. Furthermore, we extend the functions of the parent class, by creating a
new run() method.
Additionally, we use the super() function inside the __init__() method. This allows us to run
the __init__() method of the parent class inside the child class.
Polymorphism
Polymorphism is an ability (in OOP) to use a common interface for multiple forms (data types).
Suppose, we need to color a shape, there are multiple shape options (rectangle, square, circle).
However we could use the same method to color any shape. This concept is called Polymorphism.
class Parrot:
def fly(self):
print("Parrot can fly")
def swim(self):
print("Parrot can't swim")
class Penguin:
def fly(self):
print("Penguin can't fly")
def swim(self):
print("Penguin can swim")
# common interface
def flying_test(bird):
bird.fly()
#instantiate objects
blu = Parrot()
peggy = Penguin()
In the above program, we defined two classes Parrot and Penguin. Each of them have a
common fly() method. However, their functions are different.
To use polymorphism, we created a common interface i.e flying_test() function that takes any object
and calls the object's fly() method. Thus, when we passed the blu and peggy objects in
the flying_test() function, it ran effectively.
Modules
In Python, a module is a self-contained Python file that contains Python statements and definitions,
like a file named GFG.py, can be considered as a module named GFG which can be imported with
the help of import statement. However, one might get confused about the difference between
modules and packages. A package is a collection of modules in directories that give structure and
hierarchy to the modules.
Creating Modules
A module is simply a Python file with a .py extension that can be imported inside another Python
program. The name of the Python file becomes the module name. The module contains definitions
and implementation of classes, variables, and functions that can be used inside another program.
# Factorial.py
def fact(n):
if n==0 or n==1:
return (1)
else:
return (n*fact(n-1))
#test.py
import Factorial
n=int(input(“Enter the value: “))
print(Factorial.fact(n))
Now enter the Python interpreter and import this module with the following command:
Python pip is the package manager for Python packages. We can use pip to install packages that do
not come with Python. The basic syntax of pip commands in command prompt is:
pip 'arguments'
Python pip comes pre-installed on 3.4 or older versions of Python. To check whether pip is installed
or not type the below command in the terminal.
pip --version
This command will tell the version of the pip if pip is already installed in the system.
We can install additional packages by using the Python pip install command. Let’s suppose we want
to install the numpy using pip. We can do it using the below command.
The Python pip list command displays a list of packages installed in the system.
pip list
Python Programming Unit-2 Notes
The pip uninstall command does not uninstall the package dependencies. If you want to remove the
dependencies as well then you can see the dependencies using the pip show command and remove
each package manually.
PyPI
The Python Package Index, abbreviated as PyPI, is the official repository of software for the Python
programming language. By default, pip — which is the most popular Python package manager —
uses PyPI as the source for retrieving package dependencies.
PyPI lets you find, install and even publish your Python packages so that they are widely available to
the public. More than 300,000 different packages are currently published in the index with more than
2,500,000 releases being distributed to users.