1+ {
2+ "nbformat" : 4 ,
3+ "nbformat_minor" : 0 ,
4+ "metadata" : {
5+ "colab" : {
6+ "name" : " Tuple_Set" ,
7+ "provenance" : [],
8+ "authorship_tag" : " ABX9TyMLjblb2LOWQ7yX7msW38vN" ,
9+ "include_colab_link" : true
10+ },
11+ "kernelspec" : {
12+ "name" : " python3" ,
13+ "display_name" : " Python 3"
14+ }
15+ },
16+ "cells" : [
17+ {
18+ "cell_type" : " markdown" ,
19+ "metadata" : {
20+ "id" : " view-in-github" ,
21+ "colab_type" : " text"
22+ },
23+ "source" : [
24+ " <a href=\" https://colab.research.google.com/github/HsiaoEn/python-programming/blob/master/5.Container/Tuple_Set.ipynb\" target=\" _parent\" ><img src=\" https://colab.research.google.com/assets/colab-badge.svg\" alt=\" Open In Colab\" /></a>"
25+ ]
26+ },
27+ {
28+ "cell_type" : " markdown" ,
29+ "metadata" : {
30+ "id" : " unb8q-dox27Y" ,
31+ "colab_type" : " text"
32+ },
33+ "source" : [
34+ " ## Tuple\n " ,
35+ " \n " ,
36+ " 和 list 很像,都是一系列的任意型別資料。但與 list 不同的是,tuple 中的資料一經宣告後,**就不可做任何異動**,所以我們可以把 tuple 視為常數的串列。"
37+ ]
38+ },
39+ {
40+ "cell_type" : " code" ,
41+ "metadata" : {
42+ "id" : " WSV_6IiJ6xmx" ,
43+ "colab_type" : " code" ,
44+ "colab" : {
45+ "base_uri" : " https://localhost:8080/" ,
46+ "height" : 579
47+ },
48+ "outputId" : " 9f613010-d68e-46d8-8e5b-42d7740d7c8b"
49+ },
50+ "source" : [
51+ " from IPython.display import HTML\n " ,
52+ " HTML(\"\"\"\n " ,
53+ " <div style=\" display: flex; justify-content: center;\" >\n " ,
54+ " <video width=1000 controls>\n " ,
55+ " <source src=\" https://drive.google.com/uc?id=1Y-fKkQHVas9gcWTJQhAxl2Vxpwo4IT8I\" type=\" video/mp4\" >\n " ,
56+ " </video>\n " ,
57+ " </div>\n " ,
58+ " \"\"\" )"
59+ ],
60+ "execution_count" : null ,
61+ "outputs" : [
62+ {
63+ "output_type" : " execute_result" ,
64+ "data" : {
65+ "text/html" : [
66+ " \n " ,
67+ " <div style=\" display: flex; justify-content: center;\" >\n " ,
68+ " <video width=1000 controls>\n " ,
69+ " <source src=\" https://drive.google.com/uc?id=1Y-fKkQHVas9gcWTJQhAxl2Vxpwo4IT8I\" type=\" video/mp4\" >\n " ,
70+ " </video>\n " ,
71+ " </div>\n "
72+ ],
73+ "text/plain" : [
74+ " <IPython.core.display.HTML object>"
75+ ]
76+ },
77+ "metadata" : {
78+ "tags" : []
79+ },
80+ "execution_count" : 2
81+ }
82+ ]
83+ },
84+ {
85+ "cell_type" : " code" ,
86+ "metadata" : {
87+ "id" : " C8bFwtXQx27Z" ,
88+ "colab_type" : " code" ,
89+ "colab" : {},
90+ "outputId" : " 54515622-8b25-4305-9fd5-c98b5d720416"
91+ },
92+ "source" : [
93+ " # 宣告空的 tuple\n " ,
94+ " T1 = ()\n " ,
95+ " T2 = tuple()\n " ,
96+ " \n " ,
97+ " print(T1)\n " ,
98+ " print(T2)"
99+ ],
100+ "execution_count" : null ,
101+ "outputs" : [
102+ {
103+ "output_type" : " stream" ,
104+ "text" : [
105+ " ()\n " ,
106+ " ()\n "
107+ ],
108+ "name" : " stdout"
109+ }
110+ ]
111+ },
112+ {
113+ "cell_type" : " code" ,
114+ "metadata" : {
115+ "id" : " MZoybAEhx27s" ,
116+ "colab_type" : " code" ,
117+ "colab" : {},
118+ "outputId" : " 10a14d28-d1ef-4bbe-f6d8-65bb48ef4e4f"
119+ },
120+ "source" : [
121+ " # Tuple 和 List 是可以互相轉換的\n " ,
122+ " T = (1, 2, 3)\n " ,
123+ " L = list(T)\n " ,
124+ " print(L)\n " ,
125+ " print(T)\n " ,
126+ " \n " ,
127+ " L = [1, 2, 3]\n " ,
128+ " T = tuple(L)\n " ,
129+ " print(L)\n " ,
130+ " print(T)"
131+ ],
132+ "execution_count" : null ,
133+ "outputs" : [
134+ {
135+ "output_type" : " stream" ,
136+ "text" : [
137+ " [1, 2, 3]\n " ,
138+ " (1, 2, 3)\n " ,
139+ " [1, 2, 3]\n " ,
140+ " (1, 2, 3)\n "
141+ ],
142+ "name" : " stdout"
143+ }
144+ ]
145+ },
146+ {
147+ "cell_type" : " markdown" ,
148+ "metadata" : {
149+ "id" : " jofK7Fdpx27v" ,
150+ "colab_type" : " text"
151+ },
152+ "source" : [
153+ " ## Set\n " ,
154+ " \n " ,
155+ " 集合與List最大的不同是沒有順序性。"
156+ ]
157+ },
158+ {
159+ "cell_type" : " code" ,
160+ "metadata" : {
161+ "id" : " 8kRzBnpNx27v" ,
162+ "colab_type" : " code" ,
163+ "colab" : {},
164+ "outputId" : " 9da04568-3eec-4f23-b0cd-03fd007c9491"
165+ },
166+ "source" : [
167+ " # 宣告一個空集合\n " ,
168+ " S = set()\n " ,
169+ " \n " ,
170+ " print(S)"
171+ ],
172+ "execution_count" : null ,
173+ "outputs" : [
174+ {
175+ "output_type" : " stream" ,
176+ "text" : [
177+ " set()\n "
178+ ],
179+ "name" : " stdout"
180+ }
181+ ]
182+ },
183+ {
184+ "cell_type" : " code" ,
185+ "metadata" : {
186+ "id" : " -KfUjgObx27z" ,
187+ "colab_type" : " code" ,
188+ "colab" : {},
189+ "outputId" : " 5fa5ea1e-994b-45f9-e2c9-34356dc852f6"
190+ },
191+ "source" : [
192+ " # 宣告有東西的集合使用大括號 {},\n " ,
193+ " S1 = {1, 2, 3, 4}\n " ,
194+ " S2 = {3, 4, 5, 6, 6}\n " ,
195+ " \n " ,
196+ " print(S1)\n " ,
197+ " print(S2)"
198+ ],
199+ "execution_count" : null ,
200+ "outputs" : [
201+ {
202+ "output_type" : " stream" ,
203+ "text" : [
204+ " {1, 2, 3, 4}\n " ,
205+ " {3, 4, 5, 6}\n "
206+ ],
207+ "name" : " stdout"
208+ }
209+ ]
210+ },
211+ {
212+ "cell_type" : " code" ,
213+ "metadata" : {
214+ "id" : " COcvMsacx272" ,
215+ "colab_type" : " code" ,
216+ "colab" : {},
217+ "outputId" : " 8bd9a5a5-48b2-49a7-b997-65138df6c2eb"
218+ },
219+ "source" : [
220+ " # 兩集合的交集使用 & 符號\n " ,
221+ " print(S1 & S2)\n " ,
222+ " \n " ,
223+ " # 兩集合的聯集使用 | 符號\n " ,
224+ " print(S1 | S2)\n " ,
225+ " \n " ,
226+ " # 兩集合的差集使用 - 符號\n " ,
227+ " print(S1 - S2)\n " ,
228+ " print(S2 - S1)\n " ,
229+ " \n " ,
230+ " # 兩集合的互斥集使用 ^ 符號\n " ,
231+ " print(S1 ^ S2)\n " ,
232+ " \n " ,
233+ " # 使用 >, <, =, >=, <=, == 判斷集合間的子集關係\n " ,
234+ " print(S1 <= S2)\n " ,
235+ " print(S1 >= S2)"
236+ ],
237+ "execution_count" : null ,
238+ "outputs" : [
239+ {
240+ "output_type" : " stream" ,
241+ "text" : [
242+ " {3, 4}\n " ,
243+ " {1, 2, 3, 4, 5, 6}\n " ,
244+ " {1, 2}\n " ,
245+ " {5, 6}\n " ,
246+ " {1, 2, 5, 6}\n " ,
247+ " False\n " ,
248+ " False\n "
249+ ],
250+ "name" : " stdout"
251+ }
252+ ]
253+ },
254+ {
255+ "cell_type" : " code" ,
256+ "metadata" : {
257+ "id" : " dk5EAx01x278" ,
258+ "colab_type" : " code" ,
259+ "colab" : {},
260+ "outputId" : " ce549928-2c77-4ecd-b797-f8c56bf93f92"
261+ },
262+ "source" : [
263+ " # 集合可由 list 及 tuple 轉換而來,不過集合中的資料是沒有順序的。\n " ,
264+ " S = set([3, 1, 2, 4])\n " ,
265+ " print(S)\n " ,
266+ " \n " ,
267+ " S = set((1, 5, 2, 3))\n " ,
268+ " print(S)"
269+ ],
270+ "execution_count" : null ,
271+ "outputs" : [
272+ {
273+ "output_type" : " stream" ,
274+ "text" : [
275+ " {1, 2, 3, 4}\n " ,
276+ " {1, 2, 3, 5}\n "
277+ ],
278+ "name" : " stdout"
279+ }
280+ ]
281+ },
282+ {
283+ "cell_type" : " code" ,
284+ "metadata" : {
285+ "id" : " zvQuIIEOx27_" ,
286+ "colab_type" : " code" ,
287+ "colab" : {},
288+ "outputId" : " b60ed2a6-9860-4991-d27c-578f35cbd9a6"
289+ },
290+ "source" : [
291+ " S = {1, 2, 3, 4}\n " ,
292+ " \n " ,
293+ " # 在集合中增加一物件\n " ,
294+ " S.add(7)\n " ,
295+ " print(S)\n " ,
296+ " \n " ,
297+ " # 若要將集合與其他集合做合併,可使用 update(),兩集合重複的物件將不會重複合併。\n " ,
298+ " S.update((4, 5, 6))\n " ,
299+ " print(S)"
300+ ],
301+ "execution_count" : null ,
302+ "outputs" : [
303+ {
304+ "output_type" : " stream" ,
305+ "text" : [
306+ " {1, 2, 3, 4, 7}\n " ,
307+ " {1, 2, 3, 4, 5, 6, 7}\n "
308+ ],
309+ "name" : " stdout"
310+ }
311+ ]
312+ }
313+ ]
314+ }
0 commit comments