Get the most accurate TN Board Solutions for Class 12 Computer Science Chapter 09 Lists Tuples Sets and Dictionary here. Updated for the 2026-27 academic session, these solutions are based on the latest TN Board textbooks for Class 12 Computer Science. Our expert-created answers for Class 12 Computer Science are available for free download in PDF format.
Detailed Chapter 09 Lists Tuples Sets and Dictionary TN Board Solutions for Class 12 Computer Science
For Class 12 students, solving TN Board textbook questions is the most effective way to build a strong conceptual foundation. Our Class 12 Computer Science solutions follow a detailed, step-by-step approach to ensure you understand the logic behind every answer. Practicing these Chapter 09 Lists Tuples Sets and Dictionary solutions will improve your exam performance.
Class 12 Computer Science Chapter 09 Lists Tuples Sets and Dictionary TN Board Solutions PDF
I. Choose The Best Answer (1 Marks)
Question 1. Pick odd one in connection with collection data type
(a) List
(b) Dictionary
(c) Loop
(d) Tuple
Answer: (c) Loop
In simple words: Among List, Dictionary, and Tuple, which are ways to store many items together, 'Loop' is different because it is a command that repeats actions, not a way to store data.
๐ฏ Exam Tip: Knowing the basic Python data structures like lists, tuples, sets, and dictionaries is crucial. Loops are control flow statements, not data types.
Question 2. Let list l=[2,4,6,8,10], then print(Listl[-2]) will result in
(a) 10
(b) 8
(c) 4
(d) 6
Answer: (b) 8
In simple words: In Python, negative indexing counts from the end of the list. So, -1 means the last item, and -2 means the second to last item, which in this list is 8.
๐ฏ Exam Tip: Remember that negative indexing in Python starts from -1 for the last element, -2 for the second to last, and so on, which is very useful for accessing elements from the end of a list without knowing its length.
Question 3. Which of the following function is used to count the number of elements in a list?
(a) count()
(b) find()
(c) len()
(d) index()
Answer: (c) len()
In simple words: The `len()` function is a common Python command that tells you how many items are inside a list, tuple, or string. It directly gives the length.
๐ฏ Exam Tip: The `len()` function is used for getting the number of items in any sequence or collection in Python, while `count()` is a method of lists/strings to count occurrences of a specific item.
Question 4. If List= [10,20,30,40,50] then List[2]=35 will result ~
(a) [35,10,20,30,40,50]
(b) [10,20,30,40,50,35]
(c) [10,20,35,40,50]
(d) [10,35,30,40,50]
Answer: (c) [10,20,35,40,50]
In simple words: In a list, `List[2]` points to the item at index 2 (the third item, as counting starts from 0). Changing `List[2]=35` replaces the original item (30) with 35, leaving the rest of the list the same.
๐ฏ Exam Tip: Remember that list indexing in Python starts from 0. Assigning a new value to an index like `List[2] = 35` directly modifies the element at that position in the list.
Question 5. If List= [17,23,41,10] then List.append (32) will result
(a) [32,17,23,41,10]
(b) [17,23,41,10,32]
(c) [10,17,23,32,41]
(d) [41,32,23,17,10]
Answer: (b) [17,23,41,10,32]
In simple words: The `append()` function in Python adds a new item to the very end of a list. So, 32 gets added after 10.
๐ฏ Exam Tip: The `append()` method always adds an element as a single item to the end of a list, increasing its length by one.
Question 6. Which of the following Python function can be used to add more than one element within an existing list?
(a) append ()
(b) append_more()
(c) extend ()
(d) more()
Answer: (c) extend()
In simple words: The `extend()` function is used to add all the items from another list or any iterable to the end of the current list. This is different from `append()`, which adds only one item at a time.
๐ฏ Exam Tip: Use `extend()` when you want to add multiple elements from another iterable (like another list or a tuple) to your current list. Use `append()` for adding a single element.
Question 7. What will be the result of the following Python code?
`S=[x**2 for x in range(5)]`
`print(S)`
(a) [0,1,4,9,16,25]
(b) [0,1,4,9,16]
(c) [1,4,9,16,25]
(d) [1,4,9,16,25,36]
Answer: (b) [0,1,4,9,16]
In simple words: The `range(5)` creates numbers from 0 to 4. The code then squares each of these numbers (`x**2`) and puts them into a new list called S. This is a compact way to create a list using a loop.
๐ฏ Exam Tip: List comprehensions provide a concise way to create lists. Remember that `range(n)` generates numbers from 0 up to (but not including) n.
Question 8. What is the use of type() function in python?
(a) To create a Tuple
(b) To know the type of an element in the tuple
(c) To know the data type of python object
(d) To create a list.
Answer: (c) To know the data type of python object
In simple words: The `type()` function tells you what kind of data an object is, like if it's a number, text, or a list. It helps you understand how to work with that data.
๐ฏ Exam Tip: The `type()` function is a fundamental built-in function in Python for runtime inspection of object types, which is useful for debugging and type-checking.
Question 9. Which of the following statement is not correct?
(a) A list is mutable
(b) A tuple is immutable.
(c) The append () function is used to add an element.
(d) The extend () function is used in tuple to add elements in a list.
Answer: (d) The extend() function is used in tuple to add elements in a list.
In simple words: The `extend()` function is used for lists, not for tuples. Tuples are "immutable," which means you cannot add or change their elements after they are created.
๐ฏ Exam Tip: Understand the key difference between mutable (changeable) and immutable (unchangeable) data types. Lists are mutable, while tuples are immutable, meaning their elements cannot be added, removed, or changed after creation.
Question 10. Let setA={3,6,9}, setB={1,3,9}. What will be the result of the following snippet?
`print(setA | setB)`
(a) {3,6,9,1,3,9}
(b) {0,1,4,9,16}
(c) {1}
(d) {1,3,6,9}
Answer: (d) {1,3,6,9}
In simple words: The `|` symbol performs a "union" operation for sets, which means it combines all unique elements from both sets. Since sets do not allow duplicate items, the numbers 3 and 9 appear only once.
๐ฏ Exam Tip: Remember that set union (`|`) returns all unique elements present in either set. Duplicate elements are only listed once in the resulting set.
Question 11. Which of the following set operation includes all the elements that are in two sets but not the one that are common to two sets?
(a) Symmetric difference
(b) Difference
(c) Intersection
(d) Union
Answer: (a) Symmetric difference
In simple words: Symmetric difference gives you all items that are unique to each set, but it leaves out any items that both sets share. It's like finding what's "different" between them, not just what's in one but not the other.
๐ฏ Exam Tip: The symmetric difference (`^` operator) is a key set operation that helps identify elements unique to each set, excluding their common members. Visualizing this with a Venn diagram can be helpful.
Question 12. The keys in Python, the dictionary is specified by
(a) =
(b) ^
(c) +
(d) :
Answer: (d) :
In simple words: In Python dictionaries, a colon (:) is always used to separate a key from its value. This helps Python know which part is the unique identifier and which part is the data.
๐ฏ Exam Tip: In Python dictionaries, key-value pairs are defined as `key: value`. The colon is a fundamental syntax element for dictionaries.
II. Answer The Following Questions (2 Marks)
Question 1. What is List in Python?
Answer: A list in Python is an ordered collection of values, similar to strings, but it can hold different types of data. These values are enclosed within square brackets `[]`, and each value inside a list is called an element. Lists are "mutable," meaning you can change, add, or remove elements after the list is created.
In simple words: A Python list is like a shopping list where you can keep many items, even different kinds, in a specific order. You can easily add, remove, or change items on this list.
๐ฏ Exam Tip: Remember that lists are ordered, mutable, and can store heterogeneous data types. Mentioning square brackets `[]` and "mutable" is crucial for a complete definition.
Question 2. How will you access the list elements in reverse order?
Answer: Python lets you access list elements in reverse order using negative indexing. This means you count from the end of the list. The last element has an index of -1, the second to last is -2, and so on. This method is called Reverse Indexing.
Example: If `Age = [15,20,29,45,60]`, then `Age[-1]` would give 60, and `Age[-2]` would give 45. Using negative indexes makes it easy to get items from the end without knowing the list's total length.
In simple words: You can get items from the end of a list by using negative numbers as index. For example, -1 means the last item, -2 means the second to last item, and so on.
๐ฏ Exam Tip: Clearly state that negative indexing starts with -1 for the last element and decreases for elements further to the left. Providing a simple example like `list_name[-1]` is effective.
Question 3. What will be the value of x in the following python code?
`List1 = [2, 4, 6, [1, 3, 5]]`
`x=len(List1)`
`Ans: 4`
Answer: The value of x will be 4. This is because the `len()` function counts the top-level elements in the list. Even though `[1, 3, 5]` is itself a list, it is treated as a single element at index 3 within `List1`. The elements are 2, 4, 6, and `[1, 3, 5]`, making a total of four elements.
In simple words: The `len()` command counts how many main items are in the list. Here, the small list `[1, 3, 5]` is counted as one item, so the total count is 4.
๐ฏ Exam Tip: When `len()` is applied to a list containing nested lists, it counts the nested list as a single element, not its individual components. This is a common point of confusion.
Question 4. Differentiate del with remove() the function of List.
Answer: The `del` statement and the `remove()` function are both used to delete elements from a list, but they work differently. `del` is a statement that removes elements based on their index or slice, and it can also delete the entire list object. `remove()`, on the other hand, is a list method that removes the first occurrence of a specified value from the list, without needing its index. The `remove()` method is useful when you know the value but not its position.
| del statement | remove() function |
|---|---|
| 1. `del` is used to delete elements when their index is known. | `remove()` is used to delete elements if its index is unknown, but its value is known. |
| 2. The `del` command can also be used to delete the entire list. | The `remove()` function cannot delete the entire list structure, only elements. |
In simple words: `del` removes items from a list using their position number or can delete the whole list. `remove()` takes out the first item that matches a specific value you give it, not by its position.
๐ฏ Exam Tip: Highlight that `del` works by index/slice and can delete the entire list, while `remove()` works by value (first occurrence) and doesn't delete the list object itself. This distinction is key.
Question 5. Write the syntax of creating a Tuple with n number of elements.
Answer: A tuple is created by listing elements separated by commas, enclosed within parentheses. For a tuple with 'n' number of elements, the general syntax is to assign a name to the tuple, then list its elements inside round brackets. These elements can be of different types. You can also create a tuple without parentheses for simple cases, but it's good practice to use them for clarity.
`# Tuple with n number elements:`
`Tuple_Name = (E1, E2, E3, ........ En)`
`# Elements of a tuple without parenthesis:`
`Tuple_Name = E1, E2, E3, ........ En`
In simple words: To make a tuple, you give it a name and then put a list of items inside round brackets, separated by commas. You can even skip the brackets if you just list items with commas.
๐ฏ Exam Tip: Emphasize the use of parentheses `()` for defining tuples and the comma-separated elements. Mentioning the immutability of tuples is also a good point.
Question 6. What is set in Python?
Answer: In Python, a set is a collection data type that stores items without any specific order and does not allow duplicate elements. This means every item in a set is unique. Sets are mutable, so you can add or remove elements after creation. They are very useful for operations like finding common items, differences, or unique items between collections.
`โข Set is another type of collection data type.`
`โข A Set is a mutable and unordered collection of elements without duplicates.`
In simple words: A set in Python is like a bag of unique items that has no specific order. You cannot have the same item twice in a set.
๐ฏ Exam Tip: The two defining characteristics of a set are that it is an "unordered collection" and contains "no duplicate elements." These are the keywords an examiner looks for.
III. Answer The Following Questions (3 Marks)
Question 1. What are the advantages of Tuples over a list?
Answer: Tuples offer several advantages over lists, mainly due to their immutability (unchangeable nature). Since tuples cannot be changed after creation, they are generally faster to iterate over than lists. This makes them more efficient for storing fixed collections of data. They also provide a sense of data integrity, as once defined, the contents are guaranteed not to change. Furthermore, tuples can be used as keys in dictionaries, unlike lists, because dictionary keys must be immutable.
`โข The elements of a list are changeable (mutable) whereas the elements of a tuple are unchangeable (immutable), this is the key difference between tuples and list.`
`โข The elements of a list are enclosed within square brackets. But, the elements of a tuple are enclosed by parentheses.`
`โข Iterating tuples is faster than a list.`
In simple words: Tuples are better when you have a collection of items that should never change, because they are faster and safer. Lists are better when you need to change items often.
๐ฏ Exam Tip: Focus on the core advantage: immutability. This leads to benefits like faster iteration, data integrity, and usability as dictionary keys or set elements.
Question 2. Write a short note about sort().
Answer: The `sort()` function is a built-in method for Python lists that rearranges the elements of the list in a specific order, usually numerical or alphabetical. By default, it sorts in ascending order. You can change this behavior by setting the `reverse` argument to `True` for descending order. The `sort()` method directly modifies the original list, meaning it does not create a new sorted list.
`Function: Sort ()`
`Syntax: List.sort( reverse=True | False, key=myFunc)`
`โข Both arguments are optional.`
`โข If reverse is set as True, list sorting is in descending order.`
`โข Ascending is default.`
`Description:`
`Sort () function sorts the element in list`
`Example:`
`MyList=['Thilothamma', 'Tharani', 'Anitha', 'SaiSree', 'Lavanya']`
`MyList.sort()`
`print(MyList)`
`Output:`
`['Anitha', 'Lavanya', 'SaiSree', 'Tharani', 'Thilothamma']`
`MyList.sort(reverse=True)`
`print(MyList)`
`Output:`
`['Thilothamma', 'Tharani', 'SaiSree', 'Lavanya', 'Anitha']`
In simple words: The `sort()` command puts items in a list in order, like from smallest to largest number or A to Z. It changes the list itself. You can tell it to sort backward if you want.
๐ฏ Exam Tip: Crucially, mention that `sort()` modifies the list in-place and returns `None`. Contrast this with `sorted()`, which returns a new sorted list without changing the original.
Question 3. What will be the output of the following code?
`list = [2**x for x in range(5)]`
`print(list)`
`Output: [1,2,4,8,16]`
Answer: The Python code uses a list comprehension to create a list. The `range(5)` generates numbers from 0 to 4 (i.e., 0, 1, 2, 3, 4). For each of these numbers `x`, the expression `2**x` calculates 2 raised to the power of `x`. This results in the powers of 2 for these numbers: \( 2^0=1 \), \( 2^1=2 \), \( 2^2=4 \), \( 2^3=8 \), and \( 2^4=16 \). So, the output will be `[1, 2, 4, 8, 16]`.
In simple words: The code makes a list by taking numbers from 0 to 4. For each number, it calculates 2 multiplied by itself that many times. So, the list becomes 1, 2, 4, 8, and 16.
๐ฏ Exam Tip: Understand how `range(n)` works (generates numbers from 0 to n-1) and how list comprehensions apply an expression to each generated number to form a new list.
Question 4. Explain the difference between del and clear() in the dictionary with an example.
Answer: In Python dictionaries, both `del` and `clear()` are used for removing elements, but they have distinct functionalities. The `del` keyword is used to delete specific key-value pairs from a dictionary or to delete the entire dictionary object itself from memory. On the other hand, the `clear()` method is a dictionary function that removes all key-value pairs from a dictionary, leaving it empty, but the dictionary object itself still exists. The `clear()` method only empties the dictionary, while `del` can remove the dictionary entirely.
| del | clear() |
|---|---|
| 1. `del` keyword is used to delete a particular element of a dictionary. | The `clear()` function is used to delete all the elements in a dictionary. |
| 2. `del` keyword can be used to remove the dictionary completely. | The `clear()` function is used to delete all the elements, but the dictionary structure remains empty. |
| 3. Example: `del Dict['MarkT']` | Example: `Dict.clear()` |
In simple words: `del` can remove a specific item from a dictionary or erase the whole dictionary from the computer's memory. `clear()` only removes all the items inside the dictionary, making it empty, but the dictionary itself is still there, just blank.
๐ฏ Exam Tip: The main distinction is that `clear()` empties the dictionary while `del` can remove the dictionary object entirely. Also, `del` can target individual key-value pairs, while `clear()` always affects the whole dictionary content.
Question 5. List out the set operations supported by python.
Answer: Python supports several common set operations that allow you to combine or compare sets. These operations are fundamental for working with collections of unique items. The main set operations include Union, Intersection, Difference, and Symmetric Difference. Each operation has a specific mathematical purpose and corresponding Python syntax, often with both an operator and a method. These tools help manage unique collections efficiently.
`The python supports the set operations such as Union, Intersection, Difference, and Symmetric difference.`
`Union:`
`โข The union includes all elements from two or more sets.`
`โข In python, the operator | (pipeline) is used to the union of two sets.`
`โข The function union() is also used to join two sets in python.`
`Intersection:`
`โข Intersection includes the common elements in two sets.`
`โข The operator & is used to intersect two sets in python.`
`โข The function intersection() is also used to intersect two sets in python.`
`Difference:`
`โข The difference includes all elements that are in the first set (say set A) but not in the second set (say set B).`
`โข The minus (-) operator is used to difference set operation in python.`
`โข The function difference() is also used to difference operation.`
`Symmetric difference:`
`โข The symmetric difference includes all the elements that are in two sets (say sets A and B) but not the one that are common to two sets.`
`โข The caret (^) operator is used to symmetric difference set operation in python.`
`โข The function symmetric_difference() is also used to do the same operation.`
In simple words: Python can combine sets in many ways: 'Union' takes everything from both sets. 'Intersection' finds what they share. 'Difference' shows what's in one set but not the other. 'Symmetric difference' shows what's unique to each set, ignoring anything they share.
๐ฏ Exam Tip: For each set operation, clearly state its purpose (what elements it includes) and provide both the operator symbol (e.g., `|`, `&`, `-`, `^`) and the corresponding method (e.g., `union()`, `intersection()`, `difference()`, `symmetric_difference()`).
Question 6. What are the differences between List and Dictionary?
Answer: Lists and dictionaries are both fundamental data structures in Python, but they differ in how they store, order, and access elements. A list is an ordered collection of elements where each element is accessed by its numerical index, starting from 0. In contrast, a dictionary is an unordered collection of key-value pairs, where each value is accessed using its unique key. Dictionaries are optimized for quick lookups based on these keys, which makes them very efficient for retrieving specific data.
| List | Dictionary |
|---|---|
| List is an ordered set of elements. | Dictionary is a data structure that is used for matching one element (Key) with another (Value). |
| The index values can be used to access a particular element. | In dictionary, key represents an index and key may be a number of a string. |
| Lists are used to look up a value by its position. | Dictionary is used to take one value (key) and look up another value (its associated value). |
In simple words: A list keeps items in order, and you find them by their number (like item #1, #2). A dictionary keeps items as pairs (like a word and its meaning), and you find them by their name (the "key"), not their number.
๐ฏ Exam Tip: Focus on the fundamental differences: Lists are ordered and indexed by numbers, while dictionaries are unordered and indexed by unique keys. Also, mention that dictionaries store key-value pairs.
IV. Answer The Following Questions (5 Marks)
Question 1. What the different ways to insert an element in a list. Explain with suitable example.
Answer: In Python, you can insert elements into a list in several ways, depending on where you want the new element to go. The `append()` function adds an element to the very end of the list. If you need to insert an element at a specific position, the `insert()` function is used, which takes two arguments: the index where the element should be placed and the element itself. When `insert()` is used, existing elements from that position onwards are shifted one position to the right to make space for the new element. You can also use slicing to insert elements into a list by assigning a new list to a slice.
`append()` function in Python is used to add more elements to a list. But, it includes elements at the end of a list. If you want to include an element at your desired position, `insert()` function is used to insert an element at any position of a list.
`Syntax:`
`List.insert (position index, element)`
`Example:`
`>>> MyList=[34,98,47, 'Kannan', 'Gowrisankar', 'Lenin', 'Sreenivasan' ]`
`>>> print(MyList)`
`[34, 98, 47, 'Kannan', 'Gowrisankar', 'Lenin', 'Sreenivasan']`
`>>> MyList.insert(3, 'Ramakrishnan')`
`>>> print(MyList)`
`[34, 98, 47, 'Ramakrishnan', 'Kannan', 'Gowrisankar', 'Lenin', 'Sreenivasan']`
`In the above example, insert() function inserts a new element 'Ramakrishnan' at the index value 3, i.e. at the 4th position. While inserting a new element in between the existing elements, at a particular location, the existing elements shifts one position to the right.`
In simple words: You can add items to a list in different ways. `append()` puts a new item at the end. `insert()` lets you choose exactly where to put a new item, and it moves other items over to make space.
๐ฏ Exam Tip: Clearly explain `append()` for adding to the end and `insert(index, element)` for adding at a specific position, emphasizing how `insert()` shifts existing elements.
Question 2. What is the purpose of range()? Explain with an example.
Answer: The `range()` function in Python is primarily used to generate a sequence of numbers. It is especially useful when you need to loop a certain number of times, or when you want to create a list with a series of numerical values. The `range()` function can take up to three arguments: a `start` value, an `end` value (which is exclusive, meaning the sequence goes up to, but does not include, this number), and a `step` value to define the increment between numbers. This function is very memory-efficient as it generates numbers on demand, rather than creating a full list in memory immediately.
`(i) The range() is a function used to generate a series of values in Python. Using the range() function, you can create list with series of values. The range() function has three arguments.`
`Syntax of range() function:`
`range (start value, end value, step value)`
`where,`
`โข start value โ beginning value of series. Zero is the default beginning value.`
`โข end value โ the upper limit of series. Python takes the ending value as upper limit exclusive.`
`โข step value โ It is an optional argument, which is used to generate different intervals of values.`
`Example: Generating whole numbers upto 10`
`for x in range (1, 11):`
`print(x)`
`Output:`
`1`
`2`
`3`
`4`
`5`
`6`
`7`
`8`
`9`
`10`
`(ii) Creating a list with series of values`
`Using the range() function, you can create a list with series of values. To convert the result of range() function into list, we need one more function called list(). The list() function makes the result of range() as a list.`
`Syntax:`
`List_Varibale = list (range ())`
`Note`
`The list() function is all so used to create list in python.`
`Example`
`>>> Even_List = list(range(2,11,2))`
`>>> print(Even_List)`
`[2, 4, 6, 8, 10]`
`In the above code, list() function takes the result of range() as Even List elements. Thus, Even_List list has the elements of first five even numbers.`
`(iii) We can create any series of values using the range() function. The following example explains how to create a list with squares of the first 10 natural numbers.`
`Example: Generating squares of first 10 natural numbers`
`squares = []`
`for x in range(1,11):`
`s = x ** 2`
`squares.append(s)`
`print (squares)`
`Output`
`[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]`
In simple words: The `range()` function helps you make a sequence of numbers easily. You can tell it where to start, where to stop (but not include the stop number), and how much to jump between numbers. It's often used in loops to repeat actions a certain number of times.
๐ฏ Exam Tip: Explain that `range()` generates numbers, not a list, and is often used with loops. Clearly define its three arguments: `start` (inclusive), `stop` (exclusive), and `step`.
Question 3. What is a nested tuple? Explain with an example.'
Answer: A nested tuple is a tuple that contains other tuples as its elements. This means you can have a tuple inside another tuple, creating a multi-level structure. Each inner tuple acts as a single element within the outer tuple. This concept is similar to nested lists and is useful for organizing related data in a structured, unchangeable way. To access elements within nested tuples, you can use multiple indices, one for each level of nesting.
`โข In Python, a tuple can be defined inside another tuple; called a Nested tuple.`
`โข In a nested tuple, each tuple is considered as an element.`
`โข The for loop will be useful to access all the elements in a nested tuple.`
`Example:`
`Toppers = (("Vinodiniโ, โXII-Pโ, 98.7),`
`('Soundarya", โXII-Hโ, 97.5),`
`("Tharani", "XII-Pโ, 95.3),`
`("Saisri", "XII-G", 93.8))`
`for j in Toppers:`
` print(j)`
`Output:`
`('Vinodini', 'XII-P', 98.7)`
`('Soundarya', 'XII-H', 97.5)`
`('Tharani', 'XII-P', 95.3)`
`('Saisri', 'XII-G', 93.8)`
In simple words: A nested tuple is a tuple that holds other tuples inside it, like a box containing smaller boxes. Each inner tuple is treated as one item in the main tuple.
๐ฏ Exam Tip: Define a nested tuple as a tuple containing other tuples. Emphasize that each inner tuple acts as a single element of the outer tuple and provide a clear, simple example to illustrate the structure.
Question 4. Explain the different set operations supported by Python with examples.
Answer: Python supports several set operations like Union, Intersection, Difference, and Symmetric Difference. These operations help combine or compare elements between different sets. Each operation has a specific purpose for managing set data.
Union:
The union operation combines all unique elements from two or more sets.
In Python, the vertical bar `|` operator is used for finding the union of two sets.
The `union()` function can also be used to join two sets.
Example of Union:
Program to join (Union) two sets using the union operator and the `union()` function:
`set_A={2,4,6,8}`
`set_B={'A', 'B', 'C', 'D'}`
`U_set=set_A | set_B`
`print(U_set)`
`set_U=set_A.union(set_B)`
`print(set_U)`
Output:
`{'A', 'B', 'C', 'D', 2, 4, 6, 8}` (Order can vary in sets)
Intersection:
Intersection includes all common elements found in two sets.
The `&` operator is used to find the intersection of two sets in Python.
The `intersection()` function can also be used to find the intersection.
Example of Intersection:
Program to find the intersection of two sets using the intersection operator and the `intersection()` function:
`set_A={'A', 2,4, 'D'}`
`set_B={'A', 'B', 'C', 'D'}`
`print(set_A&set_B)`
`print(set_A.intersection( set_B))`
Output:
`{'A', 'D'}`
Difference:
Difference includes all elements that are in the first set (e.g., set A) but not in the second set (e.g., set B).
The minus `(-)` operator is used for the difference set operation in Python.
The `difference()` function can also be used for the difference operation.
Example of Difference:
Program to find the difference of two sets using the minus operator and the `difference()` function:
`set_A={'A', 2,4, 'D'}`
`set_B={'A', 'B', 'C', 'D'}`
`print(set_A - set_B)`
`print(set_A.difference( set_B))`
Output:
`{2, 4}` (Order can vary in sets)
Symmetric Difference:
The symmetric difference includes all elements that are present in either of the two sets (e.g., sets A and B) but not in their common part.
The caret `( ^ )` operator is used for the symmetric difference set operation in Python.
The `symmetric_difference()` function is also used to perform this operation.
Example of Symmetric Difference:
Program to find the symmetric difference of two sets using the caret operator and the `symmetric_difference()` function:
`set_A={'A', 2,4, 'D'}`
`set_B={'A', 'B', 'C', 'D'}`
`print(set_A ^ set_B)`
`print(set_A.symmetric_difference(set_B))`
Output:
`{2, 4, 'B', 'C'}` (Order can vary in sets)
In simple words: Set operations help us combine, find common items, or see differences between groups of items. Union puts everything together, intersection finds what's shared, difference finds what's only in one set, and symmetric difference finds what's unique to either set.
๐ฏ Exam Tip: When explaining set operations, always define the concept clearly, state the Python operator or function used, and provide a simple code example with its expected output.
Additional Important Questions And Answers
I. Choose The Best Answer (1 Mark)
Question 1. A list in Python is denoted by ___________.
(a) \( [ ] \)
(b) \( < > \)
(c) #
Answer: (a) \( [ ] \)
In simple words: In Python, square brackets like these `[ ]` are used to show that something is a list. This helps Python understand how to handle the data.
๐ฏ Exam Tip: Remember the specific symbols for each data structure in Python: square brackets for lists, parentheses for tuples, and curly braces for sets and dictionaries.
Question 2. Which of the following is an ordered collection of values?
(a) Tuples
(b) List
(c) Set
(d) Dictionary
Answer: (b) List
In simple words: A list keeps its items in a specific order, which means the first item you put in stays first, the second stays second, and so on.
๐ฏ Exam Tip: Lists are mutable and ordered, tuples are immutable and ordered, sets are mutable and unordered (no duplicates), and dictionaries are mutable and unordered (key-value pairs).
Question 3. Each value of a list is called as
(a) Set
(b) Dictionary
(c) Element
(d) Strings
Answer: (c) Element
In simple words: Every single item that you put inside a list is called an element. For example, in a list like `[10, 20, 30]`, 10, 20, and 30 are all elements.
๐ฏ Exam Tip: Understanding basic terminology like 'element', 'index', and 'collection' is crucial for explaining programming concepts clearly.
Question 4. In the list, the negative index number begins with
(a) 0
(b) 1
(c) -1
(d) 0.1
Answer: (c) -1
In simple words: When counting from the end of a list, the very last item is called by the negative index -1. The item before that is -2, and so on.
๐ฏ Exam Tip: Negative indexing is a powerful feature in Python for easily accessing elements from the end of a sequence, especially the last element with index -1.
Question 5. To access the list elements in reverse order, negative value has to be given.
(a) 0
(b) positive
(c) imaginary
(d) negative
Answer: (d) negative
In simple words: To get items from a list starting from the end, you need to use negative numbers for their positions, like -1 for the last item.
๐ฏ Exam Tip: Be careful not to confuse regular (positive) indexing with reverse (negative) indexing, as they count from opposite ends of the list.
Question 6. Which of the following can be used to access an element in a list?
(a) Index value
(b) Function
(c) Integer
(d) Identifier
Answer: (a) Index value
In simple words: Each item in a list has a special position number called an index. You use this index number to pick out a specific item from the list.
๐ฏ Exam Tip: Always remember that Python indexing starts from 0 for the first element, not 1.
Question 7. In `sim = [4,20,71,89]`, the negative index value of 20 is
(a) 2
(b) -2
(c) -1
(d) -3
Answer: (d) -3
In simple words: In the list `[4, 20, 71, 89]`, counting from the right, 89 is at -1, 71 is at -2, and 20 is at -3.
๐ฏ Exam Tip: When calculating negative indices, start from -1 for the last element and move leftwards, decreasing the number by one for each preceding element.
Question 8. Which function is used to set the upper limit in a loop to read all elements of a list?
(a) upper ()
(b) limit
(c) len ()
(d) loop ()
Answer: (c) len ()
In simple words: The `len()` function tells you how many items are in a list. You can use this number to decide how many times a loop should run to look at every item.
๐ฏ Exam Tip: The `len()` function is essential for dynamically controlling loop iterations when working with collections of varying sizes.
Question 9. Which function shows the deleted element as soon as the element is deleted?
(a) Remove
(b) pop
(c) Delete
(d) erase
Answer: (b) pop
In simple words: The `pop()` function takes an item out of a list, and it also tells you which item it just removed. This is useful if you need to use the removed item.
๐ฏ Exam Tip: Distinguish between `remove()` (deletes by value, returns `None`) and `pop()` (deletes by index or last element, returns the deleted element) for precise list manipulation.
Question 10. What is the output for the following?
`sim = ['T', 'E', 'A', 'M']`
`for i in sim:`
`print (sim [2])`
(a) T
(b) A
(c) C
(d) M
Answer: (b) A
In simple words: The code goes through each item in the list, but inside the loop, it always prints the item at position 2, which is 'A'. So, 'A' will be printed multiple times.
๐ฏ Exam Tip: Pay close attention to what is being iterated (`for i in sim`) versus what is being accessed inside the loop (`sim[2]`). In this case, `sim[2]` is a fixed index, leading to repeated output.
Question 11. _______________ operator is used to changing the list of elements.
(a) =
(b) +
(c) +=
(d) *=
Answer: (a) =
In simple words: The equals sign `=` is used to give a new value to a list or to change an item inside a list. It lets you assign new data to it.
๐ฏ Exam Tip: The assignment operator `=` is fundamental for modifying variables and elements within mutable data structures like lists.
Question 12. Which of the following function is used to add more than one element in an existing list?
(a) append ()
(b) extend ()
(c) more ()
(d) addmore ()
Answer: (b) extend ()
In simple words: To add several new items from another list or group into an existing list, you use the `extend()` function. It's like merging two lists together.
๐ฏ Exam Tip: Remember `append()` adds a single element (which can be another list) as one item, while `extend()` adds all elements from an iterable, effectively concatenating lists.
Question 13. What is the output for the following?
`mylist = [34,45,48]`
`print(mylist.append(90))`
(a) [34,45,48,90]
(b) [90,34,45,48]
(c) [34,45,90,84]
(d) [34,45,90,48]
Answer: (a) [34,45,48,90]
In simple words: The `append()` function changes the list by adding 90 to the end. However, `append()` itself does not return a value (it returns `None`), so printing its result would normally show `None`. But in this multiple-choice question, the answer expects the updated list content, assuming a print statement for the list came after the append.
๐ฏ Exam Tip: Be aware that methods that modify a list in-place (like `append()`, `sort()`, `reverse()`) typically return `None`. If a question asks for the "output" of printing such a method call, the technical answer is `None`, but MCQs often imply the state of the modified object.
Question 14. Which of the following function is used to include an element in a list at the desired position?
(a) append ()
(b) extend ()
(c) insert ()
(d) format ()
Answer: (c) insert ()
In simple words: The `insert()` function lets you put a new item into a list at a specific spot you choose, rather than just at the end.
๐ฏ Exam Tip: `insert()` requires two arguments: the index where the element should be placed and the element itself. Elements at and after that index are shifted to the right.
Question 15. Write the output,
`list = [34, 45, 48]`
`list.append(90)`
(a) [34, 45, 48, 90]
(b) [90, 34, 45, 48]
(c) [34, 90, 45, 48]
(d) [34, 45, 90, 48]
Answer: (a) [34, 45, 48, 90]
In simple words: The `append(90)` command adds the number 90 to the very end of the existing list. So, the list becomes `[34, 45, 48, 90]`.
๐ฏ Exam Tip: For problems involving list modifications, trace the list's state step-by-step to accurately predict the final output.
Question 16. Which function can also be used to delete one or more elements if the index value is not known?
(a) push()
(b) remove ()
(c) delete ()
Answer: (b) remove ()
In simple words: If you don't know the position of an item but know its value, you use the `remove()` function to take it out of the list.
๐ฏ Exam Tip: The `remove()` method deletes only the first occurrence of the specified value in the list. If the value is not found, it raises a `ValueError`.
Question 17. Which of the following command deletes only the elements in the list?
(a) erase ()
(b) pop ()
(c) clear ()
(d) remove ()
Answer: (c) clear ()
In simple words: The `clear()` command gets rid of all the items inside a list, making it empty. The list itself is still there, just without any contents.
๐ฏ Exam Tip: `clear()` removes all elements but keeps the list object, while `del list_name` would delete the list object entirely from memory.
Question 18. Creating a Tuple with one element is called a __________ tuple.
(a) mono
(b) single
(c) Singleton
(d) Singular
Answer: (c) Singleton
In simple words: A tuple that holds only one item is called a Singleton tuple. It's special because you need to add a comma after the single item to show it's a tuple, like `(item,)`.
๐ฏ Exam Tip: Remember to always include a trailing comma when defining a singleton tuple (e.g., `(5,)`) to distinguish it from a simple parenthesized expression (`(5)`). This is a common pitfall.
II. Answer The Following Questions (2 And 3 Marks)
Question 1. Write a note on the nested list?
Answer: A nested list is a list that contains another list as one of its elements. It's like having a box inside another box. For example, `My_list = [ "Welcome", 3.14, 10, [2, 4, 6] ]` shows a list where `[2, 4, 6]` is an element within `My_list`. This allows for more complex data structures.
In simple words: A nested list is a list that has other lists inside it as items.
๐ฏ Exam Tip: When working with nested lists, remember that each inner list is considered a single element of the outer list, which affects indexing and length calculations.
Question 2. Give short notes on Tuple's assignment.
Answer: Tuple assignment is a very useful feature in Python. It allows you to assign multiple values from the right side of an assignment operator to multiple variables on the left side, all at once. Each value from the tuple on the right is matched and assigned to its corresponding variable on the left. This makes swapping values or unpacking sequences very easy.
Example:
`>>> (a,b,c) = (34, 90, 76)`
`>>> print(a,b,c)`
`34 90 76`
In simple words: Tuple assignment lets you give many values to many variables at the same time, just by putting them in a tuple on both sides of an equals sign.
๐ฏ Exam Tip: Tuple assignment is often used for parallel assignment or to easily swap variable values without needing a temporary variable.
Question 3. Explain how can you create a set in Python with an example.
Answer: Sets in Python are created by enclosing all elements, separated by commas, within a pair of curly brackets `{}`. Alternatively, you can use the `set()` function to create an empty set or convert other iterable types like lists or tuples into a set. Sets automatically remove duplicate elements and do not maintain order.
Syntax:
`Set_Variable = {E1, E2, E3, ..., En}`
Example:
`>>> S1 = {1,2,3,'A',3.14}`
`>>> print(S1)`
`{1, 2, 3, 'A', 3.14}` (Order can vary in sets)
In simple words: You can make a set in Python by putting items inside curly brackets `{}`. Sets are special because they only keep unique items and don't remember their order.
๐ฏ Exam Tip: Remember that while dictionaries also use curly braces, sets contain only values without key-value pairs. An empty `{}` creates an empty dictionary, not an empty set; use `set()` for an empty set.
Question 4. What is meant by Reverse Indexing?
Answer: Reverse indexing in Python allows you to access elements of a list or other sequence data types by counting from the end of the sequence rather than the beginning. Python assigns `-1` as the index value for the last element in the list, `-2` for the element preceding it, and so on. This provides a convenient way to access elements from the right side of a collection.
In simple words: Reverse indexing means you count positions from the end of a list, where the last item is at position -1. It helps you quickly find items without knowing the list's total length.
๐ฏ Exam Tip: Negative indices are particularly useful when you need to access the last few elements of a list without knowing its exact size, such as `list[-1]` for the very last element.
Question 5. Explain Dictionary Comprehension.
Answer: Dictionary comprehension is a concise and efficient way to create new dictionaries in Python. It allows you to build a dictionary from an iterable by applying an expression to each item, similar to list comprehensions. The syntax involves key-value pairs within curly braces, with an optional `if` condition to filter elements. This method makes dictionary creation more readable and faster.
Syntax:
`Dict = {expression for variable in sequence [if condition]}`
Example:
`Dict = {x : 2 * x for x in range(1,10)}`
Output:
`{1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12, 7: 14, 8: 16, 9: 18}`
In simple words: Dictionary comprehension is a quick way to make a new dictionary using a short line of code. You can set rules for which items to include and how to make their keys and values.
๐ฏ Exam Tip: Dictionary comprehensions are excellent for creating dictionaries on the fly, especially for mapping items from one collection to another or generating key-value pairs based on a formula. Remember that the `if` condition is optional for filtering.
Question 6. What are the four collections of data types in a Python programming language?
Answer: Python programming language includes four primary built-in collection data types that allow you to store multiple values. These are List, Tuples, Set, and Dictionary. Each type has distinct characteristics regarding order, mutability, and whether it allows duplicate elements.
In simple words: The four main ways Python groups data are Lists (like ordered shopping lists), Tuples (like fixed lists), Sets (like unique groups of items), and Dictionaries (like phone books that link names to numbers).
๐ฏ Exam Tip: For each collection type, be prepared to describe its key features: whether it's ordered, mutable, and allows duplicate members, as these are common comparison points.
Question 7. Differentiate `clear()` and `del` in list?
Answer: The `clear()` function and the `del` statement are both used to remove elements from a list, but they work differently. The `clear()` function is a method that removes all elements from a list, making it empty, but the list object itself still exists in memory. In contrast, the `del` statement completely deletes the list object (or a slice/element of it) from memory, meaning the variable name referring to the list will no longer be defined.
In simple words: `clear()` empties a list but the list still exists. `del` removes the entire list completely, so it's gone from your program.
๐ฏ Exam Tip: Use `clear()` when you want an empty list for reuse, and `del` when you no longer need the list at all and want to free up its memory space.
Question 8. Differentiate `append()` and `extend()` function.
Answer: The `append()` and `extend()` functions are both used to add elements to a list, but they handle the addition differently. The `append()` function adds a single element to the end of a list, potentially creating a nested list if the element itself is a list. The `extend()` function, on the other hand, adds all individual elements from an iterable (like another list, tuple, or string) to the end of the current list, effectively merging the collections without nesting.
| `append()` function | `extend()` function |
|---|---|
| Used to add a single element to a list. | Used to add more than one element (from an iterable) to an existing list. |
In simple words: `append()` adds one item at a time to the end of a list. `extend()` adds many items from another list to the end, making the first list longer.
๐ฏ Exam Tip: A key difference is that `append()` can lead to nested lists if you add another list, while `extend()` always flattens the added iterable into the existing list.
Question 9. What will be the output of the following snippet?
`alpha=list(range(65,70))`
`for x in alpha:`
`print(chr(x), end=' ')`
Output:
`A B C D E`
Answer: The code first creates a list `alpha` containing numbers from 65 to 69 (since `range(65,70)` generates numbers up to, but not including, 70). It then loops through each number `x` in `alpha`. Inside the loop, `chr(x)` converts each number to its corresponding ASCII character. `65` becomes `A`, `66` becomes `B`, and so on, resulting in `A B C D E`. The `end=' '` ensures they print on the same line with a space in between.
In simple words: The program makes a list of numbers from 65 to 69. Then, it changes each number into its letter form, so 65 turns into 'A', 66 into 'B', and so on, printing all these letters next to each other.
๐ฏ Exam Tip: Remember the `chr()` function converts an integer ASCII value to its character, and `ord()` converts a character to its ASCII integer value. These are useful for character-based operations.
Question 10. What will be the output of the following code?
`list=[3** x for x in range (5)]`
`print(list)`
Output:
`[1, 3, 9, 27, 81]`
Answer: The code uses a list comprehension to create a list. It calculates `3` raised to the power of `x` for each `x` in the range from `0` to `4` (since `range(5)` generates `0, 1, 2, 3, 4`). So, it computes `3^0`, `3^1`, `3^2`, `3^3`, and `3^4`, which are `1, 3, 9, 27,` and `81` respectively. These results form the new list.
In simple words: This code creates a list by taking the number 3 and raising it to the power of 0, then 1, then 2, then 3, and finally 4. The answers (1, 3, 9, 27, 81) are put into the list.
๐ฏ Exam Tip: List comprehensions provide a compact way to create lists and are a common feature in Python for efficient code writing. Always remember `range(n)` generates numbers from 0 up to `n-1`.
Question 11. How to delete elements from the list.
Answer: There are primarily two ways to delete elements from a list in Python: using the `del` statement and using the `remove()` function. The `del` statement is used to delete elements when their index (position) is known, or to delete a slice of the list, or even the entire list object. The `remove()` function is used to delete a specific element by its value, particularly when its index is unknown. It removes only the first occurrence of that value.
Syntax for `del` statement:
To delete a particular element: `del List [index of an element]`
To delete multiple elements: `del List [index from : index to]`
To delete entire list: `del List`
Syntax for `remove()` function:
`List.remove(element)`
In simple words: You can delete items from a list either by knowing their position (`del`) or by knowing the item itself (`remove()`). `del` can also clear out a whole section or the entire list.
๐ฏ Exam Tip: Ensure you understand the difference between deleting by index (`del` or `pop()`) and deleting by value (`remove()`), as well as the effects of `del` on list slices and the entire list object.
Question 12. Write notes on `pop()` and `clear()` function.
Answer: The `pop()` and `clear()` functions are both list methods used for removing elements, but they serve different purposes. The `pop()` function removes and returns an element from a specified index in the list. If no index is provided, it removes and returns the last element. The `clear()` function, on the other hand, removes all elements from the list, making it empty. Unlike `del` (which deletes the list object), `clear()` leaves an empty list object intact.
`pop()` function:
- The `pop()` function can remove an element using its given index value.
- If the index is not provided, `pop()` deletes and returns the last element of a list.
`clear()` function:
- The `clear()` function deletes all the elements in a list.
- It deletes only the elements and keeps the list.
In simple words: `pop()` takes out one item from a list (and tells you which one), while `clear()` wipes out everything inside a list, leaving it empty.
๐ฏ Exam Tip: Always remember that `pop()` returns the removed element, which can be useful for further processing, while `clear()` does not return any value.
Question 13. Define Tuple in Python with syntax?
Answer: A tuple in Python is an ordered, immutable collection of values, meaning its elements cannot be changed after creation. Tuples consist of a number of values separated by commas and enclosed within parentheses `()`. They are similar to lists but are often used for fixed collections of items, ensuring data integrity.
Syntax:
# Empty tuple:
`Tuple_Name = ()`
# Tuple with n number elements:
`Tuple_Name = (E1, E2, E3, ..., En)`
# Elements of a tuple without parenthesis:
`Tuple_Name = E1, E2, E3, ..., En`
In simple words: A tuple is like a list that you cannot change once you make it. You put its items inside round brackets `()`, and they stay in that order forever.
๐ฏ Exam Tip: Remember that the immutability of tuples means you cannot add, remove, or change elements after creation, which makes them suitable for data that should remain constant.
Question 14. Write notes on tuple () function? Give example.
Answer: The `tuple()` function in Python helps convert other collections, like a list, into a tuple. When you use this function, it takes the elements from the list, which are normally in square brackets, and puts them into a new tuple, enclosed by parentheses. This is useful for making your data unchangeable.
Example:
`MyTup3 = tuple([23,45,90])`
`>>> print(MyTup3)`
`(23,45,90)`
In simple words: This function turns a list into a tuple. It takes items that were in square brackets and puts them into a new tuple inside round brackets.
๐ฏ Exam Tip: Remember that tuples are immutable, meaning their contents cannot be changed after creation, unlike lists. This is a key distinction for choosing between them.
Question 15. How to create a set using a list or tuple? Give an example.
Answer: You can easily create a set from either a list or a tuple by using the built-in `set()` function in Python. First, make your list or tuple, then pass it as an argument inside the `set()` function. This process automatically removes any duplicate elements, which is a key characteristic of sets.
Example:
`MyList = [2,4,6,8,10]`
`MySet = set(MyList)`
`print(MySet)`
Output:
`{2, 4, 6, 8, 10}`
In simple words: To make a set from a list or tuple, just put your list or tuple inside the `set()` function. It will turn it into a set and remove any repeated items.
๐ฏ Exam Tip: Sets are incredibly useful for tasks where you need a collection of unique items or to perform mathematical set operations quickly.
Question 16. Give notes on Dictionary in Python.
Answer: In Python, a dictionary is a unique type of data collection that can hold different kinds of items. Unlike lists or tuples, dictionaries store data as pairs: a 'key' and its 'value'. These key-value pairs are separated by a colon (:) and different pairs are separated by commas. The entire dictionary is enclosed within curly braces `{}`. This structure allows for very fast lookup of values using their keys.
In simple words: A dictionary in Python holds different types of data by pairing a unique key with a value. Keys and values are joined by a colon, and the whole dictionary is put in curly brackets.
๐ฏ Exam Tip: Think of a dictionary as a real-world dictionary: you look up a word (key) to find its definition (value). Keys must be unique, but values can be repeated.
Question 17. Give notes on len() function of a list with an example.
Answer: The `len()` function in Python is a built-in tool used to count how many items are in a list. It gives you an integer representing the number of elements. This function is often used to determine the stopping point when iterating through a list with a loop, ensuring all elements are processed. Interestingly, if a list contains another list (a nested list), `len()` counts the nested list as just one single element.
Example:
`MySubject = ['Tamil', 'English', 'Science', 'Math']`
`>>> len(MySubject)`
`4`
In simple words: The `len()` function tells you how many items are in a list. It helps when looping through a list, and it counts a list inside a list as just one item.
๐ฏ Exam Tip: The `len()` function works for many sequence types beyond lists, including strings and tuples, making it a versatile tool for checking length.
Question 18. Define List comprehensions.
Answer: List comprehension in Python provides a concise and elegant way to create new lists based on existing sequences (like other lists, tuples, or ranges). It allows you to build a new list by applying an expression to each item in an iterable, optionally filtering the items based on a condition, all within a single line of code. This method is often more readable and efficient than using traditional `for` loops.
Syntax:
`List = [expression for variable in iterable (if condition)]`
Example: Generating squares of first 10 natural numbers using list comprehension.
`>>> squares = [x ** 2 for x in range(1, 11)]`
`>>> print(squares)`
Output:
`[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]`
In simple words: List comprehension is a short way to make new lists in Python. You can create a list by telling Python to do something to each item from another list or range, often much faster than writing a longer loop.
๐ฏ Exam Tip: Use list comprehensions when you need to create a new list by transforming or filtering elements from an existing iterable, as it makes code shorter and often faster.
Question 19. What is the difference in between List and Tuple.
Answer: Lists and tuples are both fundamental sequence data types in Python, but they have key differences related to mutability and syntax. Lists are defined using square brackets and can be modified after creation, while tuples use parentheses and are immutable. This distinction makes tuples ideal for fixed collections, and lists for dynamic data.
In simple words: The main difference is that lists can be changed (you can add, remove, or change items), but tuples cannot be changed once they are made. Lists use square brackets `[]` and tuples use round brackets `()`.
| List | Tuple |
|---|---|
| Elements are inside `[]` (square brackets). | Elements are inside `()` (parentheses). |
| Values in a list can be changed (mutable). | Values in a tuple cannot be changed (immutable). |
๐ฏ Exam Tip: Remember that "mutable" means changeable, and "immutable" means unchangeable. This concept is crucial for understanding how these data types behave in your programs.
Question 20. Write execution table for the following Python code.
Answer: The Python code calculates the sum of the first four elements in the `Marks` list using a `while` loop. The list is `[10, 20, 30, 40, 50]`. The loop continues as long as `i` is less than 4. In each step, the element at `Marks[i]` is added to `sum`, and `i` is increased by 1. The execution table tracks the values of `i`, the condition `i<4`, the `Marks[i]` element, the running `sum`, and the new value of `i` after the increment. This type of loop is fundamental for repetitive tasks in programming.
Code:
`Marks=[10, 20, 30, 40, 50]`
`i = 0`
`sum = 0`
`while i < 4:`
` sum += Marks[i]`
` i += 1`
In simple words: This code adds up the first four numbers in the `Marks` list. The table shows how `i` changes and how the `sum` grows step by step until the loop finishes.
| S.No | \(i\) | \(i < 4\) | \(Marks[i]\) | Sum | \(i + 1\) |
|---|---|---|---|---|---|
| 1 | 0 | 0<4 (True) | 10 | 10 | 1 |
| 2 | 1 | 1<4 (True) | 20 | 30 | 2 |
| 3 | 2 | 2<4 (True) | 30 | 60 | 3 |
| 4 | 3 | 3<4 (True) | 40 | 100 | 4 |
| 5 | 4 | 4<4 (False) |
๐ฏ Exam Tip: When tracing `while` loops, carefully check the condition before each iteration and update all relevant variables (like `sum` and `i`) inside the loop.
Question 21. What will be the output of the following snippet?
Answer: This Python snippet uses a dictionary comprehension to create a dictionary. It iterates through numbers from 97 up to (but not including) 102. For each number `x`, it creates a key using `chr(x)`, which converts the number to its corresponding ASCII character (e.g., 97 is 'a'), and uses `x` itself as the value. The output is a dictionary where each lowercase letter from 'a' to 'e' is mapped to its ASCII integer value.
Code:
`Mydict = {chr(x):x for x in range(97,102)}`
`print(Mydict)`
Output:
`{'a': 97, 'b': 98, 'c': 99, 'd': 100, 'e': 101}`
In simple words: This code makes a dictionary. It matches letters like 'a' to 'e' with their special number codes (ASCII values).
๐ฏ Exam Tip: `chr()` is a useful function to convert ASCII integer values back to characters, while `ord()` does the opposite (character to integer).
Question 22. What will be the output of the following snippet?
Answer: This Python code snippet defines two sets, `set_A` and `set_B`. It then uses the `&` operator, which performs a set intersection operation. The intersection of two sets results in a new set containing only the elements that are present in *both* original sets. In this case, the common elements between `set_A` and `set_B` are 'A' and 'D'. Set operations are very efficient for finding common or unique elements.
Code:
`set_A = {'A', 2, 4, 'D'}`
`set_B = {'A', 'B', 'C', 'D'}`
`print(set_A & set_B)`
Output:
`{'A', 'D'}`
In simple words: The code makes two sets. Then it uses the `&` symbol to find what items are in *both* sets. The answer shows only the items that appear in both `set_A` and `set_B`.
๐ฏ Exam Tip: The `&` operator is for intersection, `|` for union, `-` for difference, and `^` for symmetric difference between sets.
III. Answer the Following Questions (5 Marks)
Question 1. Explain how to access all elements of a list using while loop with suitable example
Answer: To access every item in a list one by one using a `while` loop, you need to use an index variable. This variable should start at `0`, because list indexing in Python begins from zero. The `while` loop continues as long as this index variable is less than the total number of elements in the list. Inside the loop, you can use the index to get each element, and then you increment the index to move to the next item. This sequential access ensures you visit every element. Using an index provides fine-grained control over which elements are accessed.
Example:
`Marks = [10, 23, 41, 75]`
`i = 0`
`while i < 4:`
` print(Marks[i])`
` i = i + 1`
Output:
`10`
`23`
`41`
`75`
In simple words: To get all items from a list using a `while` loop, you start counting from `0` for the first item. The loop keeps going as long as your count is smaller than the list's total size, letting you look at each item one by one.
| S.No | \(i\) | \(i < 4\) | \(Marks[i]\) (Output) | \(i = i + 1\) (Next \(i\)) |
|---|---|---|---|---|
| 1 | 0 | True | 10 | 1 |
| 2 | 1 | True | 23 | 2 |
| 3 | 2 | True | 41 | 3 |
| 4 | 3 | True | 75 | 4 |
| 5 | 4 | False | Loop ends |
๐ฏ Exam Tip: Always be careful with off-by-one errors when using `while` loops with indices; ensure your condition correctly handles the last element and avoids going out of bounds.
Question 2. Write a python program using list to read marks of six subjects and to print the marks scored in each subject and show the total marks
Answer: This Python program demonstrates how to collect marks for six different subjects using a list and then display both individual subject marks and the total score. It starts by defining a list of subject names. A `for` loop prompts the user to input marks for each subject, appending them to a `marks` list. Then, another `for` loop iterates through the collected marks, printing each subject's name and its corresponding score. Finally, the `sum()` function calculates and prints the grand total of all marks, making it easy to summarize academic performance.
Program:
`marks = [] # Initialize an empty list for marks`
`subjects = ['Tamil', 'English', 'Physics', 'Chemistry', 'Comp. Science', 'Maths']`
`for i in range(len(subjects)):`
` m = int(input("Enter Mark = "))`
` marks.append(m)`
`for j in range(len(marks)):`
` print(f"{j+1}. {subjects[j]} Mark = {marks[j]}")`
`print("Total Marks = ", sum(marks))`
Output Example:
`Enter Mark = 45`
`Enter Mark = 76`
`Enter Mark = 28`
`Enter Mark = 46`
`Enter Mark = 15`
`1. Tamil Mark = 45`
`2. English Mark = 98` (Note: Sample output had 98 for English, this could be a different run or typo in source's input list. I'll maintain source output)
`3. Physics Mark = 76`
`4. Chemistry Mark = 28`
`5. Comp. Science Mark = 46`
`6. Maths Mark = 15`
`Total Marks = 308`
In simple words: This Python program asks you for marks in six subjects, stores them in a list, then prints each subject's mark and the total marks you scored. It's a simple way to keep track of scores.
๐ฏ Exam Tip: For interactive programs, ensure clear prompts for user input and format the output neatly for better readability.
Question 3. Explain remove, pop () and clear () used in list with an example.
Answer:
`remove()`: The `remove()` function in Python is used to delete the *first occurrence* of a specified value from a list. You provide the actual item you want to remove, not its position. If the item appears multiple times, only the first one is taken out. This function is helpful when you know what value you want to eliminate but don't know (or care about) its index.
Syntax: `List.remove(element)`
Example:
`MyList = [12, 89, 34, 'Kannan', 'Gowrisankar', 'Lenin']`
`>>> print(MyList)`
`[12, 89, 34, 'Kannan', 'Gowrisankar', 'Lenin']`
`>>> MyList.remove(89)`
`>>> print(MyList)`
`[12, 34, 'Kannan', 'Gowrisankar', 'Lenin']`
In simple words: `remove()` takes out the first exact item you tell it to from a list.
`pop()`: The `pop()` function removes and returns an element from a list, either by its specified index or, if no index is given, it removes and returns the last element. This means `pop()` is useful when you want to retrieve an element and simultaneously remove it from the list, often used for stack-like operations (Last-In, First-Out).
Syntax: `List.pop(index_of_an_element)`
Example:
`MyList = [12, 'Kannan', 'Gowrisankar', 'Lenin']`
`>>> MyList.pop(1)`
`'Kannan'`
`>>> print(MyList)`
`[12, 'Gowrisankar', 'Lenin']`
In simple words: `pop()` takes an item out of a list and gives it to you. If you tell it *which* item by its number, it takes that one; otherwise, it takes the very last item.
`clear()`: The `clear()` function is used to remove all elements from a list, effectively making the list empty. However, it does not delete the list itself; the list object still exists but contains no items. When you print a list after using `clear()`, it will show an empty square bracket `[]`, confirming that it's an empty list. This is useful for resetting a list without needing to create a new one.
Syntax: `List.clear()`
Example:
`>>> MyList.clear()`
`>>> print(MyList)`
`[]`
In simple words: `clear()` wipes out all the items from a list, leaving it completely empty, but the list itself is still there.
๐ฏ Exam Tip: Understand the difference between deleting by value (`remove()`), by index (`pop()`), and clearing all contents (`clear()`). Each has a specific use case.
Question 4. Explain the following functions used in list function with an example. max(), min(),sum()
Answer: Python provides several built-in functions to easily perform common operations on numerical lists. These functions are very helpful for quickly getting insights or performing basic calculations on numerical data stored in lists without needing to write loops.
In simple words: Python has easy functions to find the biggest, smallest, or total sum of numbers in a list without needing to write complex code.
| Function | Description | Syntax | Example |
|---|---|---|---|
| `max()` | Finds the largest number in a list. | `max(list)` | `MyList=[21,76,98,23]` `print(max(MyList))` Output: `98` |
| `min()` | Finds the smallest number in a list. | `min(list)` | `MyList=[21,76,98,23]` `print(min(MyList))` Output: `21` |
| `sum()` | Adds up all the numbers in a list. | `sum(list)` | `MyList=[21,76,98,23]` `print(sum(MyList))` Output: `218` |
๐ฏ Exam Tip: These functions are typically used with lists containing numbers, and will raise an error if non-numeric types are present (except for `max()` and `min()` with strings, which compare lexicographically).
Question 4. Explain the following functions used in list function with an example. count () 3. index () 4. reverse () 5.sort ()
Answer: These methods are essential for managing and manipulating list data, allowing for operations like non-destructive copying or in-place reordering. Each function provides a specific utility for common list tasks.
In simple words: These tools help you work with lists by letting you copy, count items, find item positions, and change the order of items.
| Function | Description | Syntax | Example |
|---|---|---|---|
| `copy()` | Makes an exact new copy of a list, so you can change the copy without changing the original. | `List.copy()` | `MyList=[12,12,36]` `x = MyList.copy()` `print(x)` Output: `[12,12,36]` |
| `count()` | Counts how many times a specific item appears in a list. | `List.count(value)` | `MyList=[36,12,12]` `x = MyList.count(12)` `print(x)` Output: `2` |
| `index()` | Gives you the position (index) of the *first* time a certain item is found in the list. | `List.index(element)` | `MyList=[36,12,12]` `x = MyList.index(12)` `print(x)` Output: `0` |
| `reverse()` | Changes the list by putting its items in the opposite order, from last to first (in-place). | `List.reverse()` | `MyList=[36,23,12]` `MyList.reverse()` `print(MyList)` Output: `[12,23,36]` |
๐ฏ Exam Tip: Be aware that `reverse()` modifies the list in-place and does not return a new list. If you need a new reversed list, use `list(reversed(my_list))`.
Question 4. Explain the following functions used in list function with an example. sort ()
Answer: The `sort()` method is crucial for organizing data within a list, making it easier to search, compare, or display information in a structured way. It allows you to arrange elements in ascending or descending order, or even based on a custom key.
In simple words: The `sort()` function puts the items in a list into a specific order, like alphabetical or from smallest to largest.
| Function | Description | Syntax | Example |
|---|---|---|---|
| `sort()` | Arranges the items in a list in a specific order (like smallest to largest, or A to Z). | `List.sort(reverse=True | False, key=myFunc)` | `MyList = ['Thilothamma', 'Tharani', 'Anitha', 'SaiSree', 'Lavanya']` `MyList.sort()` `print(MyList)` Output: `['Anitha', 'Lavanya', 'SaiSree', 'Tharani', 'Thilothamma']` `MyList.sort(reverse=True)` `print(MyList)` Output: `['Thilothamma', 'Tharani', 'SaiSree', 'Lavanya', 'Anitha']` |
๐ฏ Exam Tip: Remember that `sort()` modifies the list in-place and does not return a new list. Use `sorted()` if you need a new sorted list while keeping the original unchanged.
Question 6. What will be the output of the following Python program?
Answer: This Python program creates two sets, `A` and `B`, using set comprehension. Set `A` contains multiples of 3 for numbers from 1 to 5, and set `B` contains squares of even numbers from 1 to 9. The program then prints these sets and the results of various set operations: `A | B` (union) combines all unique elements from both sets; `A - B` (difference) includes elements only in A but not in B; `A & B` (intersection) shows elements common to both sets; and `A ^ B` (symmetric difference) gives elements present in either A or B, but not in their intersection. This demonstrates how sets efficiently handle unique elements and set operations.
Code:
`A = {x*3 for x in range (1,6))}`
`B = {y**2 for y in range (1,10,2))}`
`print(A)`
`print(B)`
`print(A | B)`
`print(A-B)`
`print(A&B)`
`print(A ^ B)`
Output:
`{3, 6, 9, 12, 15}`
`{1, 9, 25, 49, 81}`
`{1, 3, 6, 9, 12, 15, 25, 49, 81}`
`{3, 6, 12, 15}`
`{9}`
`{1, 3, 6, 12, 15, 25, 49, 81}`
In simple words: This code makes two groups of numbers (sets) using simple rules. Then, it shows what numbers are in each group. After that, it combines them in different ways: putting all unique numbers together, finding numbers only in the first group, finding numbers common to both, and finding numbers that are in either but not both.
๐ฏ Exam Tip: For set comprehensions, carefully analyze the `range()` function arguments (start, stop, step) to determine the elements generated for each set.
Question 7. What will be the output of the following Python program?
Answer: This Python program first creates a list `N` containing numbers from 1 to 10. It then converts this list into a tuple named `Num` and prints it. Next, it iterates through the `N` list using `enumerate`, which provides both the index and the value of each element. Inside the loop, it checks if an element `i` is odd. If it is, the `del` statement removes that element from `N` at its current `index`. Deleting elements while iterating through a list in this way works in Python, and in this specific case, it successfully removes all odd numbers, leaving only the even ones.
Code:
`N = []`
`for x in range(1,11):`
` N.append(x)`
`Num = tuple(N)`
`print(Num)`
`for index, i in enumerate(N):`
` if(i % 2 == 1): # If i is odd`
` del N[index]`
`print(N)`
Output:
`(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)`
`[2, 4, 6, 8, 10]`
In simple words: The code first makes a list of numbers from 1 to 10, then turns it into a tuple and prints it. After that, it goes through the original list and deletes every odd number from it. The final list only contains even numbers.
๐ฏ Exam Tip: Modifying a list while iterating over it can lead to unexpected results; always trace carefully or use alternative methods (like creating a new list) for safer deletion.
Question 8. What will be the output of the following Python program?
Answer: This Python program creates two sets, `A` and `B`, using set comprehensions. Set `A` includes multiples of 3 for numbers from 1 to 5. Set `B` contains squares of numbers starting from 1 up to 9, taking every second number. The program then prints these two sets. Following this, it performs and prints several set operations: union (`A | B`), difference (`A - B`), intersection (`A & B`), and symmetric difference (`A ^ B`). This code visually illustrates how set operations combine or separate unique elements efficiently.
Code:
`A = {x*3 for x in range (1,6)}`
`B = {y**2 for y in range (1,10,2)}`
`print(A)`
`print(B)`
`print(A | B)`
`print(A-B)`
`print(A&B)`
`print(A^B)`
Output:
`{3, 6, 9, 12, 15}`
`{1, 9, 25, 49, 81}`
`{1, 3, 6, 9, 12, 15, 25, 49, 81}`
`{3, 6, 12, 15}`
`{9}`
`{1, 3, 6, 12, 15, 25, 49, 81}`
In simple words: This code makes two groups of numbers (sets) using simple rules. Then, it shows the numbers in each group. After that, it combines them in different ways: putting all unique numbers together, finding numbers only in the first group, finding numbers common to both, and finding numbers that are in one group but not in both.
๐ฏ Exam Tip: Pay close attention to the `range()` parameters to correctly identify the numbers included in the comprehensions, especially the `step` value.
Question 9. Write a program using a function that returns the area and circumference of a circle whose radius is passed as an argument two values using tuple assignment.
Answer: This Python program defines a function to calculate both the area and circumference of a circle. It takes the radius of the circle as an input. Inside the function, it uses the value of \( \pi \) (pi) set to 3.14 to compute the area ( \( \pi r^2 \) ) and the circumference ( \( 2 \pi r \) ). The function then returns these two calculated values as a tuple. The main part of the program prompts the user for a radius, calls the function, and uses tuple assignment to unpack the returned area and circumference into separate variables, which are then printed. This demonstrates an effective way to return multiple values from a function.
Program:
`pi = 3.14`
`def Circle(r):`
` return (pi * r * r, 2 * pi * r)`
`radius = float(input("Enter the Radius! "))`
`(area, circum) = Circle(radius)`
`print("Area of the circle = ", area)`
`print("Circumference of the circle = ", circum)`
Output:
`Enter the Radius: 5`
`Area of the circle = 78.5`
`Circumference of the circle = 31.400000000000002`
In simple words: This Python code has a function that calculates a circle's area and circumference after you give it the circle's size (radius). The function gives back both answers at once, which are then printed.
๐ฏ Exam Tip: When a function needs to return multiple related values, packaging them into a tuple is a clean and efficient Pythonic approach.
No educational content (questions, answers, or exercise headings) was found between pages 43 and 44 of the provided document. The content in this range consists solely of website navigation, metadata, and footer information, which is to be ignored according to the processing rules.Free study material for Computer Science
TN Board Solutions Class 12 Computer Science Chapter 09 Lists Tuples Sets and Dictionary
Students can now access the TN Board Solutions for Chapter 09 Lists Tuples Sets and Dictionary prepared by teachers on our website. These solutions cover all questions in exercise in your Class 12 Computer Science textbook. Each answer is updated based on the current academic session as per the latest TN Board syllabus.
Detailed Explanations for Chapter 09 Lists Tuples Sets and Dictionary
Our expert teachers have provided step-by-step explanations for all the difficult questions in the Class 12 Computer Science chapter. Along with the final answers, we have also explained the concept behind it to help you build stronger understanding of each topic. This will be really helpful for Class 12 students who want to understand both theoretical and practical questions. By studying these TN Board Questions and Answers your basic concepts will improve a lot.
Benefits of using Computer Science Class 12 Solved Papers
Using our Computer Science solutions regularly students will be able to improve their logical thinking and problem-solving speed. These Class 12 solutions are a guide for self-study and homework assistance. Along with the chapter-wise solutions, you should also refer to our Revision Notes and Sample Papers for Chapter 09 Lists Tuples Sets and Dictionary to get a complete preparation experience.
FAQs
The complete and updated Samacheer Kalvi Class 12 Computer Science Solutions Chapter 9 Lists, Tuples, Sets and Dictionary is available for free on StudiesToday.com. These solutions for Class 12 Computer Science are as per latest TN Board curriculum.
Yes, our experts have revised the Samacheer Kalvi Class 12 Computer Science Solutions Chapter 9 Lists, Tuples, Sets and Dictionary as per 2026 exam pattern. All textbook exercises have been solved and have added explanation about how the Computer Science concepts are applied in case-study and assertion-reasoning questions.
Toppers recommend using TN Board language because TN Board marking schemes are strictly based on textbook definitions. Our Samacheer Kalvi Class 12 Computer Science Solutions Chapter 9 Lists, Tuples, Sets and Dictionary will help students to get full marks in the theory paper.
Yes, we provide bilingual support for Class 12 Computer Science. You can access Samacheer Kalvi Class 12 Computer Science Solutions Chapter 9 Lists, Tuples, Sets and Dictionary in both English and Hindi medium.
Yes, you can download the entire Samacheer Kalvi Class 12 Computer Science Solutions Chapter 9 Lists, Tuples, Sets and Dictionary in printable PDF format for offline study on any device.