1. What Makes Python a Popular Programming Language?
Ans:
Python is widely loved for its simple syntax, making it easy for beginners and efficient for professionals. It supports various fields like web development, AI, automation and data science. With a large standard library and strong community support, it speeds up development and makes troubleshooting easier.
2. How Does Python Manage Memory Internally?
Ans:
Python handles memory using a private heap where all objects and data are stored. It also includes a built in garbage collector automatically removes unused objects. This process reduces memory leaks and simplifies memory management for developers.
3. What Do Python Namespaces Mean?
Ans:
A namespace in Python is like a container that stores names of variables, functions and objects to avoid confusion. Python uses different types such as local, global and built-in namespaces to manage how names are accessed and prevent conflicts in the code.
4. What is PEP 8 and Why is it Used?
Ans:
The approved style guide for PEP 8 is writing Python code. It defines best practices for naming, spacing, line length and formatting. By following PEP 8, developers can write cleaner, more readable code that’s easier to maintain and collaborate on.
5. How Are Lists and Tuples Different in Python?
Ans:
Lists are mutable, meaning you can change, add or remove items after creation. Tuples, on the other hand, are immutable and fixed. Use lists when data might change and tuples when storing constant or secure data.
6. What is a Decorator in Python?
Ans:
A decorator is a function that adds new behavior to another function without changing its code. It’s often used for logging, authentication or timing. Decorators help keep code clean and reusable by wrapping functions with extra features.
7. What Types of Inheritance Does Python Support?
Ans:
Python supports several inheritance types including single, multiple, multilevel, hierarchical and hybrid. These allow classes to inherit features from one or more parent classes, making code easier to organize and reuse in larger applications.
8. How is Multithreading Done in Python?
Ans:
Python uses the threading module to perform multithreading, allowing multiple tasks to run at once. It's best for I/O-bound tasks like reading files or network operations. The Global Interpreter Lock (GIL) is the reason it's less effective for CPU-heavy tasks.
9. What’s the Difference Between a Shallow Copy and a Deep Copy?
Ans:
A new item is produced by a shallow copy, however keeps references to inner objects, so changes in nested data affect both. A deep copy creates a fully independent object with its own copies of nested elements, avoiding shared changes.
10. How Do You Build Stacks and Queues in Python?
Ans:
Stacks can be made using lists with .append() to add and .pop() to remove items (LIFO). For queues, collections.deque is preferred, using .append() to add and .popleft() to remove items in FIFO order.