1. How Do Lists and Tuples Differ in Python?
Ans:
Lists are mutable, meaning you can change, add or remove elements after creation. Tuples are immutable, so once defined their values cannot be changed. Lists use square brackets [], while tuples use parentheses (). Lists are preferred when you need flexibility, whereas tuples are used for fixed collections.
2. What Is List Comprehension in Python?
Ans:
One line of code can be used to generate the new lists in a clear manner with list comprehension. It combines a loop and an optional condition into one expression.
3. Why Is the self Keyword Used in Python Classes?
Ans:
The self keyword representing the instance of a class and is used to access variables and methods within the class. It must be the first parameter in instance methods. It allows each object to maintain its own data and behavior separately from other objects.
4. What Are Generators in Python?
Ans:
Generator are an iterable type that use the yield keyword to produce items one at a time. They are memory-efficient because they don't store the entire sequence in memory. You can use them to generate large data sets or infinite sequences on the fly.
5. How Is Exception Handling Done in Python?
Ans:
Python uses try, except and optionally finally blocks to handle errors.The except block captures any errors that arise after the code in the try block is run. This prevents program from crashing and allows graceful error handling.
6. What Are Modules and Packages in Python?
Ans:
A module is single Python file containing functions, classes or variables. A package is collection of modules organized in directories with an init .py file. They help you organize your code into reusable components and manage large projects efficiently.
7. When Should You Use the pass Statement in Python?
Ans:
The pass statement is used as placeholder when a statement is required syntactically but no action is needed. It’s commonly used in empty functions, classes or loops to prevent syntax errors during development.
8. How Do Shallow and Deep Copy Differ From One Another?
Ans:
A shallow copy creates the new object but does not copy nested objects inside it changes to nested data affect both copies. A deep copy creates completely independent copy of the original object and all its nested elements You can use the copy module for both.
9. What Are the Built-In Data Types in Python?
Ans:
Python provides several built-in data types to handle different kinds of data. Numeric types include int, float, and complex. For sequences, Python offers list, tuple, and range. Text is represented using the str type, while sets can be either set or frozenset. Dictionaries fall under the mapping type using dict. The Boolean type is bool, and binary data types include bytes, bytearray, and memoryview.
10. How Can You Convert a List Into a Dictionary in Python?
Ans:
You can convert a list into a dictionary by using the dict() function along with zip(). This involves pairing one list as keys and another list as values.