# **2023-11-06 Python設計課(期中考)** ### h3examination scope(考題範圍): > ## **1.Data Types<font color=#800000>(int,float,bool,string)</font>,expression** > ## **2.Data Structure <font color=#800000>(list,tuple,dictionary,set)</font>** # **1.Data Types** # 數據類型(Data Types) ## 1. 整數(Integers) - 整數是一種數字數據類型,通常表示不帶小數部分的數值。 - 整數可以是正數、負數或零。 - ## 範例:<span style="color: red; text-decoration: underline;">`x = 5`</span>, <span style="color: red; text-decoration: underline;">`y = -3`</span>, <span style="color: red; text-decoration: underline;">`z = 0`</span> - 整數支援基本數學運算,如加法、減法、乘法和除法. ## 2. 浮點數(Floats) - 浮點數是一種數字數據類型,可以表示帶有小數部分的數值. - 浮點數通常用於表示實數,包括小數和科學記號表示法. - ## 範例:<span style="color: red; text-decoration: underline;">`a = 3.14`</span>, <span style="color: red; text-decoration: underline;">`b = -0.5`</span>, <span style="color: red; text-decoration: underline;">`c = 1.23e-4`</span> - 浮點數也支援基本數學運算. ## 3. <span style="color: red; text-decoration: underline;">布林值(Booleans)</span> - 布林值是一種二元數據類型,只有兩個值:True(真)和False(假). - 布林值常用於控制流程和條件判斷. - ## 範例:<span style="color: red; text-decoration: underline;">`is_true = True`</span>, <span style="color: red; text-decoration: underline;">`is_false = False`</span>. - 布林運算符包括 AND、OR 和 NOT. ## 4. 字符串(Strings) - 字符串是一種表示文本數據的數據類型,由字符序列組成. - 字符串用引號括起來,可以使用單引號或雙引號. - ## 範例:<span style="color: red; text-decoration: underline;">`name = "Alice"`</span>, <span style="color: red; text-decoration: underline;">`message = 'Hello, World!'`</span>. - 字符串支援字符串連接、切片、替換等操作. ## 5. 表達式(Expressions) - 表達式是一組數學、邏輯或操作符操作,可以計算得到一個值. - 表達式可以包含變數、常數和操作符,例如加法、減法、比較等. - ## 範例:<span style="color: red; text-decoration: underline;">`result = x + y`</span>, <span style="color: red; text-decoration: underline;">`is_greater = a > b`</span>, <span style="color: red; text-decoration: underline;">`greeting = "Hello, " + name`" # **2.Data Structure** # 列表(List) > ### **用法:列表是一種有序的數據結構,可以包含不同類型的數據項,並允許重複的元素.** >>## 1.創建: **<font color=#FF0000>my_list = [1, 2, 3, '蘋果']</font>** >>## 2.存取元素:<span style="color: red; text-decoration: underline;">**element = my_list[0]**</span> >>## 3.添加元素:<span style="color: red; text-decoration: underline;">**my_list.append(4)**</span> >>## 4.修改元素:<span style="color: red; text-decoration: underline;">**my_list[1] = 5**</span> >>## 5.移除元素:<span style="color: red; text-decoration: underline;">**my_list.remove(2)**</span> >>## 6.長度:<span style="color: red; text-decoration: underline;">**length = len(my_list)**</span> # 集合(Set) >### 用法:集合是一種無序的數據結構,**<font color=#FF0000>不允許重複的元素</font>**,通常用於去除重複數據或執行集合操作. >>## **1.創建:<span style="color: red; text-decoration: underline;">**my_set = {1, 2, 3, 3, 4}**</span> >>## **2.添加元素:<span style="color: red; text-decoration: underline;">**my_set.add(5)**</span> >>## **3.移除元素:<span style="color: red; text-decoration: underline;">**my_set.remove(2)**</span> >>## **4.長度:<span style="color: red; text-decoration: underline;">**length = len(my_set)**</span> >>## **5.集合操作1:<span style="color: red; text-decoration: underline;">**union_set = set1 | set2**</span> >>## **6.集合操作2 : <span style="color: red; text-decoration: underline;">**intersection_set = set1 & set2**</span> # 元組(Tuple) >### 用法:元組是一種有序的 **<font color=#FF0000>不可變數據結構</font>**,通常用於表示不可更改的數據. >>## **創建:<span style="color: red; text-decoration: underline;">**my_tuple = (1, 2, 3)**</span> >>## **存取元素:<span style="color: red; text-decoration: underline;">**element = my_tuple[0]**</span> >>## **長度:<span style="color: red; text-decoration: underline;">**length = len(my_tuple)**</span> >># **不可變性:元組的元素無法被修改,但你可以創建新元組.** # 字典(Dictionary) >### 用法:字典是一種鍵-值對的數據結構,用於快速查找和存儲數據. >>## **創建:<span style="color: red; text-decoration: underline;">**my_dict = {'姓名': '艾麗絲', '年齡': 30}**</span> >>## **存取值:<span style="color: red; text-decoration: underline;">**value = my_dict['姓名']**</span> >>## **添加鍵-值對:<span style="color: red; text-decoration: underline;">**my_dict['城市'] = '紐約'**</span> >>## **移除鍵-值對:<span style="color: red; text-decoration: underline;">**del my_dict['年齡']**</span> >>## **鍵和值的列表:<span style="color: red; text-decoration: underline;">**keys = my_dict.keys(), values = my_dict.values()**</span> >>## **檢查鍵是否存在:<span style="color: red; text-decoration: underline;">**'姓名' in my_dict**</span>