-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path05_lists_tutorial.py
More file actions
146 lines (111 loc) · 3.9 KB
/
05_lists_tutorial.py
File metadata and controls
146 lines (111 loc) · 3.9 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
# 1. Creating lists
# List - ordered mutable collection of elements
empty_list = []
numbers = [1, 2, 3, 4, 5]
mixed = [1, "text", 3.14, True, [1, 2]] # Can contain different types
print(f"Empty list: {empty_list}")
print(f"Numbers: {numbers}")
print(f"Mixed list: {mixed}")
# 2. Accessing elements (indexing)
fruits = ["apple", "banana", "orange", "pear"]
print(f"First element: {fruits[0]}") # apple
print(f"Last element: {fruits[-1]}") # pear
print(f"Second from end: {fruits[-2]}") # orange
# 3. Slicing
print(f"First two elements: {fruits[0:2]}") # ['apple', 'banana']
print(f"From third to end: {fruits[2:]}") # ['orange', 'pear']
print(f"Last two: {fruits[-2:]}") # ['orange', 'pear']
print(f"Every second: {fruits[::2]}") # ['apple', 'orange']
print(f"In reverse order: {fruits[::-1]}") # ['pear', 'orange', 'banana', 'apple']
# 4. Modifying elements
numbers = [1, 2, 3, 4, 5]
numbers[0] = 10 # Change first element
print(f"After modification: {numbers}")
numbers[1:3] = [20, 30] # Change slice
print(f"After slice modification: {numbers}")
# 5. Adding elements
fruits = ["apple", "banana"]
# append() - add to end
fruits.append("orange")
print(f"After append: {fruits}")
# insert() - insert at specific position
fruits.insert(1, "pear") # Insert at index 1
print(f"After insert: {fruits}")
# extend() - add multiple elements
fruits.extend(["kiwi", "mango"])
print(f"After extend: {fruits}")
# Alternative to extend - concatenation
fruits = fruits + ["lemon"]
print(f"After concatenation: {fruits}")
# 6. Removing elements
numbers = [1, 2, 3, 4, 5, 3]
# remove() - remove first occurrence of value
numbers.remove(3) # Will remove first 3
print(f"After remove(3): {numbers}")
# pop() - remove by index and return value
last = numbers.pop() # Without argument removes last
print(f"Removed: {last}, remaining: {numbers}")
second = numbers.pop(1) # Remove element at index 1
print(f"Removed: {second}, remaining: {numbers}")
# del - delete element or slice
numbers = [1, 2, 3, 4, 5]
del numbers[0] # Delete first element
print(f"After del: {numbers}")
# clear() - clear entire list
numbers.clear()
print(f"After clear: {numbers}")
# 7. Finding elements
fruits = ["apple", "banana", "orange", "banana"]
# in - check presence
print(f"'banana' in fruits: {'banana' in fruits}") # True
# index() - find index of first occurrence
index = fruits.index("banana")
print(f"Index of 'banana': {index}")
# count() - count number of occurrences
count = fruits.count("banana")
print(f"Count of 'banana': {count}")
# 8. Sorting
numbers = [5, 2, 8, 1, 9, 3]
# sort() - sorts list in place (modifies original)
numbers.sort()
print(f"After sort(): {numbers}")
numbers.sort(reverse=True) # In reverse order
print(f"After sort(reverse=True): {numbers}")
# sorted() - returns new sorted list (doesn't modify original)
original = [5, 2, 8, 1]
sorted_list = sorted(original)
print(f"Original: {original}, sorted: {sorted_list}")
# 9. Other useful methods
numbers = [1, 2, 3]
# reverse() - reverse list
numbers.reverse()
print(f"After reverse(): {numbers}")
# copy() - create copy of list
copy = numbers.copy()
print(f"Copy: {copy}")
# 10. List length
print(f"len(fruits): {len(fruits)}")
# 11. Lists as stack (LIFO - Last In, First Out)
stack = []
stack.append(1) # Push to stack
stack.append(2)
stack.append(3)
print(f"Stack: {stack}")
top = stack.pop() # Pop from stack
print(f"Popped: {top}, remaining: {stack}")
# 12. Multidimensional lists (lists of lists)
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
print(f"First row: {matrix[0]}")
print(f"Element [1][2]: {matrix[1][2]}") # 6
# 13. List unpacking
fruits = ["apple", "banana", "orange"]
first, second, third = fruits
print(f"first: {first}, second: {second}, third: {third}")
# With * you can collect remaining elements
numbers = [1, 2, 3, 4, 5]
first, *rest, last = numbers
print(f"first: {first}, rest: {rest}, last: {last}")