1. Describe Python and the reasons for its popularity.
Ans:
Python is a widely used, beginner-friendly programming language known for its simplicity and versatility. It is applied in web development, data science, AI, and automation. Its clean syntax, extensive libraries, and strong community support make it one of the most popular languages today.
2. What is PEP 8 and why should we follow it?
Ans:
PEP 8 is Python’s official style guide that outlines best practices for writing neat and consistent code. Following it improves readability, teamwork, and ensures code is easier to maintain across projects.
3. How are lists and tuples different in Python?
Ans:
Lists are mutable, meaning their elements can be changed, added, or removed after creation. Tuples are immutable and cannot be altered once defined. Lists are ideal when data may change, while tuples are better for fixed data.
4. How does the Python handle memory?
Ans:
Python uses automatic memory management and garbage collection. It tracks active objects and frees unused memory, helping developers focus on coding without worrying about manual memory allocation.
5. What are the main types of data in Python?
Ans:
Python provides built-in data types such as integers, floats, strings, lists, tuples, sets, and dictionaries. These data types allow developers to manage different kinds of information effectively.
6. How are values passed into functions in Python?
Ans:
When calling a function, Python passes values either by reference or by creating a copy depending on the data type. Mutable objects (like lists) are passed by reference, while immutable ones (like numbers or strings) act as copies.
7. What is the Global Interpreter Lock in Python?
Ans:
The GIL is a mechanism that ensures only one thread executes Python bytecode at a time, even on multi-core processors. While it helps maintain memory safety, it can limit performance in CPU-heavy multithreaded tasks.
8. What is a module in Python and how is it different from a package?
Ans:
A module is a single Python file containing code, such as functions and variables. A package is a collection of modules organized in directories with an __init__.py file. Packages are useful for structuring larger applications.
9. What are some popular built in libraries in Python?
Ans:
Python includes many useful libraries like math for mathematical functions, datetime for date and time operations, and os for file system tasks. These built-ins reduce the need to write extra code for common operations.
10. How can memory usage be handled better in Python?
Ans:
Although Python manages memory automatically, developers can optimize usage by cleaning up unused variables, handling large data efficiently, and using generators for memory-heavy tasks. This ensures programs run smoothly.