-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path05_lists.py
More file actions
142 lines (113 loc) · 3.16 KB
/
05_lists.py
File metadata and controls
142 lines (113 loc) · 3.16 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
# Lists - storing multiple items in one variable
# Creating lists
fruits = ["apple", "banana", "cherry"]
numbers = [1, 2, 3, 4, 5]
mixed = [1, "hello", 3.14, True]
empty = []
print(fruits)
# Accessing items (indexing starts at 0)
print(fruits[0]) # apple
print(fruits[1]) # banana
print(fruits[-1]) # cherry (last item)
print(fruits[-2]) # banana (second from end)
# Changing items
fruits[1] = "blueberry"
print(fruits)
# List length
print(len(fruits))
# Adding items
fruits.append("orange") # add to end
print(fruits)
fruits.insert(1, "mango") # insert at position
print(fruits)
# Removing items
fruits.remove("cherry") # remove by value
print(fruits)
last_fruit = fruits.pop() # remove and return last item
print(last_fruit)
print(fruits)
fruits.pop(0) # remove item at index 0
print(fruits)
# Checking if item exists
if "apple" in fruits:
print("We have apples")
else:
print("No apples")
# Looping through lists
print("\nAll fruits:")
for fruit in fruits:
print(f"- {fruit}")
# Loop with index
for i in range(len(fruits)):
print(f"{i}: {fruits[i]}")
# Better way with enumerate
for index, fruit in enumerate(fruits):
print(f"{index}: {fruit}")
# List slicing
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(numbers[2:5]) # [2, 3, 4]
print(numbers[:4]) # [0, 1, 2, 3]
print(numbers[5:]) # [5, 6, 7, 8, 9]
print(numbers[::2]) # [0, 2, 4, 6, 8] (every 2nd item)
print(numbers[::-1]) # reverse the list
# Sorting
scores = [85, 92, 78, 95, 88]
scores.sort() # sorts in place
print(scores)
scores.sort(reverse=True) # descending order
print(scores)
# Sorted function (creates new list)
original = [3, 1, 4, 1, 5]
sorted_list = sorted(original)
print(original) # unchanged
print(sorted_list) # sorted
# List methods
numbers = [1, 2, 3, 2, 4, 2, 5]
print(numbers.count(2)) # count occurrences
print(numbers.index(4)) # find index of value
numbers.reverse() # reverse in place
print(numbers)
# Joining lists
list1 = [1, 2, 3]
list2 = [4, 5, 6]
combined = list1 + list2
print(combined)
list1.extend(list2) # add list2 items to list1
print(list1)
# Copying lists
original = [1, 2, 3]
copy = original.copy() # or original[:]
copy.append(4)
print(original) # [1, 2, 3]
print(copy) # [1, 2, 3, 4]
# List comprehension (advanced but useful)
squares = [x**2 for x in range(10)]
print(squares)
even_numbers = [x for x in range(20) if x % 2 == 0]
print(even_numbers)
# Practical example - shopping list
shopping_list = []
while True:
print("\n--- Shopping List ---")
print("1. Add item")
print("2. Remove item")
print("3. View list")
print("4. Done")
choice = input("Choose: ")
if choice == "1":
item = input("Item to add: ")
shopping_list.append(item)
print(f"Added {item}")
elif choice == "2":
item = input("Item to remove: ")
if item in shopping_list:
shopping_list.remove(item)
print(f"Removed {item}")
else:
print("Item not found")
elif choice == "3":
print("\nYour shopping list:")
for i, item in enumerate(shopping_list, 1):
print(f"{i}. {item}")
elif choice == "4":
break