# Python基本教學 Day11 Day10 參考答案、set集合的建立與提供的函數 ## Day10 參考答案 1. 合法:a,d,f,g;不合法:b,e是保留字,c是數字開頭 2. (a) ![image](https://hackmd.io/_uploads/rkHv_Uou0.png) (b) ![image](https://hackmd.io/_uploads/B1SpOIiOC.png) (c) ![image](https://hackmd.io/_uploads/B11MtUiOR.png) (d) ![image](https://hackmd.io/_uploads/BJI7FLou0.png) (e) ![image](https://hackmd.io/_uploads/SkfHtUsu0.png) (f) ![image](https://hackmd.io/_uploads/By48FLiuR.png) (g) ![image](https://hackmd.io/_uploads/HyVPtUsdA.png) 3. ![image](https://hackmd.io/_uploads/HysTYUsdR.png) 4. ![image](https://hackmd.io/_uploads/ByOIq8iOR.png) 5. ![image](https://hackmd.io/_uploads/SJXXoIi_C.png) 6. ![image](https://hackmd.io/_uploads/BJnHR8jO0.png) 7. ![image](https://hackmd.io/_uploads/BkIOyPi_0.png) 8. ![image](https://hackmd.io/_uploads/BJGckvoOA.png) 9. ![image](https://hackmd.io/_uploads/r1qtQtj_0.png) 10. ![image](https://hackmd.io/_uploads/SkdPEtoOR.png) 11. ![image](https://hackmd.io/_uploads/SylpEKi_A.png) 12. ![image](https://hackmd.io/_uploads/rkUZrFi_A.png) 13. ![image](https://hackmd.io/_uploads/HyizSKsO0.png) 14. ![image](https://hackmd.io/_uploads/ryHg8tjuA.png) 15. ![image](https://hackmd.io/_uploads/r1q4IFsuR.png) ## Day11 set集合的建立與提供的函數 要建立一個集合,可以利用大括號<code>{}</code>將元素括起來,或是呼叫set來完成 <code>set(obj)</code> 依照<code>obj</code>內容建立一個集合 ![image](https://hackmd.io/_uploads/H12YRYjOA.png) <code>s1.add(x)</code> 將元素x添加到s1中 ![image](https://hackmd.io/_uploads/SJv0RYj_0.png) <code>s1.clear()</code> 移除集合s1中的所有元素 ![image](https://hackmd.io/_uploads/SkxYJk5sdR.png) <code>s1.difference(s2)</code> 傳回存在s1,但不存在於s2的集合(取差集,即s1-s2) ![image](https://hackmd.io/_uploads/Sk6vk9iuC.png) <code>s1.discard(x)</code> 刪除s1中指定的元素x,若x不存在則不做任何處理 ![image](https://hackmd.io/_uploads/B19FJ5jO0.png) <code>s1.intersection(s2)</code> 傳回s1和s2的交集 ![image](https://hackmd.io/_uploads/BJlo15ouC.png) <code>s1.isdisjoint(s2)</code> 判斷s1和s2是否沒有相同的元素,若沒有則回傳True ![image](https://hackmd.io/_uploads/HkVTy9suR.png) <code>s1.issubset(s2)</code> 判斷s1是否為s2的子集合 ![image](https://hackmd.io/_uploads/Hy9ke9iOC.png) <code>s1.issuperset(s2)</code> 判斷s1是否為s2的父集合 ![image](https://hackmd.io/_uploads/BJSWgcsuC.png) <code>s1.pop()</code> 從s1隨機移除一個元素,並傳回移除的元素值 ![image](https://hackmd.io/_uploads/BkxmecsdA.png) <code>s1.remove(x)</code> 將x從集合s1中刪除,若x不存在則回傳錯誤訊息 ![image](https://hackmd.io/_uploads/BkxSlqoOR.png) <code>s1.union(s2)</code> 傳回s1和s2的聯集 ![image](https://hackmd.io/_uploads/HyeZUx9s_R.png) <code>s1.update(s2)</code> 將s2中不存在s1的元素加入s1 ![image](https://hackmd.io/_uploads/HkBp-cidR.png) 題目:https://hackmd.io/@RshQciPYTE2wmLLiZx4JAA/Sk7OUSoOA 參考資料:https://docs.python.org/zh-tw/3/tutorial/index.html
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up