FE_Experiment 02
FE_Experiment 02
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).
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:
Removing elements:
Updating elements:
Sorting:
---------------------------------------------------------------------------------------------------------------------
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 ( ∩ )
● 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'
}
1. Accessing Values
You can access the value associated with a key using square brackets ([]) or the
get() method.
3. Removing Items