-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path04_lists.py
46 lines (38 loc) · 1.24 KB
/
04_lists.py
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
# Initialize it
test_list = [1, 2, 3]
# Append 4
test_list.append(4)
# Insert 0 at 0th position
test_list.insert(0, 0)
print("Test list: {}\n".format(test_list))
# Initialize it using its class name
next_list = list()
next_list.append(7)
next_list.append(2)
next_list.append(5)
next_list.append(1)
print("Next list: {}".format(next_list))
print("Sorted next list (does not mutate): {}\n".format(sorted(next_list)))
print("So here's next_list: {}".format(next_list))
if 3 in next_list:
print("3 is here!")
else:
print("No 3 :(")
if 2 in next_list:
print("2 is here!")
else:
print("No 2 :(")
# Did you know lists can contain *any* data type?
next_list.insert(0, "test")
next_list.append(12)
next_list.append(25)
next_list.append(-4)
next_list.append([2, 5])
## Slicing syntax: [start:stop:step]. Note that stop is exclusive -- the element
# at that index is not included
print("\nRemember our dear list {}?\n".format(next_list))
print("List, but no first element: {}".format(next_list[1:]))
print("List, but no last element: {}".format(next_list[:len(next_list)-1]))
print("List, some middle elements: {}".format(next_list[2:6]))
print("List, but every other element: {}".format(next_list[::2]))
print("List, but reversed: {}".format(next_list[::-1]))