[Python]基礎課程
Python 起源
- 1991年Python正式誕生
 - Python底層是由C語言開發的直譯器
 - Python由Python軟體基金會管理
 
Python 相較於其他語言優勢
- 較受歡迎的主流語言
 - 入門檻較低
 - 應用多元
 
Python 應用的領域
- 資料科學
 - 人工智慧
 - 開發/管理網站
 - 遊戲開發
 - ..
 
目前主流的開發環境
- 網路開發 Google Colaboratory
 - 本機開發 Anaconda
 
撰寫規則
- 不用宣告變數屬性
 - 不用 ; 結尾
 - 執行(RUN) 快捷建 : Ctrl+Enter
 - 註解寫法 多行'''command ''' 單行 # command
 - 四則運算
 - + #加
 - - #減
 - * #乘
 - / #除
 - ** #次方
 - // #商
 - % #餘數
 - 函數用法
 - print() #輸出
 - input() #輸入
 - del x #刪除變數x
 - help() #函數用法說明
 - type() #顯示資料型態
 - abs() #取絕對值
 - pow(x,y) #x的y次方
 - 多重指派
 - x=y=z=20
 - x,y,z=10,20,30
 - x,y=y,x #swap
 - 資料型態
 - int()
 - float
 - str()
 - bool()
 - bool(0) #False
 - bool(非零數值) # True
 - 輸出方式
 - 字串以" " 或' ' 框住
 
x = ' This\' 'y = "python"z = "class"str_variable = x + y + z # + 限定str型態print(str_variable)
- print() #強制轉換成字串型 str()
 - \' #跳脫字元
 - \n #換行字元 也可以用 \r\n 或\n\r
 - \t #Tab字元
 - print( , , , ) # 用, 接值可以不用轉型
 - print('Hi'*5) # HiHiHiHiHi
 
classname = "python基礎課程" hours = 15 print("這是", hours, "個小時的", classname) print("這是", hours, "個小時的", classname, sep='--') print("--------------") print("這是", hours, "個小時的", classname, end='\t') print("這是", hours, "個小時的", classname, sep='--')
變數的命名
- 使用英文字母、中文或底線_開頭
 - 變數名稱只能以英文字母、中文、數字、底線組成
 - 大小寫不同將被視為不同變數名稱,如:School, school
 - 不可使用系統的保留字、內建函數的名稱
 
變數的保留字
import keyword
print(keyword.kwlist)
classname = "python Class" hours = 15 print("這是 {} 個小時的 {}".format(hours, classname))
format 函數 -輸出
name = input("請輸入您的名字") print("Hello, %s 感謝您使用本系統" %name) print("- - - - - - - - - - - - -") print("Hello, %s \n感謝您使用本系統" %name)
流程控制
關係運算子
- a > b : 判斷a是否大於b
 - a >= b : 判斷a是否大於等於b
 - a < b : 判斷a是否小於b
 - a <= b : 判斷a是否小於等於b
 - a == b : 判斷a是否等於b
 - a != b : 判斷a是否不等於b
 
邏輯運算子
- and
 - or
 - not
 
if 敘述
if(條件判斷式):
    程式區塊說明:當條件判斷式成立(True),執行程式區塊
#漸進式讀取需要有順序的判斷規則
if ... else 敘述
if(條件判斷式):
    程式區塊一
else:
    程式區塊二說明:當if條件判斷式成立(True)執行程市區塊一,否則執行程式區塊二
if ... elif ... else 敘述
if(條件判斷式一):
    程式區塊一
elif(條件判斷式二):
    程式區塊二
...
...
else:
    程式區塊三說明:當條件判斷式一成立(True)執行程式區塊一,否則判斷條件判斷式二是否成立(True)若成立則執行程式區塊二、若不成立(False)執行程式區塊三
巢狀 if 敘述
if(條件判斷一): if(條件判斷A): 程式區塊A else: 程式區塊B else: 程式區塊二
#以Tab鍵當作判斷程式區塊的基準
資料結構
Python的資料結構
- python內建結構
- list
 - tuple
 - dictionary
 - set
 
 - 其他好用的結構
 - numpy
 - pandas
 
list 串列資料結構
變數名稱 = [資料]   # 資料不限定型態
- list[index] #從0開始
 
串列的切片
list[start:end]   #取從start到(end-1)的資料
list[:]    #取全部的資料
list[:n]   #取前n筆資料
list[n:]   #取從第n筆到最後的資料
list[-n:]   #取串列後n名 list[start:end:step]   #取從start到(end-1)中每格step的資料 
list搭配的常用函數
- len(list) : list的個數
 - max(list) : 取最大值
 - min(list) : 取最小值
 - sum(list) : 總和
 
#輸出小數點後一位x=3.16159print(round(x,1))print("%.1f" %x )
串列相加與乘法
my_list1 = [1, 2, 3] my_list2 = [4, 5, 6] my_list3 = my_list1 + my_list2 print(my_list3) #[1,2,3,4,5,6] print(my_list1*2) #[1,2,3,1,2,3]
del list
新增串列元素
list.append(內容)
list.insert(索引, 內容)
刪除串列元素
list.pop(索引)
list.remove(內容)
修改串列的元素
list[索引] = 內容
串列的拷貝與淺拷貝
#deep copy
boy=[A,B]
girl=boy
boy.append(C)
print(boy) #[A,B,C]
print(girl) #[A,B,C]
id(list)  #顯示list在記憶體的位置
#shadow copy
boy=[A,B]
girl=boy[:]
boy.append(C)
print(boy) #[A,B,C]
print(girl) #[A,B]
串列的排序
num_list.sort() #由小到大
num_list.sort(reverse = True) #由大到小
排序
1. list.sort()
2. list.sort(reverse=True)
3. other_list = sorted(list) #不更動list串列
4. other_list = sorted(list, reverse=True)  #不更動list串列
留言
張貼留言