Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

Python_Interview_Questions_with_Answers

The document contains 100 Python interview questions and answers covering key features, memory management, data types, and differences between Python versions. It explains concepts such as deep vs shallow copy, mutable vs immutable types, and the use of *args and **kwargs. Additionally, it addresses exception handling, multi-threading, lambda functions, and file operations.

Uploaded by

atalparkec
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Python_Interview_Questions_with_Answers

The document contains 100 Python interview questions and answers covering key features, memory management, data types, and differences between Python versions. It explains concepts such as deep vs shallow copy, mutable vs immutable types, and the use of *args and **kwargs. Additionally, it addresses exception handling, multi-threading, lambda functions, and file operations.

Uploaded by

atalparkec
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

100 Python Interview Questions with Answers

1. What are Python's key features?

Answer: Python is an interpreted, dynamically-typed, and high-level programming language. It supports


object-oriented programming, has built-in data structures, automatic memory management, and is easy to
read and write.

2. What is the difference between deep copy and shallow copy?

Answer: A shallow copy creates a new object but inserts references to the original objects. A deep copy
creates a completely independent copy of the original object, including all nested objects.

3. Explain Python's memory management.

Answer: Python uses automatic memory management with garbage collection. It keeps track of object
references and removes unused objects to free memory.

4. What is PEP 8, and why is it important?

Answer: PEP 8 is Python's style guide that provides best practices for writing clean and readable code.
Following PEP 8 improves code consistency and maintainability.

5. What are Python's built-in data types?

Answer: Python has several built-in data types including int, float, str, list, tuple, set, dict, and bool.

6. Explain the difference between lists and tuples.

Answer: Lists are mutable (can be changed), while tuples are immutable (cannot be changed). Tuples are
faster than lists due to their immutability.

7. What is the difference between Python 2 and Python 3?

Answer: Python 3 introduced several improvements over Python 2, including better Unicode support, new
syntax (e.g., print() as a function), and improved integer division (// operator).

8. How does Python handle memory allocation?

Answer: Python manages memory using private heap space, which contains all Python objects and data
structures. The memory is handled by Python's built-in garbage collector.

9. What is the difference between mutable and immutable types?

Answer: Mutable types (e.g., lists, dictionaries) can be changed after creation, whereas immutable types
(e.g., strings, tuples) cannot be modified after they are created.

10. Explain the use of *args and **kwargs in Python.


Answer: *args allows a function to accept a variable number of positional arguments, while **kwargs allows a
function to accept a variable number of keyword arguments.

11. What is the difference between a shallow copy and a deep copy?

Answer: A shallow copy only copies references to objects, whereas a deep copy creates new objects
recursively, ensuring no references are shared.

12. How does Python's garbage collector work?

Answer: Python uses automatic garbage collection, primarily using reference counting and a cyclic garbage
collector to remove objects that are no longer needed.

13. What are Python's built-in functions?

Answer: Some built-in functions in Python include len(), type(), id(), sorted(), sum(), min(), max(), and abs().

14. How do you handle exceptions in Python?

Answer: Exceptions are handled using try-except blocks. Optionally, you can use finally to execute cleanup
code and else to run code if no exception occurs.

15. What is the difference between a module and a package?

Answer: A module is a single Python file, while a package is a collection of modules organized in a directory
with an __init__.py file.

16. How does Python implement multi-threading?

Answer: Python provides threading using the threading module, but due to the Global Interpreter Lock (GIL),
true parallel execution is better achieved with multiprocessing.

17. What is a lambda function in Python?

Answer: A lambda function is an anonymous function defined using the lambda keyword. Example: lambda x:
x * 2.

18. What are list comprehensions?

Answer: List comprehensions provide a concise way to create lists. Example: [x for x in range(10) if x % 2 ==
0].

19. Explain the difference between is and == in Python.

Answer: is checks for object identity (whether two variables reference the same object), while == checks for
equality (whether the values are the same).

20. How do you open and read a file in Python?

Answer: Using the open() function with 'r' mode: with open('file.txt', 'r') as file: data = file.read().

You might also like