Top Python Interview Questions and Answers
Top Python Interview Questions and Answers
(Download PDF)
We have prepared the most frequently asked Python Interview Questions and
Answers that will help you to prepare for the interview questions on Python that an
interviewer might ask you during your interview. In this list of Python Scripting
interview questions, we have covered all commonly asked basic and advanced Python
programming questions with detailed answers to help you clear the job interview
easily.
We have covered almost all important Python interview questions for freshers and
experienced candidates to help you prepare for the upcoming interview. This detailed
guide of Python coding interview questions will help you to crack your Job interview
for Python Programming.
2) What is PEP 8?
Pickle module accepts any Python object and converts it into a string representation
and dumps it into a file by using dump function, this process is called pickling. While
the process of retrieving original Python objects from the stored string representation
is called unpickling.
Python memory is managed by Python private heap space. All Python objects
and data structures are located in a private heap. The programmer does not have
an access to this private heap and interpreter takes care of this Python private
heap.
The allocation of Python heap space for Python objects is done by Python
memory manager. The core API gives access to some tools for the programmer
to code.
Python also have an inbuilt garbage collector, which recycle all the unused
memory and frees the memory and makes it available to the heap space.
6) What are the tools that help to find bugs or perform static analysis?
PyChecker is a static analysis tool that detects the bugs in Python source code and
warns about the style and complexity of the bug. Pylint is another tool that verifies
whether the module meets the coding standard.
The difference between list and tuple is that list is mutable while tuple is not. Tuple
can be hashed for e.g as a key for dictionaries.
There are mutable and Immutable types of Pythons built in types Mutable built-in
types
List
Sets
Dictionaries
Strings
Tuples
Numbers
In Python, every name introduced has a place where it lives and can be hooked for.
This is known as namespace. It is like a box where a variable name is mapped to the
object placed. Whenever the variable is searched out, this box will be searched, to get
corresponding object.
A lambda form in python does not have statements as it is used to make new function
object and then return them at runtime.
A mechanism to select a range of items from sequence types like list, tuple, strings
etc. is known as slicing.
To copy an object in Python, you can try copy.copy () or copy.deepcopy() for the
general case. You cannot copy all objects but most of them.
Python sequences can be index in positive and negative numbers. For positive index,
0 is the first index, 1 is the second index and so forth. For negative index, (-1) is the
last index and (-2) is the second last index and so forth.
In order to convert a number into a string, use the inbuilt function str(). If you want a
octal or hexadecimal representation, use the inbuilt function oct() or hex().
Xrange returns the xrange object while range returns the list, and uses the same
memory and no matter what the range size is.
25) What is module and package in Python?
In Python, module is the way to structure program. Each Python program file is a
module, which imports other modules like objects and attributes.
The folder of Python program is a package of modules. A package can have modules
or subfolders.
26) Mention what are the rules for local and global variables in Python?
Local variables: If a variable is assigned a new value anywhere within the function's
body, it's assumed to be local.
Global variables: Those variables that are only referenced inside a function are
implicitly global.
To share global variables across modules within a single program, create a special
module. Import the config module in all modules of your application. The module will
be available as a global variable across modules.
28) Explain how can you make a Python Script executable on Unix?
import random
random.random()
Module = =PyImport_ImportModule("<modulename>");
It is a Floor Divisionoperator , which is used for dividing two operands with the result
as quotient showing only digits before the decimal point. For instance, 10//5 = 2 and
10.0//5.0 = 2.0.
Python comprises of a huge standard library for most Internet platforms like
Email, HTML, etc.
Python does not require explicit memory management as the interpreter itself
allocates the memory to new variables and free them automatically
Provide easy readability due to use of square brackets
Easy-to-learn for beginners
Having the built-in data types saves programming time and effort from
declaring variables
The use of the split function in Python is that it breaks a string into shorter strings
using the defined separator. It gives a list of all words present in the string.
Flask is a web micro framework for Python based on "Werkzeug, Jinja 2 and good
intentions" BSD licensed. Werkzeug and jingja are two of its dependencies.
36) Mention what is the difference between Django, Pyramid, and Flask?
Like Pyramid, Django can also used for larger applications. It includes an ORM.
Flask-WTF offers simple integration with WTForms. Features include for Flask WTF
are
38) Explain what is the common way for the Flask script to work?
A session basically allows you to remember information from one request to another.
In a flask, it uses a signed cookie so the user can look at the session contents and
modify. The user can modify the session if only it has the secret key Flask.secret_key.
40) Is Flask an MVC model and if yes give an example showing MVC pattern for
your application?
Def hello():
app.run(debug = True)
Flask supports database powered application (RDBS). Such system requires creating a
schema, which requires piping the shema.sql file into a sqlite3 command. So you need
to install sqlite3 command in order to create or initiate the database in Flask.
42) You are having multiple Memcache servers running Python, in which one of
the memcacher server fails, and it has your data, will it ever try to get key data
from that one failed server?
The data in the failed server won't get removed, but there is a provision for auto-
failure, which you can configure for multiple nodes. Fail-over can be triggered during
any kind of socket or Memcached server level errors and not during normal client
errors like adding an existing key, etc.
43) Explain how you can minimize the Memcached server outages in your
Python Development?
When one instance fails, several of them goes down, this will put larger load on
the database server when lost data is reloaded as client make a request. To
avoid this, if your code has been written to minimize cache stampedes then it
will leave a minimal impact
Another way is to bring up an instance of Memcached on a new machine using
the lost machines IP address
Code is another option to minimize server outages as it gives you the liberty to
change the Memcached server list with minimal work
Setting timeout value is another option that some Memcached clients
implement for Memcached server outage. When your Memcached server goes
down, the client will keep trying to send a request till the time-out limit is
reached
44) Explain what is Dogpile effect? How can you prevent this effect?
Dogpile effect is referred to the event when cache expires, and websites are hit by the
multiple requests made by the client at the same time. This effect can be prevented by
using semaphore lock. In this system when value expires, first process acquires the
lock and starts generating new value.
45) Explain how Memcached should not be used in your Python project?