Python Interview Questions
1 .
How is Inheritance and Overriding methods are related?
If class A is a subclass of class B, then everything in B is accessible in /by class A. In addition, class A can define methods that are unavailable in B, and also it is able to override methods in B. For Instance, If class B and class A both contain a method called func(), then func() in class B can override func() in class A. Similarly, a method of class A can call another method defined in A that can invoke a method of B that overrides it.
2 .
How to retrieve data from a table in MySQL database through Python code? Explain.
Import MySQLdb module as : import MySQLdb Establish a connection to the database. db = MySQLdb.connect(“host”=”local host”, “database-user”=”user-name”, “password”=”password”, “database-name”=”database”) Initialize the cursor variable upon the established connection: c1 = db.cursor() Retrieve the information by defining a required query string. s = “Select * from dept” Fetch the data using fetch() methods and print it. data = c1.fetch(s) Close the database connection. db.close()
3 .
Explain Python’s zip() function.?
zip() function : it will take multiple lists say list1, list2, etc and convert them into a single list of tuples by taking their corresponding elements of the lists that are passed as parameters.
 
Example :
list1 = [‘A’, ‘B’,’C’] and list2 = [10,20,30]. zip(list1, list2) # results in a list of tuples say [(‘A’,10),(‘B’,20),(‘C’,30)]
 
whenever the given lists are of different lengths, zip stops generating tuples when the first list ends.
4 .
Write a sort algorithm for a dataframe in python?
import pandas as pd
import numpy as np
df = pd.DataFrame({‘A’:[3,1,1],’B’:[1,3,2]})
sort = df.sort_values(by = ‘B’)
print(sort)
5 .
What is heap-max and heap-min?
* In, a heap-max the key at root should be maximum among all the keys present in the heap. The same property must be recursively true for all nodes.

* In a heap-min the key at root should be minimum among all the keys present in the heap. The same property should be recursively true for all nodes.
6 .
What are the file related modules in python?
Python provides modules with functions which enable you to manipulate text and binary files on file system. Based on them you can create files, update the contents, copy, and delete files. The modules are os, shutil.
7 .
What is the procedure to install Python on Windows and set path variable?
We need to implement the following steps to install Python on Windows, and they are :
 
First you need to install Python from https://www.python.org/downloads/
After installing Python on your PC, find the place where it is located in your PC using the cmd python command.

Then visit advanced system settings on your PC and add new variable. Name the new variable as PYTHON_NAME then copy the path and paste it.

Search for the path variable and select one of the values for it and click on ‘edit’.
Finally we need to add a semicolon at the end of the value and if the semicolon is not present then type %PYTHON_NAME%.
8 .
What is data abstraction in Python?
In simple words, abstraction can be defined as hiding of unnecessary data and showing or executing necessary data. In technical terms, abstraction can be defined as hiding internal process and showing only the functionality. In Python abstraction can be achieved using encapsulation.
9 .
Define encapsulation in Python?
Encapsulation is one of the most important aspects of object-oriented programming. Binding or wrapping of code and data together into a single cell is called encapsulation. Encapsulation in Python is mainly used to restrict access to methods and variables.
10 .
How can we create an empty class in Python?
Empty class in Python is defined as a class that does not contain any code defined within the block. It can be created using pass keyword and object to this class can be created outside the class itself.
 
Example :
class x:
  pass
obj=x()
obj.id="123"
print("Id = ",obj.id)
 
Output : 123