1. What is Python and what makes it popular among programmers?
Ans:
Python is a high-level, general-purpose programming language notable for being simple and readable. Its support for object-oriented and functional programming, extensive libraries and cross-platform capabilities make it widely used in fields like data science, automation and web development.
2. What is PEP 8 and why should you follow it?
Ans:
PEP 8 is the standard guideline for writing clean and consistent Python code. Adhering to it improves readability, reduces errors and makes collaboration easier within teams. Following PEP 8 also ensures that code looks professional and is easier to maintain over time.
3. How do lists and tuples differ in Python?
Ans:
Lists are mutable, allowing modification, insertion or deletion of elements, while tuples are immutable and cannot be changed after creation. Lists are suitable when data may need updates, whereas tuples are preferred for fixed collections that should remain constant, with both supporting multiple data types.
4. How is memory managed in Python programs?
Ans:
Python uses automatic memory management through reference counting and a garbage collector. Objects that are no longer in use are cleared to free memory. The system organizes objects into generations, which helps optimize performance and manage memory efficiently.
5. What are the commonly used built-in data types in Python?
Ans:
Python offers numeric types like integers and floats, sequences such as lists and tuples, text types like strings, sets, dictionaries for mappings and Boolean values. These built-in types provide flexibility and efficiency for storing, manipulating and organizing data.
6. How does Python pass arguments to functions?
Ans:
Python functions receive arguments by object reference. Changes to mutable objects like lists inside a function affect the original object while immutable types such as strings remain unchanged This approach, known as call by reference, balances control and safety in functions.
7. What is the Global Interpreter Lock in Python?
Ans:
The GIL restricts Python to executing one thread at a time. While this simplifies memory management and prevents conflicts, it limits parallel processing for CPU-intensive tasks. For programs heavy on input/output, the GIL has little effect.
8. What is the difference between a module and a package in Python?
Ans:
One file containing Python code is called a module where as a package is a structured collection of modules. Modules promote code reuse and packages allow developers to organize larger projects in a clear, maintainable structure.
9. What are some popular built-in libraries in Python?
Ans:
Python provides built-in libraries for a wide range of tasks, such as mathematical calculations, date and time handling, system operations and data processing. These libraries simplify development and allow developers to perform complex tasks without extra dependencies.
10. How can you manage memory usage effectively in Python?
Ans:
Memory can be managed effectively by writing efficient code, reusing objects when possible and allowing garbage collection to free unused memory. Tools to track memory usage and memory-efficient programming practices help maintain smooth program performance.