forked from dabeaz-course/practical-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlists.py
More file actions
34 lines (24 loc) · 989 Bytes
/
lists.py
File metadata and controls
34 lines (24 loc) · 989 Bytes
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
#lists.py
# This script demonstrates basic list operations in Python.
names = ["Alice", "Bob", "Charlie", "Diana"]
nums = [1, 2, 3, 4, 5]
#split string into list
line = 'GOOG,100,490.10'
row = line.split(',')
print(row) # Output: ['GOOG', '100', '490.10']
names.append("Eve")
print(names) # Output: ['Alice', 'Bob', 'Charlie', 'Diana', 'Eve']
names.insert(2, "Frank")
print(names) # Output: ['Alice', 'Bob', 'Frank', 'Charlie', 'Diana', 'Eve']
names.remove("Bob")
print(names) # Output: ['Alice', 'Frank', 'Charlie', 'Diana', 'Eve']
names[1] = "Grace"
print(names) # Output: ['Alice', 'Grace', 'Charlie', 'Diana', 'Eve']
for name in names:
print("Hello " + name)
names.index("Charlie") # Output: 2
sorted_names = sorted(names) # names remains unchanged
names.sort()
print("sorted names:", names) # Output: ['Alice', 'Charlie', 'Diana', 'Eve', 'Grace']
nums = nums * 2
print("nums are not doubled, use numpy instead:", nums) # Output: [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]