Python programming interview questions

Q: Identify some of the core default modules included in Python?

  • email – used to parse, handle, and generate email messages.
  • string – an index of types of strings, such as all capital or lowercase letters.
  • sqlite3 – used to deal with the SQLite database.
  • xml – provides XML support.
  • logging – creates logging classes to log system details.
  • traceback – allows you to extract and print stack trace details.

Q: Why is used for?
The files are required to make Python interpret directories as containing packages.
It is mainly to prevent directories with common name being treated as packages. This permits the programmer control which directories are packages and which are not.
Moreover, can just be an empty file too. But it can also execute initialization code for the package or set the __all__variable.

Q: What is a negative index in Python?
Python arrays & list items can be accessed with positive or negative numbers (aka index). A negative index accesses the list elements from the end counting backwards.

Q: What is pickling and what is unpickling?
Pickling is a process by which a Python object is converted into a string representation by a pickle module. It is then placed into a file with the dump() function. Whereas unpickling refers to the reverse process, in which the stored string is retrieved and turned back into an object.

Q: What is slicing in Python?
Slicing in Python is a mechanism to select a range of items from Sequence types like strings, list, tuple, etc.

Q: Point out few ways to generate a random number in Python.
random() – this command returns a floating point number, between 0 and 1.
uniform(x,y) – this command returns a floating point number between the values given for x and y.
randint(x,y) – this command returns a random integer between the values given for x and y.

Q: Why is “pass” used for in Python?
“pass” is no-operation Python statement. It indicates nothing is to be done. It is just a place holder used in compound statements as they cannot be left blank.

Q: What are iterators in Python?
Iterators in Python are used to iterate over a group of elements, containers, like list. For a container to support iterator, it must provide iter().

Q: What are generators in Python?
Generators are a way of implementing iterators. Generator function is a normal function except that it contains yield expression in the function definition making it a generator function.
This function returns a generator iterator known as generator. To get the next value from a generator, we use the same built-in function as for iterators: next(). next() takes care of calling the generator’s next() method.

Q: How do I run a subprocess or a external program with arguments in Python?
There are two methods to run a sub process or program. First is to use the subprocess module in the stdlib.

The advantage of subprocess vs system is that it is more flexible. You can get the stdout, stderr, the “real” status code and better error handling. Second approach to run a program with arguments is as follows.

Q: Given the list below remove the repetition of an element.
All the elements should be unique
words = [‘one’, ‘one’, ‘two’, ‘three’, ‘three’, ‘two’]
A simple solution would be to iterate over the list, identify duplicates and remove them.
One of the best solutions which I can conceive right now could be:

set is an another type available in python. It doesn’t allows copies and provide some good functions to perform set operations like union, difference etc.

Q: Print the sum of digits of numbers starting from 1 to 100 (inclusive of both)

Q: Suggest the best way to store a list of employee first and last names?
A list of first and last names is best stored as a list of dictionaries. Following format can be used.

Q: Are arguments passed by value or by reference in Python?
Arguments are passed neither by value and nor by reference in Python. Instead they are passed by assignment.

The parameter passed in is actually a reference to an object, as opposed to reference to a fixed memory location. But the reference is passed by value. In addition, some data types (like strings, tuples etc.) are immutable whereas others are mutable.

Q: How do I copy an object in Python?
By “shallow copying” it means the content of the dictionary is not copied by value, but just creating a new reference.

In contrast, a deep copy will copy all contents by value.

Q: How do I convert a string to a number?
int() is the Python standard built-in function to convert a string into an integer value. You call it with a string containing a number as the argument, and it returns the number converted to an actual integer:

Q: How do you set a global variable in a function?
You can use a global variable in other functions by declaring it as global in each function that assigns to it:

I imagine the reason for it is that, since global variables are so dangerous, Python wants to make sure that you really know that’s what you’re playing with by explicitly requiring the global keyword.

Q: How do I share global variables across modules?
If you add a variable to the builtin module, it will be accessible as if a global from any other module that includes builtin — which is all of them, by default.

Note: Please note that builtin has been renamed to builtins in python3.

Q: Is there a tool to help find bugs or perform static analysis?
Yes. PyChecker is a static analysis tool that finds bugs in Python source code and warns about code complexity and style.
Pylint is another tool that checks if a module satisfies a coding standard, and also makes it possible to write plug-ins to add a custom feature.

Q: How do you perform unit testing in Python?
Python provides a unit testing framework called “unittest“. “unittest” module supports the following features.

  • Automation testing.
  • Sharing of setup and shutdown code for tests.
  • Aggregation of tests into collections.
  • Independence of the tests from the reporting framework.