Assignment 2
Assignment 2
PEP stands for Python Enhancement Proposal. It is a design document providing information
to the Python community or describing a new feature for Python, its processes, or
environment. PEPs are a way to propose, discuss, and document new ideas, ensuring
transparency and collaboration within the Python community.
2. Name a significant PEP that introduced a new feature to Python and describe its impact.
PEP 572 introduced the walrus operator (:=) in Python 3.8. This operator allows assignment
expressions, enabling developers to assign and use a value within a single expression. It
improved readability and efficiency, particularly in loops and comprehensions.
3. How did PEP 8 influence Python coding standards, and what are its key recommendations?
PEP 8 is the Style Guide for Python Code, defining conventions for writing readable and
consistent code. Key recommendations include:
4. Which PEP introduced type hinting in Python, and how does it improve code quality?
PEP 484 introduced type hinting in Python 3.5. It improves code quality by allowing
developers to annotate function signatures and variables with type information, aiding in
static analysis, reducing runtime errors, and improving code readability.
5. Explain the role of PEP 572 in Python and the controversy surrounding the := operator.
PEP 572 introduced the walrus operator (:=), which combines assignment and expression
evaluation. The controversy stemmed from concerns about readability and complexity, with
critics arguing that it might confuse new Python users and lead to less clear code.
6. What is the difference between a PEP with a "Final" status and one that is "Provisional"?
o Provisional: The PEP has been accepted but may undergo changes based on user
feedback or further development.
7. How did PEP 484 enhance Python’s functionality for developers working with large
codebases?
PEP 484 introduced type hinting, making it easier to understand large codebases by explicitly
stating expected data types. This aids in collaboration, reduces errors, and integrates well
with tools like linters and IDEs for better code analysis.
8. Which PEP introduced f-strings in Python, and how do they differ from older string
formatting methods?
PEP 498 introduced f-strings in Python 3.6. F-strings are concise and efficient, allowing inline
expressions with {}. They differ from older methods like .format() and % formatting by being
more readable and faster in execution.
9. What is the primary purpose of PEP 557, and how does it simplify working with classes?
PEP 557 introduced data classes in Python 3.7. Data classes simplify the creation of classes
for storing data by automatically generating methods like __init__, __repr__, and __eq__,
reducing boilerplate code.
10. Can you describe the changes introduced by PEP 616 regarding string methods?
PEP 616, introduced in Python 3.9, added support for removing prefixes and suffixes from
strings with the removeprefix() and removesuffix() methods. These methods improve
readability and eliminate common boilerplate code for trimming strings.
1. What are the key differences between a list and a tuple in Python?
o Mutability: Lists are mutable (can be changed), while tuples are immutable (cannot
be changed after creation).
o Usage: Lists are used when you need a collection of items that can change, while
tuples are used for fixed collections.
2. How do you create a list and a tuple, and can they store different types of data in a single
structure?
3. Explain how the immutability of tuples affects their use cases compared to lists.
The immutability of tuples makes them suitable for use cases where data should not
change, such as:
o Keys in dictionaries.
4. What are the main methods available for lists that are not applicable to tuples?
Lists have methods like append(), extend(), insert(), remove(), pop(), and clear() which
modify the list. These are not available for tuples because tuples are immutable.
5. How does indexing and slicing work for both lists and tuples in Python?
Indexing and slicing work the same way for both lists and tuples:
7. What is the difference between deep copying and shallow copying for a list?
o Shallow Copy: Copies the outer structure, but inner elements still reference the
original. Changes to mutable elements inside the copy affect the original.
Example: copy = original.copy()
o Deep Copy: Creates a completely independent copy, including all nested elements.
Changes to the copy do not affect the original.
Example: from copy import deepcopy; copy = deepcopy(original)
8. How can you convert a list to a tuple and vice versa in Python?
While you cannot directly use tuple comprehension, you can create a generator expression
or convert it to a tuple:
10. Provide an example where using a tuple would be more beneficial than a list.
When defining a set of constants, tuples are more beneficial as they are immutable: