Experiment 02
AIM - Mastering Python New Data Structures for Practical Applications
RESOURCE REQUIREMENT - Visual Studio Code, Windows 10
THEORY -
In Python, lists and tuples are both used to store collections of items. However,
they differ in their mutability and usage.
Lists:
Mutable: Lists can be changed after they are created (you can add, remove, or
change items).
Syntax: Defined using square brackets [].
1. Adding elements to a list:
○ append(): Adds an item to the end of the list.
○ insert(): Adds an item at a specific index.
2. Removing elements from a list:
○ remove(): Removes an element by its value.
○ pop(): Removes and returns an element by index.
○ del: Deletes an element by index.
3. Updating elements in a list:
○ You can directly access the index and assign a new value to update the element.
4. Sorting a list:
○ sort(): Sorts the list in place in ascending order by default. Use
reverse=True to sort in descending order.
Basic Operations with Lists:
Creating a list:
my_list = [1, 2, 3, 4, 5]
Accessing elements: You can access list elements by indexing (starting from 0).
print(my_list[0]) # Output: 1
print(my_list[2]) # Output: 3
Adding elements:
my_list.append(6) # Adds 6 to the end of the list
my_list.insert(1, 10) # Inserts 10 at index 1
Removing elements:
my_list.remove(3) # Removes the first occurrence of 3
my_list.pop() # Removes and returns the last element
my_list.pop(0) # Removes the element at index 0
del my_list[1] # Deletes the element at index 1
Updating elements:
my_list[0] = 100 # Updates the element at index 0 to 100
Sorting:
my_list.sort() # Sorts the list in ascending order
my_list.sort(reverse=True) # Sorts in descending order
---------------------------------------------------------------------------------------------------------------------
Tuples:
Immutable: Once a tuple is created, you cannot modify, add, or remove
elements.
Syntax: Defined using parentheses ().
○ Tuples are immutable, meaning you can't modify, add, or remove items directly.
However, you can convert the tuple to a list, modify the list, and then convert it
back to a tuple.
5. Sorting a tuple:
○ You can use the sorted() function, which returns a sorted list. You can convert
it back to a tuple if needed.
Basic Operations with Tuples:
Creating a tuple:
my_tuple = (1, 2, 3, 4, 5)
Accessing elements:
print(my_tuple[0]) # Output: 1
print(my_tuple[2]) # Output: 3
Tuples are immutable:
You cannot modify the elements of a tuple directly.
However, you can convert a tuple to a list, modify it, and then convert it back to a tuple.
Sorting a tuple (returns a new sorted list):
sorted_tuple = tuple(sorted(my_tuple)) # Sorted tuple in ascending order
Concatenating and repeating:
You can concatenate tuples:
new_tuple = my_tuple + (6, 7, 8)
You can also repeat a tuple:
repeated_tuple = my_tuple * 2 # Repeat the tuple twice
Comparison of Lists and Tuples:
---------------------------------------------------------------------------------------------------------------------
Sets:
In Python, sets are collections of unordered and unique elements. They are similar to lists or
dictionaries but do not allow duplicate values and do not maintain any specific order. Sets are
useful when you need to store a collection of items without duplicates and when you want to
perform set operations like union, intersection, and difference.
Sets are collections of distinct objects or elements, often represented as unordered and without
repetition. You can perform various set operations on them, such as:
1. Union ( ∪ )
This operation combines all elements from both sets, removing duplicates.
● Example:
Let A={1,2,3}A = \{1, 2, 3\}A={1,2,3} and B={3,4,5}B = \{3, 4, 5\}B={3,4,5}
A∪B={1,2,3,4,5}A \cup B = \{1, 2, 3, 4, 5\}A∪B={1,2,3,4,5}
2. Intersection ( ∩ )
This operation finds the common elements between two sets.
● Example:
Let A={1,2,3}A = \{1, 2, 3\}A={1,2,3} and B={3,4,5}B = \{3, 4, 5\}B={3,4,5}
A∩B={3}A \cap B = \{3\}A∩B={3}
3. Difference ( − )
This operation returns the elements that are in the first set but not in the second.
● Example:
Let A={1,2,3}A = \{1, 2, 3\}A={1,2,3} and B={3,4,5}B = \{3, 4, 5\}B={3,4,5}
A−B={1,2}A - B = \{1, 2\}A−B={1,2}
4. Symmetric Difference ( Δ )
This operation returns elements that are in either of the sets, but not in both.
● Example:
Let A={1,2,3}A = \{1, 2, 3\}A={1,2,3} and B={3,4,5}B = \{3, 4, 5\}B={3,4,5}
AΔB={1,2,4,5}A \Delta B = \{1, 2, 4, 5\}AΔB={1,2,4,5}
—----------------------------------------------------------------------------------------------------------------------------
Dictionaries:
Dictionaries in Python are unordered collections of key-value pairs. Each key must be
unique, and the value associated with each key can be any type of object. Dictionaries
are incredibly useful for storing and retrieving data in a structured way.
Basic Syntax:
my_dict = {
'key1': 'value1',
'key2': 'value2',
'key3': 'value3'
}
Key Operations on Dictionaries:
1. Accessing Values
You can access the value associated with a key using square brackets ([]) or the
get() method.
2. Adding or Updating Items
You can add new key-value pairs or update existing ones.
3. Removing Items
You can remove key-value pairs using del, pop(), or popitem().
● del: Removes a key-value pair.
● pop(): Removes the key and returns its associated value.