Skip to content

Commit be2be74

Browse files
Allen WangAllen Wang
Allen Wang
authored and
Allen Wang
committed
Quick preliminary changes for data structures snippets
1 parent 4fcd4f5 commit be2be74

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

snippets/data_structures.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Data structure snippets
2+
def list_ex():
3+
# Ways to initialize a list
4+
my_empty_list = []
5+
my_non_empty_list = [1, 4, 9, 16, 25]
6+
7+
print(my_empty_list)
8+
print(my_non_empty_list)
9+
10+
# You can index and slice a list
11+
print(my_non_empty_list[0]) # Returns the first element
12+
print(my_non_empty_list[-1]) # Returns the last element
13+
print(my_non_empty_list[-3:]) # Slicing returns a new list
14+
15+
# Lists also support concatenation
16+
print(my_non_empty_list + [5, 6, 11, 13])
17+
18+
19+
def dict_ex():
20+
# Ways to initialize a dictionary
21+
my_dict = {}
22+
23+
def tuple_ex():
24+
# Ways to initialize a tuple
25+
my_tuple = ()
26+
27+
def set_ex():
28+
# Ways to initialize a set
29+
my_set = {1,2}
30+
31+
def counter_ex():
32+
# Ways to initialize a counter
33+
my_counter = Counter()
34+
35+
36+
37+
list_ex()
38+
dict_ex()
39+
tuple_ex()
40+
set_ex()

0 commit comments

Comments
 (0)