Part2 - Python List, Tuple, Set, Dictionary & Slicing
Part2 - Python List, Tuple, Set, Dictionary & Slicing
Python, including methods, examples, and practical use cases. This overview aims
to provide a comprehensive understanding within a reasonable length.
List
Description: A list in Python is a mutable, ordered collection of items. It allows duplicate
elements and provides various methods for manipulation and traversal.
Example:
python
my_list = [1, 2, 3, 4, 5]
python
my_list.append(6)
# Result: [1, 2, 3, 4, 5, 6]
Extend: Appends elements from another iterable to the end of the list.
python
another_list = [7, 8, 9]
my_list.extend(another_list)
# Result: [1, 2, 3, 4, 5, 6, 7, 8, 9]
python
my_list.insert(2, 10)
# Result: [1, 2, 10, 3, 4, 5, 6, 7, 8, 9]
python
my_list.remove(3)
# Result: [1, 2, 10, 4, 5, 6, 7, 8, 9]
Pop: Removes and returns an element at a specified index (default is last element).
python
Krati Maheshwari
PYTHON PART - 2
popped_value = my_list.pop(1)
# Result: popped_value = 2, my_list = [1, 10, 4, 5, 6, 7, 8, 9]
python
index = my_list.index(5)
# Result: index = 4
python
count = my_list.count(4)
# Result: count = 1
python
my_list.sort()
# Result: [1, 4, 5, 6, 7, 8, 9, 10]
python
my_list.reverse()
# Result: [10, 9, 8, 7, 6, 5, 4, 1]
python
copied_list = my_list.copy()
# Result: copied_list = [10, 9, 8, 7, 6, 5, 4, 1]
Tuple
Description: A tuple in Python is an immutable, ordered collection of items. Once created, its
elements cannot be changed.
Krati Maheshwari
PYTHON PART - 2
Syntax: Tuples are defined using parentheses ().
Example:
python
my_tuple = (1, 2, 3, 4, 5)
Count:
python
my_tuple = (1, 2, 2, 3, 4, 2)
count = my_tuple.count(2)
print(count) # Output: 3
Index:
python
my_tuple = (1, 2, 3, 4, 5, 2)
index = my_tuple.index(2)
print(index) # Output: 1 (index of the first occurrence of 2)
Concatenation:
Description: Tuples can be concatenated using the + operator to create a new tuple.
Example:
python
tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)
concatenated_tuple = tuple1 + tuple2
print(concatenated_tuple) # Output: (1, 2, 3, 4, 5, 6)
Krati Maheshwari
PYTHON PART - 2
Repetition:
Description: Tuples can be repeated using the * operator to create a new tuple with
repeated elements.
Example:
python
tuple1 = ('a', 'b')
repeated_tuple = tuple1 * 3
print(repeated_tuple) # Output: ('a', 'b', 'a', 'b', 'a', 'b')
Membership Test (in and not in):
Description: Tuples support membership testing to check if a value exists in the tuple.
Example:
python
my_tuple = (1, 2, 3, 4, 5)
print(3 in my_tuple) # Output: True
print(6 not in my_tuple) # Output: True
Set
Description: A set in Python is an unordered collection of unique elements. It is mutable,
allowing for dynamic addition and removal of items.
Syntax: Sets are defined using curly braces {} or the set() function.
Example:
python
my_set = {1, 2, 3, 4, 5}
python
my_set.add(6)
# Result: {1, 2, 3, 4, 5, 6}
Krati Maheshwari
PYTHON PART - 2
Remove: Removes a specified element from the set. Raises KeyError if the element is
not present.
python
my_set.remove(3)
# Result: {1, 2, 4, 5, 6}
python
my_set.discard(2)
# Result: {1, 4, 5, 6}
python
set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1.union(set2)
# Result: {1, 2, 3, 4, 5}
python
intersection_set = set1.intersection(set2)
# Result: {3}
Difference: Returns a new set with elements in the set that are not in the other set.
python
difference_set = set1.difference(set2)
# Result: {1, 2}
python
my_set.clear()
# Result: set()
Krati Maheshwari
PYTHON PART - 2
Dictionary
Description: A dictionary in Python is a mutable, unordered collection of key-value pairs. Each
key must be unique, but values can be duplicated.
Syntax: Dictionaries are defined using curly braces {}, with key-value pairs separated by colons
: (key: value).
Example:
python
my_dict = {'name': 'Alice', 'age': 30, 'city': 'New York'}
python
print(my_dict['name']) # Accessing value by key
# Result: 'Alice'
python
my_dict['gender'] = 'Female' # Adding a new key-value pair
my_dict['age'] = 31 # Modifying an existing value
Removing Entries:
python
del my_dict['city'] # Deleting a specific key-value pair
my_dict.pop('age') # Removing and returning the value of a specific key
python
keys = my_dict.keys() # Returns a view object of all keys
values = my_dict.values() # Returns a view object of all values
python
my_dict.clear()
# Result: {}
Krati Maheshwari
PYTHON PART - 2
python
copied_dict = my_dict.copy()
# Result: {'name': 'Alice', 'age': 30, 'city': 'New York'}
SLICING
Slicing is a powerful operation in Python that allows you to extract a portion of a sequence like
strings, lists, tuples, or other iterable objects. It provides a flexible way to access multiple
elements based on their indices. Here’s a detailed explanation of slicing and its application with
examples:
Syntax of Slicing
python
sequence[start:stop:step]
start: Optional. The starting index of the slice. If omitted, it defaults to 0 (beginning of
the sequence).
stop: Required. The ending index of the slice. The slice extends up to, but does not
include, this index.
step: Optional. The step size used to skip elements in the sequence. If omitted, it defaults
to 1.
Examples of Slicing
1. Slicing Lists
python
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
Krati Maheshwari
PYTHON PART - 2
slice2 = my_list[:4]
print(slice2) # Output: [1, 2, 3, 4]
2. Slicing Tuples
python
my_tuple = (1, 2, 3, 4, 5)
3. Slicing Strings
python
my_string = "Hello, World!"
Krati Maheshwari
PYTHON PART - 2
Krati Maheshwari