Python Interview Questions and Answers Set 3

21. What’s your preferred text editor?

Emacs. Any alternate answer leads to instant disqualification of the applicant ūüėõ

22. When should you use generator expressions vs. list comprehensions in Python and vice­versa?

Iterating over the generator expression or the list comprehension will do the same thing. However, the list comp will create the entire list in memory first while the generator expression will create the items on the fly, so you are able to use it for very large (and also infinite!) sequences.

23. What is a negative index in Python?

Python arrays and list items can be accessed with positive or negative numbers. A negative Index accesses the elements from the end of the list counting backwards.



print a[-3]

Print a[-2]




24. What is the difference between range and xrange functions?

Range returns a list while xrange returns an xrange object which take the same memory no matter of the range size. In the first case you have all items already generated (this can take a lot of time and memory). In Python 3 however, range is implemented with xrange and you have to explicitly call the list function if you want to convert it to a list.

25. What is PEP8?

PEP8 is a coding convention (a set of recommendations) how to write your Python code in order to make it more readable and useful for those after you.

26. How can I find methods or attributes of an object in Python?

Built¬≠in dir() function of Python ,on an instance shows the instance variables as well as the methods and class attributes defined by the instance’s class and all its base classes alphabetically. So by any object as argument to dir() we can find all the methods & attributes of the object‚Äôs class

27. What is the statement that can be used in Python if a statement is required syntactically but the program requires no action?


28. Do you know what the difference between lists and tuples is? Can you give

Me an example for their usage?

First list are mutable while tuples are not, and second tuples can be hashed e.g. to be used as keys for dictionaries. As an example of their usage, tuples are used when the order of the elements in the sequence matters e.g. a geographic coordinates, “list” of points in a path or route, or set of actions that should be executed in specific order. Don’t forget that you can use them a dictionary keys. For everything else use lists

29. What is the function of ‚Äúself‚ÄĚ?

‚ÄúSelf‚ÄĚ is a variable that represents the instance of the object to itself. In most of the object oriented programming languages, this is passed to the methods as a hidden parameter that is defined by an object. But, in python it is passed explicitly. It refers to separate instance of the variable for individual objects. The variables are referred as ‚Äú‚ÄĚ.

30. How is memory managed in Python?

Memory management in Python involves a private heap containing all Python objects and data structures. Interpreter takes care of Python heap and the programmer has no access to it. The allocation of heap space for Python objects is done by Python memory manager. The core API of Python provides some tools for the programmer to code reliable and more robust program. Python also has a built­in garbage collector which recycles all the unused memory.

The gc module defines functions to enable /disable garbage collector: gc.enable() ­Enables automatic garbage collection. gc.disable()-Disables automatic garbage collection.