1. How are lists and tuples different in Python?
Ans:
The primary distinction between lists and tuples, which are both used in Python to store numerous items, is their capacity for change. Lists are flexible, meaning you can add, remove or modify items even after the list is created. Tuples, on the other hand are fixed and cannot be changed once they are made. This makes tuples more secure and faster in situations where data should remain constant.
2. What does list comprehension mean?
Ans:
List comprehension is the compact way of creating lists in Python. Instead of writing long code with multiple lines, you can build a new list from an existing one using a single, readable line. It is often used when you need to perform an action on each item in a collection and gather the results into a new list quickly and cleanly.
3. Why do we use the word 'self' in Python?
Ans:
Python classes use the term "self" to refer to the instance of the class. It makes it easier to determine which object is being utilized or mentioned at the moment. Python wouldn't be able to tell different instances of the same class apart without "self," particularly when it comes to accessing or changing properties or methods.
4. What are generators in Python?
Ans:
Generators are a type of function in Python that produce values one at a time, only when they are needed. This is useful when working with large amounts of data as it saves memory. Instead of storing every value in memory, a generator yields one value at a time and continues when asked for the next, making it more efficient for specific tasks.
5. How does Python handle errors or exceptions?
Ans:
Python manages errors using system called exception handling. When something goes wrong during the program execution instead of crashing, Python allows you to catch the error using a special structure and handle it gracefully. This could include showing the custom message or taking an alternate action, making the program more robust and user-friendly.
6. What are modules and packages in Python?
Ans:
Packages and modules are tools for better organizing Python code. A module is just a file that includes Python code that may be used in other projects, like variables or functions. A package consists of a grouping of similar modules in a folder. Reusing code and maintaining projects' cleanliness and manageability depend on both.
7. Why is the 'pass' statement used in Python?
Ans:
The ‘pass’ statement in Python is placeholder. It is used when a statement is required syntactically, but you don’t want any action to be taken. This is especially helpful during development when you're planning the structure of your code but haven’t written the full logic yet. It prevents errors and allows the program to run smoothly while you finish building the rest.
8. What is the difference between a shallow copy and a deep copy?
Ans:
An item is created by a shallow copy, but the deeper components within it are not replicated. Changes to the inner items impact both copies because it just replicates references to them. On the other hand, a deep copy not only makes a new object but also duplicates every element within it. Because of this the new item is completely independent of the original.
9. What are some common built-in data types in Python?
Ans:
Python provides several built-in data types to help with different types of information. These include numbers for math operations, strings for text, lists and tuples for ordered collections, dictionaries for key-value pairs, sets for unique elements and booleans for true or false values. These data types make Python versatile and easy to use in various applications.
10. How can you turn a list into a dictionary in Python?
Ans:
By organizing a list entries into pairs, where one serves as the key and the other as the value, a list can be turned into a dictionary. This is useful when you want to organize data for faster lookups or better structure. This conversion can be done using simple logic or tools provided by Python for such tasks.