O’TECH
COMPUTER AND LANGUAGE
TRAINING CENTER
DEPARTMENT OF SOFTWARE
PYTHON PROGRAMMING
LAB-3
Collections
APRIL 01, 2023
Exercise #1 – Working with Lists.
Note: - Lists are used to store a sequence of elements.
- In a list, you can have just a few items or millions of items.
- Here you may see the presence of mixed datatypes too. Formally, you can say a list can
contain a sequence of objects which may come from different data types.
Goal:
❖ Defining a list.
❖ list_name=[value1,value2,value3,..]
# A list with three strings
my_list1 = ["John", "Bob", "Sam"]
print("The my_list1 is as follows:")
print(my_list1)
Expected Output:
The my_list1 is as follows:
['John', 'Bob', 'Sam']
Goal:
❖ Defining a list with mixed data types.
# A list can store mixed data types
my_list = ["John", 12, "Sam", True, 50.7]
print(my_list)
print("----------------")
Expected Output:
['John', 12, 'Sam', True, 50.7]
----------------
Goal:
❖ Printing the elements of a list using an index.
❖ The indexing starts with 0 from the extreme left.
# You can use a list index. The usage is similar to strings.
my_list = ["John", 12, "Sam", True, 50.7]
print(my_list[0]) # John
print(my_list[1]) # 12
print(my_list[2]) # Sam
print(my_list[3]) # True
print(my_list[4]) # 50.7
# Error: List index out of range
# print(my_list[5])
Expected Output:
John
12
Sam
True
50.7
Goal:
❖ Printing the elements of a list from the extreme right.
❖ In this case, indexing starts from -1.
# You can use list indexing from the right end.
# In this case, it starts from -1
my_list = ["John", 12, "Sam", True, 50.7]
print(my_list[-1]) # 50.7
print(my_list[-2]) # True
print(my_list[-3]) # Sam
print(my_list[-4]) # 12
print(my_list[-5]) # John
# Error: List index out of range
# print(my_list[-6])
Expected Output:
50.7
True
Sam
12
John
Goal:
❖ Let’s print only a specific portion of the list.
❖ Refer to the supportive comments.
# Printing list elements starting from a particular position
my_list = ["John", 12, "Sam", True, 50.7]
print("The original list is as follows:")
print(my_list)
print("Printing the elements starting from index position 2 to end:")
print(my_list[2:])
print("Printing the elements starting from index position 1 to 3(i.e.4-1):")
print(my_list[1:4])
Expected Output:
The original list is as follows:
['John', 12, 'Sam', True, 50.7]
Printing the elements starting from index position 2 to end:
['Sam', True, 50.7]
Printing the elements starting from index position 1 to 3(i.e.4-1):
[12, 'Sam', True]
Goal:
❖ You can reassign a new value in the list.
❖ Here is an example.
# You can reassign a new value inside the list
my_list = ["John", 12, "Sam", True, 50.7]
print("The original list is as follows:")
print(my_list)
print("Changing the element at index 2.")
my_list[2] = "Bob"
print("Now the list is as follows:")
print(my_list)
Expected Output:
The original list is as follows:
['John', 12, 'Sam', True, 50.7]
Changing the element at index 2.
Now the list is as follows:
['John', 12, 'Bob', True, 50.7]
Goal:
❖ You can concatenate multiple strings.
❖ There are various ways to accomplish this task.
❖ The simplest among them is to use the “+” operator.
❖ One sample usage of this is shown in the following example when you concatenate two lists
called my_list1 and my_list2.
# Concatenation example
my_list1 = ["John", 12, 50.7]
my_list2 = ["Sam", 25, "John", False, 100.2]
print("Original lists are :")
print(my_list1)
print(my_list2)
print("After concatenating the lists, you get the following list:")
print(my_list1 + my_list2)
Expected Output:
Original lists are :
['John', 12, 50.7]
['Sam', 25, 'John', False, 100.2]
After concatenating the lists, you get the following list:
['John', 12, 50.7, 'Sam', 25, 'John', False, 100.2]
Goal:
❖ Let’s print a specific number of elements from a list.
❖ To demonstrate this, in this example, Let’s print the last 3 elements, the last 2 elements, and
the last element of a list.
# Printing a specific number of elements of a list from the # end.
my_list = ["John", 12, "Sam", True, 50.7]
print("The original list is:")
print(my_list)
print("The last 3 elements of the list are:")
print(my_list[-3:])
print("The last 2 elements of the list are:")
print(my_list[-2:])
print("The last element of the list is:")
print(my_list[-1])
Expected Output:
The original list is:
['John', 12, 'Sam', True, 50.7]
The last 3 elements of the list are:
['Sam', True, 50.7]
The last 2 elements of the list are:
[True, 50.7]
The last element of the list is:
50.7
Goal:
❖ Let’s remove an element from a list.
❖ Here we use the del() function.
❖ At first, we remove the element from index position 2.
❖ In the next step, we again remove another element from the modified list. This time we remove the
element from index location 3
# Removing an element using del()
my_list = ["John", 12, "Sam", True, 50.7]
print("The original list is:")
print(my_list)
print("Removing the element at index 2.")
del (my_list[2])
print("Now the list is:")
print(my_list)
print("Removing the element at index 3 from this updated list.")
del (my_list[3])
print("The updated list is:")
print(my_list)
Expected Output:
The original list is:
['John', 12, 'Sam', True, 50.7]
Removing the element at index 2.
Now the list is:
['John', 12, True, 50.7]
Removing the element at index 3 from this updated list.
The updated list is:
['John', 12, True]
Goal:
❖ Removing an element from a list. Here we use remove() function.
# Removing an element using remove()function
my_list = ["John", 12, 25, 12, "Sam", True, 50.7]
print("The original list is:")
print(my_list)
print("Removing the first occurrence of 12 inside the list.")
my_list.remove(12)
print("Now the list is:")
print(my_list)
Expected Output:
The original list is:
['John', 12, 25, 12, 'Sam', True, 50.7]
Removing the first occurrence of 12 inside the list.
Now the list is:
['John', 25, 12, 'Sam', True, 50.7]
Goal:
❖ In Python, elements are case-sensitive.
❖ For example, in the following code, the remove() function can correctly remove the ‘Sam’ but not
the ‘sam’ which appeared before ‘Sam’ .
# Elements are case-sensitive
my_list = ["John", 12, "sam", 25.7, "Sam", True]
print("The original list is:")
print(my_list)
print("Removing the first occurrence of 'Sam' inside the list.")
my_list.remove('Sam')
print("Now the list is:")
print(my_list)
Expected Output:
The original list is:
['John', 12, 'sam', 25.7, 'Sam', True]
Removing the first occurrence of 'Sam' inside the list.
Now the list is:
['John', 12, 'sam', 25.7, True]
Goal:
❖ Removing an element from a list using pop() .
❖ Inside this function, you need to supply the index location.
# Removing an element using pop()
my_list = ["John", 12, 25, 12, "Sam", True, 50.7]
print("The original list is:")
print(my_list)
print("Removing an element inside the list at index 3 using pop().")
my_list.pop(3)
print("Now the list is:")
print(my_list)
Expected Output:
The original list is:
['John', 12, 25, 12, 'Sam', True, 50.7]
Removing an element inside the list at index 3 using pop().
Now the list is:
['John', 12, 25, 'Sam', True, 50.7]
Goal:
❖ To examine whether a particular element is currently present inside a list.
# Checking whether an element is present inside a list
my_list = ["John", "Bob", "Sam", "Ester", 1, 2, 3, 4]
print("Is 'Sam' present inside the list?")
print('Sam' in my_list) # True
print("Is 'Jeniffer' present inside the list?")
print('Jennifer' in my_list) # False
print("Is 3 present inside the list?")
print(3 in my_list) # True
print("Is 5 present inside the list?")
print(5 in my_list) # False
# Checking whether an element is absent inside a list
print("Is 'Jeniffer' NOT present inside the list?")
print('Jennifer' not in my_list) # True
Expected Output:
Is 'Sam' present inside the list?
True
Is 'Jeniffer' present inside the list?
False
Is 3 present inside the list?
True
Is 5 present inside the list?
False
Is 'Jeniffer' NOT present inside the list?
True
Goal:
❖ To find the maximum element and minimum element from a list.
# Finding the maximum and minimum from a list
# This list contains the numbers only
my_list = [1, 23, 56.2, -3.7, 999]
print("The original list is:")
print(my_list)
print(f"The largest number is:{max(my_list)}") # 999
print(f"The smallest number is:{min(my_list)}") # -3.7
print("----------------")
Expected Output:
The original list is:
[1, 23, 56.2, -3.7, 999]
The largest number is:999
The smallest number is:-3.7
Goal:
❖ Testing max(), min() on Boolean values.
# Testing booleans with max() and min()
my_list = [0.75, True, False, 0.5, 0.6, 1, 0]
print("The original list is:")
print(my_list)
print(f"The largest number is:{max(my_list)}") # True treated as 1
print(f"The smallest number is:{min(my_list)}") # False treated as 0
Expected Output:
The original list is:
[0.75, True, False, 0.5, 0.6, 1, 0]
The largest number is:True
The smallest number is:False
Goal:
❖ Let’s add one element at the end of a list.
❖ We use the append() function for this purpose. Initially, we append 25 at the end of the list.
Later we append another element “ Bob ” to the modified list.
# I want to add an element at the end of a list
my_list = ["John", 12, "Sam", True, 50.7]
print("The original list is:")
print(my_list)
print("Appending 25 at the end of the list.")
my_list.append(25)
print("Now the list is:")
print(my_list)
print("Appending another element 'Bob' now.")
my_list.append("Bob")
print("The modified list:")
print(my_list)
Expected Output:
The original list is:
['John', 12, 'Sam', True, 50.7]
Appending 25 at the end of the list.
Now the list is:
['John', 12, 'Sam', True, 50.7, 25]
Appending another element 'Bob' now.
The modified list:
['John', 12, 'Sam', True, 50.7, 25, 'Bob']
Goal:
❖ Using append(), you can add a single element only.
❖ But you can use this function to add a list that contains multiple elements. Let us see how it
looks.
my_list = ["John", 12, "Sam", True, 50.7]
print("The initial list is:")
print(my_list)
print("Appending [10,'Bob',100.2] at the end of list:")
my_list.append([10, 'Bob', 100.2])
print("The modified list:")
print(my_list)
Expected Output:
The initial list is:
['John', 12, 'Sam', True, 50.7]
Appending [10,'Bob',100.2] at the end of the list:
The modified list:
['John', 12, 'Sam', True, 50.7, [10, 'Bob', 100.2]]
Goal:
❖ You can add multiple elements to a list using the extend() function.
❖ Here is an example.
# You can add multiple elements to a list
# using extend() function.
# Here is an example.
my_list = ["John", 12, "Sam", True, 50.7]
print("The original list is:")
print(my_list)
print("Adding 10,'Bob', and 100.2 at the list end.")
my_list.extend([10, 'Bob', 100.2])
print("Now the list is:")
print(my_list)
Expected Output:
The original list is:
['John', 12, 'Sam', True, 50.7]
Adding 10,'Bob', and 100.2 at the list end.
Now the list is:
['John', 12, 'Sam', True, 50.7, 10, 'Bob', 100.2]
Goal:
❖ You saw that you can add the elements at the end of a list.
❖ But you have the option to add an element in a particular position. The insert() function is
useful in this case.
❖ In the following example, we insert an element “Jack” into a list at index location 3.
my_list = ["John", 12, "Sam", True, 50.7]
print("The original list is:")
print(my_list)
print("Adding the element 'Jack' at index 3.")
my_list.insert(3, "Jack")
print("Now the list is as follows:")
print(my_list)
Expected Output:
The original list is:
['John', 12, 'Sam', True, 50.7]
Adding the element 'Jack' at index 3.
Now the list is as follows:
['John', 12, 'Sam', 'Jack', True, 50.7]
Goal:
❖ You can sort your list that contains the same datatype. Here is an example.
my_list = [33, 11, 555, 77, 111, 333]
print("The initial list is:")
print(my_list)
print("Using sort() on my_list now.")
my_list.sort()
print("Now the list is:")
print(my_list)
Expected Output:
The initial list is:
[33, 11, 555, 77, 111, 333]
Using sort() on my_list now.
Now the list is:
[11, 33, 77, 111, 333, 555]
Goal:
❖ sort() works on the same data types and modifies the original list.
❖ If you want to prevent modification of the original list, you can use the sorted() function.
❖ Here is an example.
my_list = [33, 11, 555, 77, 111, 333]
#my_list = ["sam","bob","jack"]
print("Initially, my_list is:")
print(my_list)
print("Printing the sorted list now.")
print(sorted(my_list))
print("The my_list now:")
print(my_list)
Expected Output:
Initially, my_list is:
[33, 11, 555, 77, 111, 333]
Printing the sorted list now.
[11, 33, 77, 111, 333, 555]
The my_list now:
[33, 11, 555, 77, 111, 333]
Exercise #2 – Working with Tuples.
Note: - Tuples are another important data type and similar to lists. But there are some noticeable
differences which are as follows:
- Tuples are immutable. This means once created, you cannot incorporate changes in them. But
you have seen that lists can be modified. For example, you reassigned a value in a list, you
extended a list, etc. So, lists are mutable.
- When you create a tuple, you use codes something like the following (here We have chosen the
variable name as my_tuple ):
my_tuple = ("John", 12, "Sam", True, 50.7)
- You can see the declaring a tuple is similar to a list, but this time, we put elements
inside the round brackets - (, )
Goal:
❖ Declaring a tuple and printing the elements inside it.
my_tuple = ("John", 12, "Sam", True, 50.7)
print("The content of my_tuple is:")
print(my_tuple)
Expected Output:
The content of my_tuple is:
('John', 12, 'Sam', True, 50.7)
Goal:
❖ Let us access the tuple elements.
# Indexing is similar to lists
my_tuple = ("John", 12, "Sam", True, 50.7)
print("The content of my_tuple is:")
print(my_tuple)
# Printing the first element
print("The first element is:")
print(my_tuple[0])
print("The last element is:")
print(my_tuple[-1])
print("Printing the elements from index 1 to index 3.")
print(my_tuple[1:4])
print("Printing the elements from index 2 to end.")
print(my_tuple[2:])
Expected Output:
The content of my_tuple is:
('John', 12, 'Sam', True, 50.7)
The first element is:
John
The last element is:
50.7
Printing the elements from index 1 to index 3.
(12, 'Sam', True)
Printing the elements from index 2 to end.
('Sam', True, 50.7)
Goal:
❖ Let’s see that you cannot reassign the value inside a tuple.
# You cannot reassign the value inside a tuple.
my_tuple = ("John", 12, "Sam", True, 50.7)
print("The content of my_tuple is:")
print(my_tuple)
print("Trying to replace 'Sam' with 'Bob':")
my_tuple[2]= 'Bob' #error
Expected Output:
You are supposed to see the error similar to the following:
The content of my_tuple is:
('John', 12, 'Sam', True, 50.7)
Trying to replace 'Sam' with 'Bob':
Traceback (most recent call last):
File ...
Explanation:
Tuples are immutable by design. So, you cannot modify them.
Goal:
❖ Converting a list to a tuple.
my_list = ["John", 12, "Sam", True, 50.7]
print("The content of my_list is:")
print(my_list)
# Converting the list to a tuple
my_tuple=tuple(my_list)
print("The content of my_tuple is:")
print(my_tuple)
Expected Output:
The content of my_list is:
['John', 12, 'Sam', True, 50.7]
The content of my_tuple is:
('John', 12, 'Sam', True, 50.7)
Goal:
❖ Reversing a tuple.
my_tuple = (1, 2, 3, 4, 5)
print("The content of my_tuple is:")
print(my_tuple)
print("Reversing the tuple:")
rev_tuple = tuple(reversed(my_tuple))
print("The content of rev_tuple is:")
print(rev_tuple)
Expected Output:
The content of my_tuple is:
(1, 2, 3, 4, 5)
Reversing the tuple:
The content of rev_tuple is:
(5, 4, 3, 2, 1)
Exercise #3 – Working with Dictionaries.
Note: - Now you see the use of another important data type. You call it a dictionary.
- These are some noticeable characteristics of this datatype
=> It is a key-value pair.
=> The keys are unique.
=> Keys and values can be of any type.
=> Dictionaries are indexed through its keys.
- When you create a dictionary, you use codes something like the following (here we have
chosen the variable name as my_dictionary ):
my_dictionary = {1: "John", 2: 12, 3: "Sam", 4: True, 5: 50.7}
- You can see that we put elements inside the curly brackets –{ , } now.
- Let us examine some built-in functions for dictionaries which are as follows.
Goal:
❖ Let us create a dictionary and print the details inside it.
# A dictionary with 5 key-value pair
my_dictionary = {1: "John", 2: 12, 3: "Sam", 4: True, 5: 50.7}
print("The my_dictionary contains:")
print(my_dictionary)
Expected Output:
The my_dictionary contains:
{1: 'John', 2: 12, 3: 'Sam', 4: True, 5: 50.7}
Goal:
❖ Let’s use different types of keys in my_dictionary.
❖ In the following code segment, you’ll see that first three keys are numbers and remaining keys
are strings.
# A dictionary with 5 key-value pair
# Choosing different types of keys in the same dictionary
my_dictionary = {1: "John", 2: 12, 3: "Sam", 'fourth': True, 'fifth': 50.7}
print("The my_dictionary contains:")
print(my_dictionary)
print("----------------")
Expected Output:
The my_dictionary contains:
{1: 'John', 2: 12, 3: 'Sam', 'fourth': True, 'fifth': 50.7}
Goal:
❖ Let’s print values for particular keys in a dictionary.
# I want to print values for particular keys
my_dictionary = {1: "John", 2: 12, 3: "Sam", 'fourth': True, 'fifth': 50.7}
print("The my_dictionary contains:")
print(my_dictionary)
print("Value at key 1:", my_dictionary[1])
print("Value at key 2:", my_dictionary[2])
print("Value at key 3:", my_dictionary[3])
print("Value at key 'fourth':", my_dictionary['fourth'])
print("Value at key 'fifth'::", my_dictionary['fifth'])
Expected Output:
The my_dictionary contains:
{1: 'John', 2: 12, 3: 'Sam', 'fourth': True, 'fifth': 50.7}
Value at key 1: John
Value at key 2: 12
Value at key 3: Sam
Value at key 'fourth': True
Value at key 'fifth':: 50.7
Goal:
❖ Let’s assign different values for the same key in a dictionary and want to see the effect.
❖ We also want to check - how many elements are present in the dictionary?
# If you assign different values for the same keys
# last assigned value will be kept
my_dictionary = {1: "John", 2: "Sam", 3: "Jack",1: "Bob"}
print("The my_dictionary contains:")
print(my_dictionary)
print("Value at key 1:", my_dictionary[1])
print(f"Number of contents:{len(my_dictionary)}")
Expected Output:
The my_dictionary contains:
{1: 'Bob', 2: 'Sam', 3: 'Jack'}
Value at key 1: Bob
Number of contents:3
Exercise #4 – Working with Sets.
Note: - Sets are a special kind of datatype that does not hold duplicate values. You can think of it
as a combination of a list and a dictionary.
- For example, like dictionaries, you use curly brackets { and } but like lists, you do not
have any key-value pair. The following is an example of a set:
my_set1 = {1, 2, 3, "Jack", "Bob"}
- Alternatively, to create a set, you can use the built-in set() function like the following:
myset=set(iterable_element)
- Where “ iterable_element “indicates that you can use something like a list, or a tuple like
the following:
#Alternative way to create a set
#Using a list now to create a set
my_set = set([1, 2, 3, "Jack", "Bob"])
#Using a tuple to create a set
my_set = set((1, 2, 3, "Jack", "Bob"))
- Now go through the following code fragments to get some idea about set data types.
Goal:
❖ Let’s supply duplicate values to sets. Then let’s test whether these sets contain duplicates.
my_set1 = {1, 2, 3, "Jack", 2, "Bob", 3, 1}
print("The my_set1 contains:")
print(my_set1)
my_set2 = {"Sam", "Bob", "Jack", "Sam", "Jack", "Ester"}
print("The my_set2 contains:")
print(my_set2)
Expected Output:
The my_set1 contains:
{1, 2, 3, 'Jack', 'Bob'}
The my_set2 contains:
{'Sam', 'Jack', 'Bob', 'Ester'}
Goal:
❖ Sets are mutable.
❖ In the following example, we add 6 to the list and then remove 2 from the set.
# Sets are mutable
my_set = {1, 2, 3, 4, 5}
#my_set = {}#it is treated as empty dictionary
#my_set=set()#this is ok for an empty set
print("The my_set contains:")
print(my_set)
print("Adding 6 to the set now.")
my_set.add(6)
print("Now the set is:")
print(my_set)
print("Removing 2 from the set now.")
my_set.remove(2)
print("Now the my_set is:")
print(my_set)
Expected Output:
The my_set contains:
{1, 2, 3, 4, 5}
Adding 6 to the set now.
Now the set is:
{1, 2, 3, 4, 5, 6}
Removing 2 from the set now.
Now, the my_set is:
{1, 3, 4, 5, 6}
Goal:
❖ You can iterate over strings. So, you can pass a string argument to the set() function.
# Strings are iterable. So, you can use strings to set().
my_str = "vaskaran"
my_set = set(my_str)
print("The my_set contains:")
print(my_set)
Expected Output:
The my_set contains:
{'n', 's', 'k', 'a', 'v', 'r'}
Goal:
❖ In the previous code segment, you saw that sets are unordered.
❖ Now you see that you cannot access the set elements referring to an index.
❖ Here is an example.
# Sets are unordered. You cannot access the elements by referring to an index
my_set = set([2,"abc",1,5])
print("The my_set contains:")
print(my_set)
print("Trying to access the 0th element.")
print(my_set[0])#error
Expected Output:
The my_set contains:
{1, 2, 5, 'abc'}
Trying to access the 0th element.
Traceback (most recent call last):
File
"path/file.py",
line 59, in <module>
print(my_set[0])#error
TypeError: 'set' object is not subscriptable
Goal:
❖ You cannot access set elements by referring to an index. But You can loop through the elements.
❖ Here is an example.
# You cannot access set elements by referring to an index.
# But You can loop through the elements.
my_set = set([2, "hello", 1, 5])
print("The my_set contains:")
for item in my_set:
print(item)
Expected Output:
The my_set contains:
{1, 2, 'hello', 5}
Trying to access the 0th element.
The my_set contains:
1
2
hello
5
Goal:
❖ Removing an element from a set.
❖ Here let’s see the use of both remove and discard.
❖ The difference is: discard() does not raise an error if the element is not present, but remove()
raises an error in that case.
# Remove an element from a set
my_set = set([1,2,3,4,5])
print("The my_set contains:")
print(my_set)
print("Removing 5 using remove().")
my_set.remove(5)
print("Now the my_set contains:")
print(my_set)
print("Removing 4 now using discard().")
my_set.discard(4)
print("Now the my_set contains:")
print(my_set)
Expected Output:
The my_set contains:
{1, 2, 3, 4, 5}
Removing 5 using remove().
Now the my_set contains:
{1, 2, 3, 4}
Removing 4 now using discard().
Now the my_set contains:
{1, 2, 3}
Exercises
1. Create a list that contains more than 2 elements. Then remove the last two elements from the list.
2. Create a tuple with more than 5 elements. Then print the 3rd element of it. Can you print the 3rd
element from last?
3. Create a tuple with elements 1,2,2,3,4,4,4,5. Then reverse the tuple and print how many times 4
appears in this tuple.
4. How a tuple is different from a list?
5. Predict the output:
my_tuple = (1, 2, 2, 3, 4, 4, 5)
my_set = set(my_tuple)
print("The my_set is:")
print(my_set)
6. Can you get any error for the following code?
my_list=["red", "blue"]
my_set = set(my_list)
print("The my_set is:")
print(my_set)
my_set.discard("green")
7. Create a dictionary and print the details. Can you print the value for a particular key?
Solution to Exercises
1
my_list = ["John", 12, 25, 12,"Sam", True, 50.7]
print("The original list is:")
print(my_list)
#Removing the last two elements from the list
del(my_list[-2:])
print("Now the list is:")
print(my_list)
Output:
The original list is:
['John', 12, 25, 12, 'Sam', True, 50.7]
Now the list is:
['John', 12, 25, 12, 'Sam']
2
my_tuple = ("John", 12, 25, 12, "Sam", True, 50.7)
print("The original tuple is:")
print(my_tuple)
#Printing the 3rd element
print("The third element:")
print(my_tuple[2])
print("The third element from last:")
print(my_tuple[-3])
3
my_tuple = (1, 2, 2, 3, 4, 4, 4, 5)
print("The original tuple is:")
print(my_tuple)
print("The reversed tuple is:")
rev_tuple = tuple(reversed(my_tuple))
print(rev_tuple)
print(f"The number of 4 in this tuple:{rev_tuple.count(4)}")
4
Tuples are immutable but lists are mutable.
5
The my_set is:
{1, 2, 3, 4, 5}
Explanation:
Sets do not contain duplicates.
6
The element “green” is not present in the set. If you use remove() , you see a
KeyError , but discard() does not raise any such error. The discard()
method removes the element if it is present in the set.
7
Try it yourself.