上午: Python程式設計
老師此次課程教學for迴圈, List comprehension, 產生器, 例外處理(try、except), 函式function
for迴圈
list_y = [ [1, -2, 0],[2, -5, 3],[-2, -3,0]]
x = 0
for i in list_y:
for j in i:
if j<0:
x = x +j
print(x)
搭配enumerate
#刪除奇數位的數字
list_x = [1, 3, 5, 0, -1, -3, -2]
list_y = list_x.copy()
enu_b = enumerate(list_y)
for i,j in enu_b:
#print(i,j)
if (i % 2) == 0:
list_x.remove(j)
print(list_x)
搭配zip
#計算獲利
list_a = [52000, 51000, 48000]
list_b = [46800, 45900, 43200]
profit = 0
for i,j in zip(list_a, list_b):
#print(i,j)
profit = profit+ (i-j)
print(f'第一季獲利: {profit} ' )
List comprehension
list_1 = [1, 2, 3, 4]
list_f = [x*x for x in list_1]
list_f
搭配if
#刪除負值
list_1 = [1, 2, 3, 4, -1, -2, -7]
list_f = [x*x for x in list_1 if x>0]
list_f
產生器(語法類似list生成器)
x = [1, 2, 3, 4]
generator_x = (item*item for item in x)
for i in generator_x :
print(i)
#產生1-100之間的奇數
generator_a = (x for x in range(1,101) if x%2==1)
for i in generator_a :
print(i)
例外處理
a = 'b'
try:
s = int(a)
# 無法轉成數字
print('沒有問題')
except:
print('發生錯誤')
print('The End')
函式function
輸入攝氏溫度轉換成華氏
def Fahrenheit(c):
#c = float(input('請輸入攝氏溫度'))
return c*(9/5) + 32
Fahrenheit(20)
下午: 人工智慧與機器學習概論
今日延續上次php的部分,此次結合highchart呈現資料視覺化最後呈現畫面