Python Interview Question

1
What is Python?
Ans.Python is a high-level, interpreted programming language known for its simplicity and readability.
2
What are the key features of Python?
Ans.Python has features like dynamic typing, automatic memory management, and a large standard library.
3
Explain the difference between a list and a tuple in Python.
Ans.A list is mutable, meaning its elements can be changed, added, or removed. A tuple is immutable, and its elements cannot be modified after creation.
4
How do you create an empty list in Python?
Ans.You can create an empty list using empty square brackets: my_list = [].
5
What is the difference between "is" and "==" in Python?
Ans."is" checks if two variables refer to the same object, while "==" checks if two variables have the same value.
6
What is the purpose of the "pass" statement in Python?
Ans.The "pass" statement is a placeholder that does nothing. It is used when a statement is syntactically required but no action is needed.
7
What is a decorator in Python?
Ans.A decorator is a design pattern in Python that allows adding functionality to an existing function by wrapping it with another function.
8
Explain the concept of "duck typing" in Python.
Ans.Duck typing is a concept where the type or the class of an object is less important than the methods it defines. If an object walks like a duck and quacks like a duck, it's considered a duck.
9
How do you handle exceptions in Python?
Ans.Exceptions are handled using the try-except block. Code that might raise an exception is placed in the try block, and the exception handling code is placed in the except block.
10
What is the purpose of the "yield" keyword in Python?
Ans.The "yield" keyword is used in generator functions to turn them into iterators. It allows generating a series of values without the need to store them all in memory.
11
What is the Global Interpreter Lock (GIL) in Python?
Ans.The GIL is a mechanism in Python that allows only one thread to execute Python bytecode at a time. It prevents multiple threads from executing Python code in parallel.
12
How can you convert a string to an integer in Python?
Ans.You can use the int() function to convert a string to an integer: my_int = int("42").
13
How do you copy an object in Python?
Ans.You can copy an object using the copy module or by using the object's built-in copy methods, such as copy() for shallow copy and deepcopy() for deep copy.
14
Explain the difference between deep copy and shallow copy in Python.
Ans.Shallow copy creates a new object but references the same objects as the original, while deep copy creates a new object and recursively copies the objects it references.
15
What are the different types of namespaces in Python?
Ans.Python has three types of namespaces: the built-in namespace, the global namespace, and the local namespace.
16
How do you open and close a file in Python?
Ans.You can open a file using the open() function and close it using the close() method of the file object.
17
What is the purpose of the "with" statement in Python?
Ans.The "with" statement is used for resource management. It ensures that a file or resource is properly closed after it's no longer needed, even if an exception occurs.
18
How do you handle file reading and writing in Python?
Ans.File reading can be done using the read() or readlines() methods, while writing can be done using the write() or writelines() methods.
19
What is the purpose of the "self" parameter in Python class methods?
Ans.The "self" parameter is a reference to the instance of the class and is used to access its attributes and methods.
20
Explain the concept of method overriding in Python.
Ans.Method overriding is a feature that allows a subclass to provide a different implementation of a method that is already defined in its superclass.