Python Interview Questions
1 .
What is the purpose of PYTHONSTARTUP, PYTHONCASEOK, and PYTHONHOME environment variables?
PYTHONSTARTUP : It contains the path of an initialization file having Python source code. It is executed every time we start the interpreter. It is named as .pythonrc.py in Unix, and it contains commands that load utilities or modify PYTHONPATH.

PYTHONCASEOK : It is used in Windows to instruct Python to find the first case-insensitive match in an import statement. We can set this variable with any value to activate it.

PYTHONHOME : It is an alternative module search path. It is usually embedded in PYTHONSTARTUP or PYTHONPATH directories to make switching of module libraries easy.
2 .
What is a map function in Python?
The map() function in Python has two parameters, function and iterable. The map() function takes a function as an argument and then applies that function to all the elements of an iterable, passed to it as another argument. It returns an object list of results.
 
For example :
def calculateSq(n):
return n*n
numbers = (2, 3, 4, 5)
result = map( calculateSq, numbers)
print(result)
3 .
Write a code to display the contents of a file in reverse.
To display the contents of a file in reverse, the following code can be used:
 
for line in reversed(list(open(filename.txt))):
print(line.rstrip())
4 .
What is __init__ in Python?
Equivalent to constructors in OOP terminology, __init__ is a reserved method in Python classes. The __init__ method is called automatically whenever a new object is initiated. This method allocates memory to the new object as soon as it is created. This method can also be used to initialize variables.
5 .
Is Python fully object oriented?
Python does follow an object-oriented programming paradigm and has all the basic OOPs concepts such as inheritance, polymorphism, and more, with the exception of access specifiers. Python doesn’t support strong encapsulation (adding a private keyword before data members). Although, it has a convention that can be used for data hiding, i.e., prefixing a data member with two underscores.
6 .
What is the difference between append() and extend() methods?
Both append() and extend() methods are methods used to add elements at the end of a list.
 
append(element) : Adds the given element at the end of the list that called this append() method

extend(another-list) : Adds the elements of another list at the end of the list that called this extend() method
7 .
How does Python Flask handle database requests?
Flask supports a database-powered application (RDBS). Such a system requires creating a schema, which needs piping the schema.sql file into the sqlite3 command. So, we need to install the sqlite3 command in order to create or initiate the database in Flask.
 
Flask allows to request for a database in three ways :
 
before_request() : They are called before a request and pass no arguments.
after_request() : They are called after a request and pass the response that will be sent to the client.
teardown_request() : They are called in a situation when an exception is raised and responses are not guaranteed. They are called after the response has been constructed. They are not allowed to modify the request, and their values are ignored.
8 .
Why would you use NumPy arrays instead of lists in Python?
NumPy arrays provide users with three main advantages as shown below :
 
* NumPy arrays consume a lot less memory, thereby making the code more efficient.
* NumPy arrays execute faster and do not add heavy processing to the runtime.
* NumPy has a highly readable syntax, making it easy and convenient for programmers.
9 .
What is monkey patching in Python?
Monkey patching is the term used to denote the modifications that are done to a class or a module during the runtime. This can only be done as Python supports changes in the behavior of the program while being executed.
 
The following is an example, denoting monkey patching in Python:
# monkeyy.py 
class X: 
     def func(self): 
          print "func() is being called"
The above module (monkeyy) is used to change the behavior of a function at the runtime as shown below:
import monkeyy 
def monkey_f(self): 
     print "monkey_f() is being called"
# replacing address of “func” with “monkey_f
monkeyy.X.func = monkey_f 
obj = monk.X() 
# calling function “func” whose address got replaced
# with function “monkey_f()
obj.func()
10 .
How does Python handle memory management?
* Python uses private heaps to maintain its memory. So the heap holds all the Python objects and the data structures. This area is only accessible to the Python interpreter; programmers can’t use it.

* And it’s the Python memory manager that handles the Private heap. It does the required allocation of the memory for Python objects.

* Python employs a built-in garbage collector, which salvages all the unused memory and offloads it to the heap space.