###### tags: `110-1學校上課` # python 上課重點整理 目前公布題目所有的答案 **GITHUB:https://github.com/Chinococo/python** ## 特殊運算符 1. **\*\*** 次方 2. **//** 整除不留小數 ## 每個資料型別(物件)的基本資料 1. **id()** 可拿他的內存地址 2. **type()** 可拿它是甚麼資料型return formate <class 'type'> 3. **val** 可拿他的值 ## 字串基本操作 1. 拿取操作:str[first,end,step] ``` for i in range(first,end,step): print(str[i]) ``` 2. 拿取長度:len(str) 回傳字串長度 3. 替換字元:str.replace(A,B)將str中A的文字換成B 4. 分割文字:str.split(spl) 將文字已spl為切割點分割 5. 轉化編碼 byte -> str :byte.encode() str -> byte :str.encode() # print() **描述:輸出於畫面** 例子說明:連續輸出兩個字串,他會以空白分開 ``` print('123','456') ``` output: 123 456 例子說明:以特殊格式化方式,進行合成 ``` input1 = float(input()) input2 = str(input()) print('Hi, my name {:.1f} {:s}'.format(input1,input2)) ``` input: 123.123 hello output: Hi, my name 123.1 hello # if else 描述:可依照邏輯進行判斷ㄅㄠ 例子說明:判斷兩數關係 ``` input1 = int(input()) input2 = int(input()) if(input1>input2): print('{} is greater than {}'.format(input1,input2)) elif(input1<input2): print('{} is less than {}'.format(input1, input2)) else: print('{} is equal {}'.format(input1, input2)) ``` sample input1 100 80 sample output1 100 is greater than 80 sample input2 80 100 sample output2 80 is less than 100 sample input3 200 200 sample output3 200 is equal 200 # Function def:定義fun關鍵字 **parameter:輸入參數,若要輸入不限參數數量,將para前面加個\*,若加兩個\*,他會輸入一個dict** ex: def FunctionName(parameter): statement # Expception | Expeion Name | descripe | |:------------------ | --------------------------- | | FloatingPointError | 浮点计算错误 | | ArithmeticError | 所有数值计算错误的基类 | | OverflowError | 数值运算超出最大限制 | | ZeroDivisionError | 除(或取模)零 (所有数据类型) | | IndexError | 序列中没有此索引(index) | | KeyError | 映射中没有这个键 | ![](https://i.imgur.com/no3WsRe.png) ![](https://i.imgur.com/1BUo4sY.png) ![](https://i.imgur.com/rO1sfdQ.png) ## list ### 基本宣告 list_name = [] **若要初始化一維可使用[0 for i in range(length)]可宣告出一個全部是0長度為length的陣列 若要初始化二維可使用[[0 for x in range(length1)] for i in range(length2)]可宣告出一個全部是0長度為length1*length2的陣列** append(elm)向後增加 len(list) 可以取出第一層有幾項 len(list[index])可以取出index位置的第二層有幾項 pop(index)可以移除index的元素 remove(val)可以移除val在這List全部元素 getval list_name[index1][index2]..... ## set ### 基本宣告 set_name = set() .add(elm) len(set_name) 不重複元素數量 remove(elm) 可以移除elm ## dict ### 基本宣告 dict_name = dict or {} dict_name[name]=val len(dict_name) ## unittest/coverge ![](https://i.imgur.com/zhBNlhV.png) ![](https://i.imgur.com/lERCa0m.png) ![](https://i.imgur.com/Ps8wLKu.png) **檔案請開在同一個資料夾** **資料夾/cov/index可以開啟**