1

## Day9 職訓(機器學習與資料分析工程師培訓班): python、 php結合highchart

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)
``````

``````#刪除奇數位的數字
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)
``````

``````#計算獲利
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
``````

``````#刪除負值
list_1 = [1, 2, 3, 4, -1, -2, -7]
list_f = [x*x for x in list_1 if x>0]
list_f
``````

``````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')
``````

``````輸入攝氏溫度轉換成華氏
def Fahrenheit(c):
#c = float(input('請輸入攝氏溫度'))
return c*(9/5) + 32
Fahrenheit(20)
``````