Python & AI (502)
01. What is Python? Discuss the features and application of Python.
Ans:- Python is a general purpose, dynamic, high-level, and Object-Oriented programming
language developed by Guido Rossum in 1989 and it is firstly released in February 1991. The
python language is easy to learn and simple to use. It is similar to other programming
languages like C, C++, Java etc. The python language is fast, secure, platform independent and
reliable language so, it is widely used in all over the world. The Java code save with .py
extension and it use its own IDE to run python code. The Python IDE available in various
version and for different platform. The Python IDE provide editor to write code, compiler to
execute code, loader to display output The python supports multiple programming pattern,
including object-oriented, imperative, and functional or procedural programming styles. The
Python Support Most of the programming features of other programming Language like control
statement, looping, array, string, class, object, inheritance etc. and it and also Introduces
various new programming components such as List, Tuple, Set, Dictionary etc. The Python
Language is used in all kinds of applications like Data Science, Date Mining, Desktop
Applications, Console-based Applications, Mobile Applications, Artificial Intelligence, Web
Applications, Machine Learning and many more.
Example: Program in Python to print sum of two number
x,y,res=0,0,0 OUTPUT:
x=eval(input("Enter first numbers:-")) Enter first number:-5
y=eval(input(("Enter Second number:-")) Enter Second number:-7
res=x+y Sum=12
print("sum=",res)
Features of Python languages
(1) Easy to Learn and Use - Python is easy to learn as compared to other programming languages.
Its syntax is straightforward and much the same as the English language. There is no use of the
semicolon or curly-bracket, the indentation defines the code block.
(2) Expressive Language - Python can perform complex tasks using a few lines of code. A simple
example, the hello world program you simply type print("Hello World"). It will take only one line to
execute, while Java or C takes multiple lines.
(3) Free and Open Source – Python is Free and Open-Source programming language. It is freely
available on its official website. It has a large community across the world that is dedicatedly working
towards make new python modules and functions. Anyone can contribute to the Python community.
(4) Object-Oriented Language - Python is an object-oriented programming language. It supports
most of the object-oriented programming features like class, object, inheritance, polymorphism, and
encapsulation, etc.
(5) Large Standard Library - It provides a vast range of libraries for the various fields such as
machine learning, web developer, and also for the scripting. There are various libraries provided by
python such as Tensor flow, Pandas, scipy, pygame, and pybrain, etc.
(6) Platform Independent – Java is platform independent programming language. It can run on
almost any platform including Windows, Linux, MacOS, Unix etc.
(7) Interpreted Language - Python is an interpreted language; it means the Python program is
executed one line at a time which makes it easier to debug and portable.
(8) Dynamic Memory Allocation - In Python, we don't need to specify the data-type of the variable.
Suppose we are assigned integer value 15 to x, then we don't need to write int x = 15. Just write x =
15.
SAURABH KUMAR Page 1 of 4 7488944009
Python & AI (502)
Applications of Python
(1) Web Applications - We can use Python to develop web applications. It provides libraries to handle
internet protocols such as HTML and XML, JSON, Email processing, request, etc.
(2) Desktop GUI Applications - The GUI stands for the Graphical User Interface, which provides a
smooth interaction to any application. Python provides a Tk GUI library to develop a user interface.
(3) Enterprise Applications - Python can be used to create applications that can be used within an
Enterprise or an Organization. Some real-time applications are OpenERP, Tryton, Picalo, etc.
(4) AI & ML - Python language is the most suitable language for Artificial intelligence or machine
learning. It consists of many scientific and mathematical libraries, which makes easy to solve
complex calculations.
(5) Image Processing Application - Python contains many libraries that are used to work with the
image. The image can be manipulated according to our requirements by using these libraries. Some
libraries of image processing are OpenCV, Pillow, etc.
02. What is Inheritance? Discuss the different types of inheritance in python.
Ans:- The inheritance implements the reusability feature.so, using inheritance we can inherit one
class property to another class The inheritance provides another form of data abstraction. It is an
important part of OOPs (Object Oriented programming system). The inheritance establishes a
relationship between two or more classes. In simple word, we can say that the inheritance is a
mechanism of designing a new class from the old class in such a way that the new class acquire or
inherit all the data member and Member function of the old class. By using inheritance, the new
class object can only access the all-data member and methods of the old class. The new class has
properties and behaviour of the existing class as well as its own unique properties and behaviour.
There are two types of classes in inheritance:
(i) Super class – The class that is inherit the new class or the class from which a new class is inherited
is called super class. It is also known as base or parent class.
(ii) sub class – The new class which is inherited by the old class is called sub class. It is also known
as child or derived class
In python, a derived class can inherit base class by just mentioning the base class name in the
bracket after the derived class name.
Syntax:–
class derivedclassname(Baseclassname):
data member
methods ()
Example:-
Class dog(animal):
def bark(self):
print("dog barking")
Types of Inheritance:- In python, there are 5 types of inheritance
(i) Single Inheritance- – Single inheritance is the type of inheritance in
which a derived class is inherited from a single base class the derived class
acquires all the properties and behaviour of the single base class and it also
has its own unique properties and behaviour.
SAURABH KUMAR Page 2 of 4 7488944009
Python & AI (502)
Example-
class Parent: c = Child()
def func1(self): c.func1()
print("parent class.") c.func2()
class Child(Parent): OUTPUT:
def func2(self): Parent class
print("child class.") Child class
(ii) Multiple Inheritance - Multiple inheritance is a
type of inheritance in which derived class is inherited
by more than one base classes. In this inheritance,
the derived class acquires all the properties and
behaviour of the all base classes and it also has its
own unique properties and behaviour.
Example:-
class base1: c = child()
def show1(self): c.show1()
print("Base1 Class") c.show2()
class base2(): c.show3()
def show2(self): OUTPUT:
print("Base2 Class") Base1 Class
class child(base1,base2): Base2 Class
def show3(self): Child Class
print("Child Class")
(iii) Multilevel inheritance – Multilevel inheritance is a type of inheritance in which one class is
inherited by another class and it also inherit another class. In this inheritance, a derived class will
be inherited from a base class as well as the derived class also act as the base class to other class.
The lower-level classes have properties and behaviour of all higher-level classes.
Example:-
class base: print("Child Class")
def show1(self): c = Child()
print("Base c.show1()
Class") c.show2()
class middle(base): c.show3()
def show2(self): OUTPUT:
print("Middle Base Class
Class") Middle Class
class Child(middle): Child Class
def show3(self):
SAURABH KUMAR Page 3 of 4 7488944009
Python & AI (502)
(iv) Hierarchical Inheritance – Hierarchical inheritance
is a type of inheritance in which multiple derived class is
inherited from a single base class. In this type of
inheritance all the derived class acquire the properties
and behaviour of the base class. In Hierarchical
Inheritance, one class serves as a superclass (base class)
for more than one subclass.
Example :
class Parent: object2 = Child2()
def func1(self): object1.func1()
print("parent class.") object1.func2()
class Child1(Parent): object2.func1()
def func2(self): object2.func3()
print("child1 Class.") OUTPUT:
class Child2(Parent): Parent class
def func3(self): Child1 class
print("Child2 Class.") Parent class
object1 = Child1() Child2 class
(v) Hybrid Inheritance - Hybrid inheritance is a combination of more than one type of inheritance in
a single program.
class A: print("Class D ")
def func1(self): obj=D()
print("Class A") obj.func1()
class B(A): obj.func2()
def func2(self): obj.func3()
print("Class B ") obj.func4()
class C(A): OUTPUT:
def func3(self): Class A
print("Class C ") Class B
class D(B,C): Class C
def func4(self): Class D
SAURABH KUMAR Page 4 of 4 7488944009