-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataTypes.py
148 lines (119 loc) · 2.86 KB
/
dataTypes.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
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
147
148
# basic examples of different python data types
# ---------------------------- string ----------------------------
str1 = "Hello world!"
str2 = "My name is Sophie."
str3 = str1 + " " + str2
print(str3)
print(str1, str2)
print(f"{str1} {str2}")
# ---------------------------- int ----------------------------
int1 = 4
int2 = 2
int3 = int1 + int2
print(int1, "plus", int2, "equals", int3)
int4 = int1 - int2
print(int1, "minus", int2, "equals", int4)
int3 = int1 * int2
print(int1, "times", int2, "equals", int3)
int4 = int1 / int2
print(int1, "divided (/)", int2, "equals", int4)
int4 = int1 // int2
print(int1, "divided (//)", int2, "equals", int4)
# ---------------------------- list ----------------------------
list1 = ["apples", "oranges", "bananas"]
print(list1)
print(len(list1))
print(list1[1])
list2 = ["eggplants", "cucumbers", "capsicums"]
# list of lists
list3 = [list1, list2]
print(list3)
# iteration
for i in range(len(list3)):
print(list3[i])
for count, ele in enumerate(list3):
print(count, ele)
for i in range(len(list3)):
for j in range(len(list3[i])):
print(list3[i][j])
# string to list
list4 = list(str1)
print(list4)
list5 = str1.split()
print(list5)
# adding and subtracting from lists
list1.append("blueberries")
print(list1)
list1.extend(["raspberries"])
print(list1)
list1.insert(1, "strawberries")
print(list1)
list1.remove("raspberries")
print(list1)
list1.pop(3)
# modify list
list1[0] = "fuji apples"
print(list1)
list1[1], list1[0] = list1[0], list1[1]
print(list1)
# copy list
list6 = [x for x in list1]
print(list6)
list7 = ["I love "+x for x in list1]
print(list7)
# sort
list6.sort()
print(list6)
list6.reverse()
print(list6)
# slicing
list8 = [0, 1, 2, 3, 4, 5, 6, 7, 8]
print("::",list8[::])
print("0:7:",list8[0:7:])
print(":7:",list8[:7:])
print(":-3:",list8[:-3:])
print("::2",list8[::2])
print("-2::",list8[-2::])
print("-5::",list8[-5::])
print("-1:-7:",list8[-1:-7:]) # nonsense
print("-7:-1:",list8[-7:-1:])
# ---------------------------- tuple ----------------------------
tuple1 = (1, 2, 3, 3, 4)
print(tuple1)
# ---------------------------- set ----------------------------
set1 = {1, 2, 3, 4, 3}
print(set1)
set2 = {1, "hi", False}
set1.add(5)
print(set1)
set1.remove(5)
print(set1)
# ---------------------------- dict ----------------------------
dict1 = {
"fruit":"apple",
"vegetable":"carrot",
"protein":"salmon",
"days":7
}
print(dict1)
dict2 = dict(name = "Sophie", age = 20, city = "Oakland")
print(dict2)
# get value
print(dict1["fruit"])
print(dict1.get("fruit"))
print(dict1.values())
# get keys
keys = dict1.keys()
print(keys)
items = dict1.items()
print(items)
# for key, value in items:
# print(key)
# print(value)
# change, add, remove
dict2["city"] = "San Francisco"
print(dict2)
dict2["state"] = "California"
print(dict2)
dict2.pop("state")
print(dict2)