0% found this document useful (0 votes)
16 views2 pages

Python Lists, Tuples, and Sets Quiz

The document is a question bank for a Python programming course, specifically focusing on lists, tuples, and sets. It includes various questions and programming tasks related to list operations, tuple characteristics, and set functionalities. The questions cover definitions, methods, comparisons, and practical programming exercises to reinforce understanding of these data structures in Python.

Uploaded by

chethannagaral
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views2 pages

Python Lists, Tuples, and Sets Quiz

The document is a question bank for a Python programming course, specifically focusing on lists, tuples, and sets. It includes various questions and programming tasks related to list operations, tuple characteristics, and set functionalities. The questions cover definitions, methods, comparisons, and practical programming exercises to reinforce understanding of these data structures in Python.

Uploaded by

chethannagaral
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

QUESTION BANK

Course Code: 24BTPHY204/24BTELY205 Semester: II

Course: Programming in Python

Module 3: Lists—Tuples—Sets

Lists

1. What is a list in Python? How is it different from an array?


2. Write a program to demonstrate indexing and slicing of a list.
3. Explain list concatenation and repetition with suitable examples.
4. Write a program to update elements in a list using indexing and slicing.

5. How does the in and not in operators work in a list? Illustrate with examples.
6. Compare two lists using comparison operators and explain the output.
7. Write a Python program to demonstrate membership and comparison operations on lists.

8. List and explain any five commonly used list methods with examples.
9. Write a program using append(), insert(), remove(), sort(), and reverse() on a list.

10. Write a function that accepts a list and returns the sum of all its elements.
11. What are multidimensional lists? Create a 2D list and access its elements.
12. Define List? Explain append (), insert () and remove () methods with examples.
13. How is a tuple different from a list and what function is used to convert list to tuple?
Explain.
14. Explain different ways to delete an element from a list with suitable Python
syntax and programming examples
15. Explain append() and index() functions with respect to lists in Python.
16. Write the output of the following python code >>>spam=’Hello worls!’
i) >>> spam[0] ii) >>>spam[4] iii) >>>spam[-1] iv) >>>spam[0:5]
v) >>> spam[:5] vi) >>>spam[7:]
17 explain following methods used in list with an example.
i. len() ii. sum() iii) max() iv) min
18. What is the difference between a multidimensional list and a nested list with
examples?
19 Explain the difference between a List and a Generator in Python.
20. What is the difference between remove() and pop() methods in lists?

Tuples

1. What is a tuple in Python? How is it different from a list?


2. Explain tuple indexing and slicing with examples.
3. Write a program to demonstrate multiple assignment using tuples.
4. Explain the different Tuple operations supported by python with suitable example.
5. Explain different tuple methods used in python with an example.
6. Write a program to find maximum and minimum element of tuple.
7. . Compare lists and tuples in terms of mutability, performance, and use cases.

Set

1. What is a set in Python? How do you create a set?


2. Write a program to create a set from a list and remove duplicates.
3. Demonstrate the use of add(), update(), remove(), discard(), and clear() methods on sets.
4. What is the difference between remove() and discard() in sets?
5. Write a program to perform union, intersection, and Symmetric Difference of two sets.
6. Explain the different set operations supported by python with suitable example.
7. Difference between a discard() method and a remove() method

Common questions

Powered by AI

The remove() method in Python lists is used to delete the first occurrence of a specified value, and it raises a ValueError if the value is not found in the list . This requires error handling to manage exceptions. On the other hand, the pop() method removes an element at a given index and returns it. If no index is specified, pop() deletes the last element of the list. An IndexError is raised when pop() is used on an empty list or if the specified index is out of range. Thus, using pop() demands careful index management to prevent runtime errors .

List comprehension in Python provides a concise way to create lists. It is generally faster and more memory-efficient than using traditional loops to create lists because it is optimized for Python's internal execution. For example, creating a list of squares using a loop might be done via `squares = [] for n in range(10): squares.append(n**2)`, whereas list comprehension simplifies this to `squares = [n**2 for n in range(10)]` . The performance gain comes from reducing overhead and eliminating the need for repeated calls to the append() function, thus making list comprehension faster and more concise .

Multidimensional lists and nested lists are often interchangeably used, but they slightly differ in context. A multidimensional list usually refers to a list of lists where each sub-list represents a dimension (e.g., a 2D grid). For example, `matrix = [[1, 2], [3, 4]]` is a 2D list where `matrix[row][col]` accesses elements . Conversely, nested lists emphasize the hierarchical relationship, where lists can be nested within others more arbitrarily, such as `nested = [1, [2, [3, 4]]]`, which lacks uniform dimension structure . While both involve embedded lists, multidimensional lists typically imply a structured grid-like form, useful in mathematical or data grid representations, whereas nested lists allow for arbitrary levels and structures.

Lists in Python are mutable, meaning their elements can be changed or updated. This makes them ideal for scenarios where data modification is frequent, but it comes with a performance cost since mutable data structures have overhead related to dynamic resizing and memory allocation . In contrast, tuples are immutable, which means they cannot be altered once created, leading to better performance in terms of iteration speed. Tuples are more suitable for fixed data collections or when the integrity of the data should be preserved . The decision between using lists or tuples often depends on the need for mutability versus the requirement for speed and memory efficiency.

Using `append()` on a list is generally O(1) since it adds an element to the end of a list without needing to reallocate space . The `insert()` method, however, is O(n) as it may require shifting elements to accommodate the new value. Similarly, `remove()` is O(n) because it needs to search the list to find and remove the specified element . The `sort()` method has a time complexity of O(n log n) due to the efficient sorting algorithms implemented in Python. Lastly, `reverse()` method is O(n) since it directly traverses the list to reverse its elements. Consequently, these methods can significantly impact performance, especially with large datasets, and should be selected based on the specific needs and limits of the operation .

The choice between using a list and a generator has significant implications for memory usage. Lists create and store all elements in memory, which can consume extensive memory for large datasets. For example, `nums = [x for x in range(1000000)]` generates all numbers at once, occupying substantial memory . In contrast, generators yield one item at a time, using memory more efficiently by not holding all items simultaneously. Using a generator, `nums = (x for x in range(1000000))`, doesn't generate values until they are needed, which is ideal for iterating large sequences without high memory consumption . This makes generators particularly useful for handling large datasets or streams in memory-constrained environments.

Tuple unpacking in Python allows for assigning the values of a tuple to corresponding variables in a single statement, leading to cleaner and more readable code. For example, consider a practical scenario of swapping two variables: `a, b = 5, 10`. Using tuple unpacking, swapping can be done as `a, b = b, a`, without requiring a temporary variable . This direct and succinct approach enhances code readability and simplicity by exploiting Python's support for multiple assignments and tuple unpacking capabilities .

In Python sets, the `add()` method adds a single item to a set, altering the set's size if the item isn't already present . The `update()` method can add multiple elements, taking an iterable and updating the set with each element in the iterable, which modifies the set's size and content . The `remove()` method deletes a specified item but raises a KeyError if the item is not found in the set, which necessitates exception handling . In contrast, `discard()` also removes a specified item but doesn't raise an error if the item doesn't exist. This can be advantageous for removing items without needing additional checks or exception handling .

Indexing and slicing in Python lists can be utilized to access and update sub-elements within lists, offering flexibility in data manipulation. For instance, in a list `names = ['Alice', 'Bob', 'Charlie', 'David']`, changing a slice can be done with `names[1:3] = ['Eve', 'Frank']`, resulting in `['Alice', 'Eve', 'Frank', 'David']` . This capability to change multiple elements simultaneously gives lists a significant advantage over tuples, which do not support this level of modification due to their immutability . Consequently, lists are more suited for scenarios demanding frequent updates, while tuples are preferred when data should remain constant.

In Python, membership operators like `in` and `not in` are used to check if an element is present in a list. This operation is crucial for conditions and loops where list elements guide logic paths. For example, `if 'apple' in fruits:` executes its block if 'apple' appears in the list `fruits` . While this provides a straightforward method to verify element presence, it involves linear search complexity—O(n)—which may be inefficient for large lists. Therefore, although useful, membership checks should be applied judiciously in performance-critical applications to avoid slow execution .

You might also like