Python Questions Long Answer Type

Python Questions Long Answer Type


Que-1.What is python programming language? Give its applications and features?
Ans:-Python is a general purpose, dynamic, high level and interpreted programming language. Python programming language was developed by Guido Van Rossum. It supports object oriented programming approach to develop applications. It is simple and easy to learn and provide lots of high level data structures.
Python is easy to learn yet powerful and versatile scripting language which makes it attractive for Application Development. Python makes the development and debugging fast because there is no compilation step included in python development and edit-test-debug cycle is very fast.
FEATURES:-
1 .Easy to learn and use:-Python is easy to learn and use. It is developer-friendly and high level programming language.
2 .Expressive language:-Python language is more expressive means that it is more understandable and readable.
3. Interpreted language:-Python is an interpreted language i.e interpreter executes the code line by line at a time. This makes debugging easy and thus suitable for beginners.
4. Free and open source:-Python language is freely available at official web address.The source code is also available.
5.Integrated:-It can be easily integrated with languages like C,C++,JAVA etc.
APPLICATIONS:-
Python is known for its general purpose nature that makes it applicable in almost each domain of software development.
1.WEB APPLICATIONS:-We can use Python to develop web applications.It provides libraries to handle internet  protocols such as HTML,XML,JSON,Email  processing etc.
2.DESKTOP GUI  APPLICATIONS:-Python provides TK GUI library to develop user interface in python based application.
3.SOFTWARE DEVELOPMENT:-Python is helpful for software development process.It works as a support language and can be used for build control and management,testing etc.
4.BUSINESS APPLICATIONS:-Python is used to build Business applications like ERP and e-commerce system.
5.CONSOLE BASED APPLICATIONS:-We can use Python to develop console based applications.For example:IPython.
Que-2.What is inheritance and its types with examples?
Ans:-Inheritance:-It is an important aspect of the object oriented paradigm.Inheritance provides code reusability to the program because we can use an existing class to create a new class instead of creating it from scratch.In inheritance,the child class axquires the properties and can access all the data members and functions defined in the parent class.
SYNTAX:-
  Class  derived-class(base class):
<class-suite>
EXAMPLE:-      
class Animal:
                        def speak(self):
                                    print(“Animal speaking”)
class Dog(Animal):
                        def bark(self):
                                    print(“dog barking”)
d=Dog()
d.bark()
d.speak()
(i)MULTI-LEVEL INHERITANCE:-Multi-level inheritance is possible in Python like other object-oriented languages.Multi-level inheritance is achieved when a derived class inherits another derived class.There is no limit on the number of levels up to which,the multi-level inheritance is achieved in python.
SYNTAX:-
 class class1:
<class-suite>
 class class2(class1):
<class suite>
 class class3(class2):
<class suite>
EXAMPLE:-
class Animal:
                        def speak(self):
                                    print(“Animal speaking”)
class Dog(Animal):
                        def bark(self):
                                    print(“dog barking”)
class DogChild(Dog):
                        def eat(self):
                                    print(“eating bread…”)
d=DogChild()
d.bark()
d.speak()
d.eat()
(ii)MULTIPLE INHERITANCE:-Python provides us the flexibility to inherit multiple base classes in the child class.
SYNTAX:-
 class Base1:
<class-suite>
 class Base2:
<class-suite>
 class BaseN:
<class-suite>
 Class Derived(Base1,Base2,……..BaseN):
<class-suite>
EXAMPLE:-
class Calculation1:
                        def Summation(self,a,b):
                                    return a+b;
class Calculation2:
                        def Multiplication(self,a,b):
                                    return a*b;
class Derived(Calculation1,Calculation2):
                        def Divide(self,a,b):
                                    return a/b;
d=Derived()
print(d.Summation(10,20))
print(d.Multiplication(10,20))
print(d.Divide(10,20))

Que-3.What is tuple?What are its basic operations and functions?
Ans:-Python tuple is used to store the sequence of immutable python objects.Tuple is similar to lists since the value of the items stored in the list can be changed whereas the tuple is immutable and the value of the items stored in the tuple can not be changed.A tuple can be written as the collection of comma-separated values enclosed with the small brackets.A tuple can be defined as follows:-
T1=(101,”xyz”,22)
EXAMPLE:-
tuple1=(10,20,30,40,50)
print(tuple1)
count=0
for I in tuple1:
                        print(“tuple1[%d]=%d”%(count,i);
                        count=count+1
TUPLE OPERATIONS:-
The operators like concatenation(+),repetition(*),membership(in)works in the same way as they work with the list.
EXAMPLE:-Tuple t=(1,2,3,4,5)and Tuple t1=(6,7,8,9)are declared.
(i)Repetition:-The repetition operator enables the tuple elements to be repeated multiple times.
Example:- T1*2=(1,2,3,4,5,1,2,3,4,5)
(ii)Concatenation:-It concatenates the tuple mentioned on either side of the operator.
Example:-T1+T2=(1,2,3,4,5,6,7,8,9)
(iii)Membership:-It returnstrue if a particular item exists in the tuple otherwise false.
Example:-print(2 in T1)prints True
(iv)Iteration:-The for loop is used to iterate over the tuple elements.
Example:-for I in T1
                                    Print(i)
(V)Length:-It is used to get the length of the tuple.
Example:-len(T1)=5
TUPLE FUNCTIONS:-
(i)cmp(tuple1,tuple2):-It compares two tuples and returns true if tuple1 is greater than tuple2 otherwise false.
(ii)len(tuple):-It calculates the length of the tuple.
(iii)max(tuple):-It returns the maximum element of the tuple.
(iv)min(tuple):-It returns the minimum element of the tuple.
(v)tuple(seq):-It converts the specified sequence to the tuple.
Que-4.What is file? Explain the methods to open,close the file in Python?
Ans:-A file is some information or data which stays in the computer storage device.Python gives you easy ways to manipulate these files.
OPENING A FILE:-Python provides the open()function which accepts two arguments,file name,access mode in which the file is accessed.The function returns a file object which can be used to perform various operations like reading writing etc.
SYNTAX:-
File object=open(<file-name>,<access-mode>)
EXAMPLE:-
fileptr=open(“file.txt”,”r”)
if fileptr:
                        print(“file is opened successfully”)
CLOSING A FILE:-Once all the operations are done on the file,we must close it through our python script using the close()method.

SYNTAX:-
fileobject.close()
EXAMPLE:-
fileptr=open(“file.txt”,”r’)
if fileptr:
                        print(“file is opened successfully”)
fileptr.close()
Que-5. What is exception? Name 5 common exceptions?
Ans:-An exception can be defined as an abnormal condition in a program resulting in the disruption in the flow of the program. Whenever an exception occurs, the program halts the execution, and thus the further code is not executed. Therefore, an exception is the error which python script is enable to tackle with.
Python provides us with the way to handle the exception so that the other part of the code can be executed without any disruption. However, if we do not handle the exception, the interpreter does not execute all the code that exists after that.
5 COMMON EXCEPTIONS:-
1.ZeroDivisionError:-Occurs when a number is divided by zero.
2.NameError:-It occurs when a name is not found. It may be local or global.
3.IndentationError:-If incorrect indentation is given.
4.IOError:-It occurs when input output operation fails.
5.EOFError:-It occurs when the end of the file is reached, and yet operations are being performed.
Que-6. What are modules? Explain types of statements provided to load modules?
Ans:-A python module can be defined as a python program file which contains a python code including python functions, class or variables. In other words we can say that our python code file saved with the extension(.py)is treated as a module. We may have a run able code inside the python module.
Modules in python provides us the flexibility to organize the code in a logical way.
To use the functionality of one module into another, we must have to import the specific module.
EXAMPLE:-
In this example, we will create a module named as file.py
Def displayMsg(name)
                        Print(“Hi”+name);
STATEMENTS USED TO LOAD THE PYTHON MODULE:-
We need to load the module in our python code to use its functionality.Python provides two types of statements:-
(i)The import statement
(ii)The from-import statement
1.The import statement:-The import statement is used to import all the functionality of one module into another.
We can import multiple modules with a single import statement.
SYNTAX:-
Import module1,module2,……..modulen
EXAMPLE:-
Import file;
name=input(“Enter the name”)
file.displayMsg(name)
2.The from-import statement:-Instead of importing the whole module into the namespace,python provides the flexibility to import only the specific attributes of a module.
SYNTAX:-
From<module-name>import<name1>,<name2>,<name3>
calculation.py
def summation(a,b):
                        return a+b;
def multiplication(a,b)
                        return a*b;
def divide(a,b)
                        return a/b;
Main.py
from calculation import summation
a=int(input(“enter the first number”))
b=int(input(“enter the second number”))

print(“sum=”,summation(a,b))