-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollections.py
More file actions
159 lines (136 loc) · 5.09 KB
/
Copy pathCollections.py
File metadata and controls
159 lines (136 loc) · 5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# =====================================================================
# FILE: Collections.py
# DESCRIPTION: Lists, tuples, dictionaries, sets, slicing, unpacking, and set operations.
#
# SYNTAX QUICK-REFERENCE:
# # Slicing: list[start:stop:step]
# nums = [1, 2, 3, 4, 5]
# print(nums[1:4]) # [2, 3, 4]
# print(nums[::-1]) # [5, 4, 3, 2, 1] (reversed)
#
# # Tuple Unpacking and Swapping
# x, y = 10, 20
# x, y = y, x # Swap variables
#
# # Set operations
# a = {1, 2, 3}
# b = {3, 4, 5}
# print(a | b) # Union {1, 2, 3, 4, 5}
# print(a & b) # Intersection {3}
# print(a - b) # Difference {1, 2}
# =====================================================================
# Collections.py
# Reference Guide: Lists, Tuples, Dictionaries, Sets, Indexing, Slicing, and Comprehensions
# ==========================================
# 1. LISTS & SLICING
# ==========================================
# A list is an ordered, changeable collection that allows duplicates.
print("--- 1. LISTS & SLICING ---")
fruits = ["apple", "banana", "cherry"]
nums = [1, 2, 3, 4, 5]
# Indexing (Positive and Negative)
print(f"Index 0: {fruits[0]}, Index 1: {fruits[1]}, Index -1: {fruits[-1]}")
# Slicing: list[start:stop:step]
print(f"nums[1:4] -> {nums[1:4]}") # [2, 3, 4]
print(f"nums[:3] -> {nums[:3]}") # [1, 2, 3]
print(f"nums[2:] -> {nums[2:]}") # [3, 4, 5]
print(f"nums[::2] -> {nums[::2]}") # [1, 3, 5] (every second item)
print(f"nums[::-1] -> {nums[::-1]}") # [5, 4, 3, 2, 1] (reversed list)
# List Methods
fruits.append("mango") # Add to end
fruits.insert(1, "orange") # Add at index 1
print(f"After additions: {fruits}")
fruits.remove("banana") # Remove by value
fruits.pop() # Remove last item
fruits.pop(1) # Remove by index
print(f"After removals: {fruits}")
# Sorting & Reversing
list_to_sort = [3, 1, 4, 2]
list_to_sort.sort()
print(f"Sorted: {list_to_sort}")
# Looping with index (enumerate)
for idx, fruit in enumerate(fruits):
print(f" Index {idx}: {fruit}")
# List Comprehension
squares = [i * i for i in range(1, 6)]
evens = [i for i in range(1, 11) if i % 2 == 0]
print(f"Squares list comprehension: {squares}")
print(f"Evens list comprehension: {evens}")
# Nested Lists (2D List)
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
print(f"Matrix [1][2] (row index 1, col index 2): {matrix[1][2]}")
print()
# ==========================================
# 2. TUPLES
# ==========================================
# A tuple is ordered and immutable (cannot be changed). Allows duplicates.
print("--- 2. TUPLES ---")
fruits_tuple = ("apple", "banana", "cherry")
single_item_tuple = (42,) # ⚠️ Comma is mandatory for single item tuples
print(f"Tuple: {fruits_tuple}, Single item tuple: {single_item_tuple}")
# Tuple Unpacking
a, b, c = fruits_tuple
print(f"Unpacked: a={a}, b={b}, c={c}")
# Swap variables using tuples:
x, y = 10, 20
x, y = y, x
print(f"Swapped via tuple packing/unpacking: x={x}, y={y}")
print()
# ==========================================
# 3. DICTIONARIES
# ==========================================
# A dictionary stores data as key: value pairs. Ordered, mutable, no duplicate keys.
print("--- 3. DICTIONARIES ---")
student = {
"name": "Alice",
"age": 20,
"grade": "A"
}
# Accessing Values
print(f"Brackets access: {student['name']}")
print(f"Safe get method: {student.get('age')}")
print(f"Safe get with fallback: {student.get('city', 'Not Found')}") # Returns "Not Found" instead of raising KeyError
# Adding/Updating & Deleting
student["city"] = "NY" # Add new key
student["age"] = 25 # Update key
print(f"Updated dictionary: {student}")
student.pop("age") # Remove by key
print(f"Dictionary after pop: {student}")
# Looping through Dictionary
print("Looping items:")
for key, value in student.items():
print(f" {key}: {value}")
# Dictionary Comprehension
squares_dict = {i: i*i for i in range(1, 4)}
print(f"Dict Comprehension: {squares_dict}")
print()
# ==========================================
# 4. SETS
# ==========================================
# A set is unordered and unindexed. No duplicate items allowed.
print("--- 4. SETS ---")
nums_set = {1, 2, 2, 3, 3, 3}
print(f"Unique values only: {nums_set}") # {1, 2, 3}
empty_set = set() # ⚠️ Note: {} creates an empty dict, not a set!
# Set Methods
nums_set.add(4)
nums_set.remove(2) # Raises KeyError if not present
nums_set.discard(10) # Safe discard, does not raise error if not present
print(f"Modified Set: {nums_set}")
# Set Operations
set_a = {1, 2, 3, 4}
set_b = {3, 4, 5, 6}
print(f"Set A: {set_a}, Set B: {set_b}")
print(f" Union (a | b): {set_a | set_b}") # All elements
print(f" Intersection (a & b): {set_a & set_b}") # Common elements
print(f" Difference (a - b): {set_a - set_b}") # Elements in A but not B
print(f" Symmetric Diff (a ^ b):{set_a ^ set_b}") # Elements unique to either set
# Remove duplicates from list (Trick)
duplicate_list = [1, 2, 2, 3, 3, 3, 4]
clean_list = list(set(duplicate_list))
print(f"List with duplicates: {duplicate_list} -> Clean list: {clean_list}")
print()