Python Interview Question
1. What do you know about Python?
2. Who developed Python?
3. Why Python was named as “Python”?
G.V Rossum picked the name “Python” from the famous TV show, Monty Python’s Flying Circus and the rest is history.
4. Python is “dynamically typed”. Explain.
5. Is Python a scripting language?
The scripting language is a programming language that does not use a compiler for executing the source code. Rather, it uses an interpreter to translate source code to machine code on the go. Python is capable of scripting but, is considered to be a general-purpose language. The general-purpose programming language has a wide range of application domains.
6. What is Byte code?
7. What are the flavors of Python?
8. What is CPython?
9. Do you what is Jython?
10. What is a built-in datatype in Python?
None, int, float, complex, bool, str, bytes, list, tuple, range, etc.
11. You are given a mathematical expression in the form of the string, write a python code to solve it.
Do it with the help of the eval() function.
12. Give a brief explanation about the list and tuple?
Lists are mutable(can be changed) whereas Tuples are immutable(can’t be changed). Tuples are faster than lists.
General format for tuple : (4,5,6,9)
General format for list : [4,5,6,9]
13. Differentiate between list and tuple?
Tuple uses less memory whereas lists use more memory. We can’t modify tuples but lists can be modified. Tuples are faster than lists.
14. What is PEP 8?
It is a Python Enhancement Proposal. It refers to the formatting of python code for maximum readability and understandability.
15. Explain memory management in Python?
Memory Management in Python is managed by the Python Private heap space. All Python objects and data structures are stored in the heap area. Programmers do not have any direct access to the private heap area, the interpreter takes care of it.
16. Explain the Garbage Collection in Python?
Python has the concept of an inbuilt garbage collector. It recycles all the unused memory so that they get available for future use.
17. What is PYTHON PATH?
It is an environment variable which we can use to set the additional directories, where modules and packages are looked for.
For MAC :
1. Open Terminal.app;
2. Open the file ~/.bash_profile in your text editor – e.g. atom ~/.bash_profile;
3. Add the following line :
export PYTHONPATH=”/Users/my/code” and save it.
For LINUX :
1. Open your terminal;
2. Open the file ~/.bashrc in your text editor – e.g. atom ~/.bashrc;
3. Add the following line to the end: export PYTHONPATH=/home/my/code and save it.
18. What are python modules?
Python modules are .py files python executable code. Some builtin modules are math, random, sys, JSON, itertools.
19. What do you mean by JSON?
Python inbuilt module JSON is basically used to work with text, written with JavaScript Object Notation.
20. Explain the role of loads() & dumps() methods of JSON module.
loads() -> for parsing json text/string.
dumps() -> for converting python code to json string.
21. What is the role of the ord() method?
It is used to change a character to an integer.
22. What the basic difference between list & array?
Arrays have similar types of elements whereas lists have dynamic type elements.
23. Explain the role of the __init()__ method?
It is a predefined method in python classes. This method is called when an object is created from a class, it basically initializes the attributes of a class.
24. What are anonymous functions?
Lambda functions are popularly called as anonymous functions. Such methods generally have multiple parameters but a single statement.
For example :
a = lambda x,y : x+y
print(a(6,8)) will result in 14.
25. What is the self?
It is an object or an instance of a class, which is explicitly included as the first argument.
26. What is the pass keyword?
27. What is the continue keyword?
28. How we can generate randomize elements of lists?
29. Differentiate between range and xrange?
- it returns lists containing values.
- takes more memory.
- supports sliding.
- returns generator objects.
- takes less memory.
- doesn't support slicing.
30. How pickling works?
31. What are Generator functions?
32. What are decorators?
33. Tell me the role of 'is', 'not', 'in' keyword?
34. Analyze the role of help()?
35. Analyze the role of dir()?
It returns the list of all valid attributes.
36. Give general syntax for a ternary operator?
37. What are *args & **kargs?
38. How we can delete files in Python?
39. Expand docstring?
40. What are the different types of variables in python?
41. Explain the Instance variable?
42. How we can access the instance variable?
43. What are instance methods?
Methods that act on the instances of a class are known as instance methods. They generally use self as their first parameter, which refers to the location of that instance in memory.
44. Explain Static methods?
They are simple methods with no self argument. They work on class attributes not on instance attributes. They can be called through both class and instance.
45. Explain Abstract methods?
Abstract methods are the methods, that does not have anybody, and are further refined in the subclasses. Abstract methods are generally written with decorator @abstractmethod above them.
46. Abstract class contains only abstract methods. True?
No, it can also contain concrete methods.
47. In Python, is it possible to write abstract methods with the body also?
Yes.