0% found this document useful (0 votes)
4 views

Assignment 2

Python data types

Uploaded by

Pradeepth S Jain
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Assignment 2

Python data types

Uploaded by

Pradeepth S Jain
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

1. What does PEP stand for in Python, and what is its purpose in the Python community?

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:

o Use 4 spaces per indentation level.

o Limit lines to 79 characters.

o Use meaningful variable names.

o Follow conventions for imports: standard libraries first, followed by third-party


libraries, and local imports last.

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 Final: The PEP has been fully accepted and implemented.

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.

QUESTIONS ON LISTS AND TUPLES

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 Syntax: Lists use square brackets [ ], while tuples use parentheses ( ).

o Performance: Tuples are faster than lists due to their immutability.

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?

o List: my_list = [1, "hello", 3.5]

o Tuple: my_tuple = (1, "hello", 3.5)


Both can store different types of data (e.g., integers, strings, floats) 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.

o Fixed collections of constants.

o Ensuring integrity of data passed between functions.

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:

o Indexing: Access specific elements using an index, e.g., list[0] or tuple[0].

o Slicing: Extract a subset using a range, e.g., list[1:3] or tuple[1:3].


6. Can a tuple contain mutable elements, and if so, how does this affect the tuple's
immutability?
Yes, a tuple can contain mutable elements like lists or dictionaries. While the tuple itself
cannot be changed, the mutable elements inside it can be modified. For example:

t = (1, [2, 3])

t[1].append(4) # Modifies the list within the tuple

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?

o List to Tuple: my_tuple = tuple(my_list)

o Tuple to List: my_list = list(my_tuple)

9. What is list comprehension, and can it be applied to tuples?


List comprehension is a concise way to create lists based on existing iterables. Example:

squares = [x**2 for x in range(5)]

While you cannot directly use tuple comprehension, you can create a generator expression

or convert it to a tuple:

squares_tuple = tuple(x**2 for x in range(5))

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:

DIRECTIONS = ("NORTH", "SOUTH", "EAST", "WEST")

This ensures the values remain fixed and unmodifiable.

You might also like