PYTHON INTERVIEW QUESTION
1. What is Python? Benefits of Python
2. Difference between List and Tuples?
3. What is the difference between compiler and interpreter and why python is considered
an interpreted language.
4. What is the difference between For Loop and While Loop?
5. What is function?
6. What are python Decorators?
7. What is Constructor?
8. What is Destructor?
9. Difference between List and Dictionary comprehension?
10. How memory managed in python?
11. Difference between Generators and Iterators?
12. What is __init__ keywords?
13. Difference between Modules and Package?
14. Difference between Range and XRange?
15. What is built-in Data Type?
16. Explain Ternary Operator?
17. Difference between Local variable and Global Variable?
18. What is global, protected and private attributes
19. What is Scope in python?
20. What is PEP 8 and why it is important?
ANSWERS
1. Python is a high-level, interpreted programming language known for simplicity and
readability. It supports multiple programming paradigms, including procedural, object-
oriented and functional programming. Python is widely used in various domain such ad
web development, Data Science, AI and Automation.
Benefits of using Python:
Python is a general-purpose programming language that has a simple, easy-to-learn
syntax that emphasizes readability and therefore reduces the cost of program
maintenance.
Moreover, the language is capable of scripting, is completely open-source, and supports
third-party packages encouraging modularity and code reuse.
Its high-level data structures, combined with dynamic typing and dynamic binding,
attract a huge community of developers for Rapid Application Development and
deployment.
2. Lists and Tuples are both sequence data types that can store a collection of objects in
Python. The objects stored in both sequences can have different data types. But the real
difference is:
List is Mutable and Tuple is Immutable.
List can be sliced, append and modified but Tuple can’t be sliced, modified and
append.
List can be represented in square brackets and Tuples can be represented in
parentheses.
Lists consume more memory and Tuple consume less memory.
3. Compiler translate the entire source code into machine code at once. It creates
executable file whereas Interpreter translate and execute code line by line.
Compiler code generally run faster because the entire program is translated before
execution (e.g, Java, C, C++) whereas Interpreter code is slower than compiled code
because they translate and execute each line during runtime. (e.g, Python, JavaScript)
When we write python code and run it, the source code is first complied into byte code.
Bytecode is a lower-level representation of your code but it is not directly a machine
code. After python code is complied into bytecode, it executed by the python virtual
machine which is an interpreter. The PVM reads the bytecode and executes it line-byline
at run-time, which is why python is considered an interpreted language.
4. A for loop is used to iterate over a sequence (such as a list, tuple or string) whereas
while loop is used to executed a block of code as long as a certain condition is true.
For loop is use when you know exactly how many times you want to execute the loop
but while loop is use when you don’t know how many times you need to execute the
loop beforehand.
5. Function is a block of code which runs only when it’s called. It accepts or returns data
known as parameter or arguments.
6. Decorators are a just a function that takes another function as an argument, and some
functionality and the return another function. All of this without changing source code
of the original function that you have passed in.
7. Constructor is a special method that automatically called when a new object is created.
It is used to initialized the attribute of the object. (e.g, __init__).
8. Destructor method is called when the reference to an object have been destroyed. In
the python __del__() method is referred to as a destruction method.
9. The fundamental difference between list and comprehension in python lies in the type
of data structure they produce.
List comprehension:
Creates a new list. The result is in ordered sequence of elements, where each element
derived from expression applied to items in iterable.
Dictionary Compreshion:
Creates a new dictionary. The result is in key value pair, where both key and value
derived from expression applied to items in an iterable.
10. Memory management in python is handled by the python memory manager.
The memory allocated by the manager is in form of a private heap space dedicated to
python.
All python objects are stored in this heap and being private, it is inaccessible to the
programmer. Though, python does provide some core API Functions to work upon the
private heap space.
Additionally, Python has an in-built garbage collection to recycle the used memory for
private heap space.