Python 1695083170
Python 1695083170
CONTENTS
Curated by TRS
21. What is the significance of functions that start and end with _
symbol in Python?
22. What is the difference between xrange and range in Python?
23. What is lambda expression in Python?
24. How will you copy an object in Python?
25. What are the main benefits of using Python?
26. What is a metaclass in Python?
27. What is the use of frozenset in Python?
28. What is Python Flask?
29. What is None in Python?
30. What is the use of zip() function in Python?
31. What is the use of // operator in Python?
32. What is a Module in Python?
33. How can we create a dictionary with ordered set of keys in Python?
34. Python is an Object Oriented programming language or a
functional programming language?
35. How can we retrieve data from a MySQL database in a Python
script?
36. What is the difference between append() and extend() functions of a
list in Python?
37. How will you handle an error condition in Python code?
38. What is the difference between split() and slicing in Python?
39. How will you check in Python, if a class is subclass of another class?
40. How will you debug a piece of code in Python?
41. How do you profile a Python script?
42. What is the difference between ‘is’ and ‘==’ in Python?
43. How will you share variables across modules in Python?
44. How can we do Functional programming in Python?
45. What is the improvement in enumerate() function of Python?
46. How will you execute a Python script in Unix?
47. What are the popular Python libraries used in Data analysis?
48. What is the output of following code in Python?
49. What is the output of following code in Python?
50. If you have data with name of customers and their location, which
data type will you use to store it in Python?
ACKNOWLEDGMENTS
In the above line we can replace encoding with the encoding that we want
to use.
4. What is the use of PEP 8 in Python?
PEP 8 is a style guide for Python code. This document provides the coding
conventions for writing code in Python. Coding conventions are about
indentation, formatting, tabs, maximum line length, imports organization,
line spacing etc. We use PEP 8 to bring consistency in our code. We
consistency it is easier for other developers to read the code.
5. What is Pickling in Python?
Pickling is a process by which a Python object hierarchy can be converted
into a byte stream. The reverse operation of Pickling is Unpickling.
Python has a module named pickle. This module has the implementation of
a powerful algorithm for serialization and de-serialization of Python object
structure.
Some people also call Pickling as Serialization or Marshalling.
With Serialization we can transfer Python objects over the network. It is
also used in persisting the state of a Python object. We can write it to a file
or a database.
6. How does memory management work in
Python?
There is a private heap space in Python that contains all the Python objects
and data structures. In CPython there is a memory manager responsible for
managing the heap space.
There are different components in Python memory manager that handle
segmentation, sharing, caching, memory pre-allocation etc.
Python memory manager also takes care of garbage collection by using
Reference counting algorithm.
7. How will you perform Static Analysis on
a Python Script?
We can use Static Analysis tool called PyChecker for this purpose.
PyChecker can detect errors in Python code.
PyChecker also gives warnings for any style issues.
Some other tools to find bugs in Python code are pylint and pyflakes.
8. What is the difference between a Tuple
and List in Python?
III. Size: A Tuple takes much lesser space than a List in Python.
Main differences between List and Dictionary data types in Python are as
follows:
We can use the unit testing modules unittest or unittest2 to create and run
unit tests for Python code.
We can even do automation of tests with these modules. Some of the main
components of unittest are as follows:
I. Test fixture: We use test fixture to create preparation methods
required to run a test. It can even perform post-test cleanup.
II. Test case: This is main unit test that we run on a piece of code. We
can use Testcase base class to create new test cases.
III. Test suite: We can aggregate our unit test cases in a Test suite.
IV. Test runner: We use test runner to execute unit tests and produce
reports of the test run.
19. What is the difference between an
Iterator and Iterable in Python?
When we work on a map or a for loop in Python, we can use next() method
to get an Iterable item from the Iterator.
20. What is the use of Generator in
Python?
II. Large library: There is a large library for utilities in Python that
can be used for different kinds of applications.
In Python, we have a built-in function zip() that can be used to aggregate all
the Iterable objects of an Iterator.
We can use it to aggregate Iterable objects from two iterators as well.
E.g.
list_1 = ['a', 'b', 'c']
list_2 = ['1', '2', '3']
for a, b in zip(list_1, list_2):
print a, b
Output:
a1
b2
c3
By using zip() function we can divide our input data from different sources
into fixed number of sets.
31. What is the use of // operator in
Python?
If we do not want to stop the program, we can just catch the error condition,
print a message and continue with our program.
E.g. In following code snippet we are catching the error and continuing
with the default value of age.
#!/usr/bin/python
try:
age=18+'duration'
except:
print("duration has to be a number")
age=18
print(age)
38. What is the difference between split()
and slicing in Python?
Both split() function and slicing work on a String object. By using split()
function, we can get the list of words from a String.
E.g. 'a b c '.split() returns [‘a’, ‘b’, ‘c’]
Slicing is a way of getting substring from a String. It returns another String.
E.g. >>> 'a b c'[2:3] returns b
39. How will you check in Python, if a class
is subclass of another class?
In Python, we can use the debugger pdb for debugging the code. To start
debugging we have to enter following lines on the top of a Python script.
import pdb
pdb.set_trace()
After adding these lines, our code runs in debug mode. Now we can use
commands like breakpoint, step through, step into etc for debugging.
41. How do you profile a Python script?
Python provides a profiler called cProfile that can be used for profiling
Python code.
We can call it from our code as well as from the interpreter.
It gives use the number of function calls as well as the total time taken to
run the script.
We can even write the profile results to a file instead of standard out.
42. What is the difference between ‘is’ and
‘==’ in Python?
E.g.
>>> lst = [10,20, 20]
>>> lst == lst[:]
True
>>> lst is lst[:]
False
43. How will you share variables across
modules in Python?
This will tell Unix to use Python interpreter to execute the script.
47. What are the popular Python libraries
used in Data analysis?
Some of the popular libraries of Python used for Data analysis are:
I. Pandas: Powerful Python Data Analysis Toolkit
II. SciKit: This is a machine learning library in Python.
III. Seaborn: This is a statistical data visualization library in Python.
IV. SciPy: This is an open source system for science, mathematics and
engineering implemented in Python.
48. What is the output of following code in
Python?
>>> thelist=['a','b']
>>> print thelist[3:]
Ans: The output of this code is following:
[]
Even though the list has only 2 elements, the call to thelist with index 3
does not give any index error.
49. What is the output of following code in
Python?
>>>name=’John Smith’
>>>print name[:5] + name[5:]
Ans: Output of this will be
John Smith
This is an example of Slicing. Since we are slicing at the same index, the
first name[:5] gives the substring name upto 5th location excluding 5th
location. The name[5:] gives the rest of the substring of name from the 5th
location. So we get the full name as output.
50. If you have data with name of
customers and their location, which data
type will you use to store it in Python?
In Python, we can use dict data type to store key value pairs. In this
example, customer name can be the key and their location can be the value
in a dict data type.
Dictionary is an efficient way to store data that can be looked up based on a
key.