if expression1:
statement #滿足條件一,要做的事
elif expression2:
statement #滿足條件二,要做的事
elif expression3:
statement #滿足條件三,要做的事
else:
statement #上述條件皆不滿足,要做的事
if (a > b):
print ("a is greater than b")
elif (a < b):
print ("a is less than b")
else:
print ("a is equal to b")
a = 100
b = 95
if (a > b):
print ("a is greater than b")
elif (a < b):
print ("a is less than b")
else:
print ("a is equal to b")
a is greater than b
my_string = "Hello Python!!"
if "H" in my_string:
print("Found ","H")
else :
print("Not Found")
Found H
in
及 if
/else
判斷式,判斷 fruit 中有沒有 "apple"
¶fruit = ['banana', 'Wax apple','cherry', 'coconut', 'jujube', 'durian', 'grape', 'grapefruit', 'guava', 'lemon', 'pineapple', 'Watermelon', 'Kiwi', 'Orange', 'Longan', 'Blueberry', 'Mango']
fruit = ['banana', 'Wax apple','cherry', 'coconut', 'jujube', 'durian', 'grape', 'grapefruit', 'guava', 'lemon', 'pineapple', 'Watermelon', 'Kiwi', 'Orange', 'Longan', 'Blueberry', 'Mango']
if "apple" in fruit:
print('I find apple!')
else:
print('No apple.')
No apple.
if "apple" in "pineapple":
print('I find apple!')
else:
print('No apple.')
I find apple!
if "apple" in "Wax apple":
print('I find apple!')
else:
print('No apple.')
I find apple!
for
)¶for iterating_var in sequence:
statements
NOTE 迭代子(iterating_var) 為 sequence 的內容
city = 'Taipei'
print
出來¶print(city[0])
print(city[1])
print(city[2])
print(city[3])
print(city[4])
print(city[5])
T a i p e i
city = 'Taipei'
for
¶for i in city:
print(i)
T a i p e i
Temperature_Taipei = [22 , 24 , 19, 19, 30, 21, 25]
print
出來¶Temperature_Taipei = [22 , 24 , 19, 19, 30, 21, 25]
print(Temperature_Taipei[0])
print(Temperature_Taipei[1])
print(Temperature_Taipei[2])
print(Temperature_Taipei[3])
print(Temperature_Taipei[4])
print(Temperature_Taipei[5])
print(Temperature_Taipei[6])
22 24 19 19 30 21 25
for
¶Temperature_Taipei = [22 , 24 , 19, 19, 30, 21, 25]
for i in Temperature_Taipei:
print(i)
22 24 19 19 30 21 25
NOTE
print('range(10) 的型態 :' ,type(range(10)))
print('輸出 range(10) :' ,range(10))
print('利用 list range(10) 中的元素都輸出 :' ,list(range(10)))
range(10) 的型態 : <class 'range'> 輸出 range(10) : range(0, 10) 利用 list range(10) 中的元素都輸出 : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print('range(0, 10) : ' ,list(range(0, 10)))
print('range(0, 10, 2) : ' ,list(range(0, 10, 2)))
print('range(1, 6, 2) : ' ,list(range(1, 6, 2)))
range(0, 10) : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] range(0, 10, 2) : [0, 2, 4, 6, 8] range(1, 6, 2) : [1, 3, 5]
NOTE 迭代子 (index) 為 索引值
for i in range(5, -6 , -1):
print(i)
5 4 3 2 1 0 -1 -2 -3 -4 -5
for i in range(-5, 6, 1):
print(i)
-5 -4 -3 -2 -1 0 1 2 3 4 5
city = 'Taipei'
for
¶city = 'Taipei'
for i in city:
print(i)
T a i p e i
city = 'Taipei'
for & range( )
(索引值)¶city = 'Taipei'
for i in range(0,len(city)):
print(city[i])
T a i p e i
city = ['Taipei', ['Taichung',22], {'Kaohsiung':25}]
NOTE 輸出不必是單一個值也可以是 list 或 dictionary
city_list = ['Taipei', ['Taichung',22], {'Kaohsiung':25}]
city = ['Taipei', ['Taichung',22], {'Kaohsiung':25}]
for i in city:
print (i)
Taipei ['Taichung', 22] {'Kaohsiung': 25}
city_list = ['Taipei', ['Taichung',22], {'Kaohsiung':25}]
for i in range(len(city_list)):
print (city_list[i])
Taipei ['Taichung', 22] {'Kaohsiung': 25}
weather_dict = { 'Taipei':'rainy', 'Taichung':'cloudy' , 'Tainan':'sunny' , 'Kaohsiung':'sunny' }
print(weather_dict.keys())
dict_keys(['Taipei', 'Taichung', 'Tainan', 'Kaohsiung'])
for i in weather_dict:
print(i)
Taipei Taichung Tainan Kaohsiung
weather_dict = { 'Taipei':'rainy', 'Taichung':'cloudy' , 'Tainan':'sunny' , 'Kaohsiung':'sunny' }
print(weather_dict.values())
dict_values(['rainy', 'cloudy', 'sunny', 'sunny'])
for v in weather_dict.values():
print(v)
rainy cloudy sunny sunny
weather_dict = { 'Taipei':'rainy', 'Taichung':'cloudy' , 'Tainan':'sunny' , 'Kaohsiung':'sunny' }
for (key, value) in weather_dict.items():
print(key, ":", value)
Taipei : rainy Taichung : cloudy Tainan : sunny Kaohsiung : sunny
for (key, value) in weather_dict.items():
print("%s:%s" % (key, value))
Taipei:rainy Taichung:cloudy Tainan:sunny Kaohsiung:sunny
Week = ['SUN','MON','TUE','WED','THU','FRI','SAT']
Temperature_Taipei = [22 , 24 , 19, 19, 30, 21, 25]
list( zip( Week,Temperature_Taipei ) )
[('SUN', 22), ('MON', 24), ('TUE', 19), ('WED', 19), ('THU', 30), ('FRI', 21), ('SAT', 25)]
Week = ['SUN','MON','TUE','WED','THU','FRI','SAT']
Temperature_Taipei = [22 , 24 , 19, 19, 30, 21, 25]
Weather_Taipei = ['Yes','Yes','No']
list( zip( Week,Temperature_Taipei,Weather_Taipei ) )
[('SUN', 22, 'Yes'), ('MON', 24, 'Yes'), ('TUE', 19, 'No')]
for week ,temperature in zip( Week,Temperature_Taipei ):
print(week ,temperature)
SUN 22 MON 24 TUE 19 WED 19 THU 30 FRI 21 SAT 25
for index in range(len(Temperature_Taipei)):
print(index , ":", Temperature_Taipei[index])
0 : 22 1 : 24 2 : 19 3 : 19 4 : 30 5 : 21 6 : 25
for index, temperature in enumerate(Temperature_Taipei):
print(index , ":", temperature)
0 : 22 1 : 24 2 : 19 3 : 19 4 : 30 5 : 21 6 : 25
Temperature_Taipei = [22, 24, 19, 19, 30, 21, 25]
公式: $Fahrenheit = Celsius × (9/5) + 32$
Temperature_Taipei = [22, 24, 19, 19, 30, 21, 25]
Fahrenheit_Temperature_Taipei = Temperature_Taipei * (9/5) + 32
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-39-82acf661b327> in <module>() ----> 1 Fahrenheit_Temperature_Taipei = Temperature_Taipei * (9/5) + 32 TypeError: can't multiply sequence by non-int of type 'float'
NOTE list 不能做 element-wise 的運算
Temperature_Taipei = [22, 24, 19, 19, 30, 21, 25]
for i in Temperature_Taipei:
print(i * (9/5) + 32)
71.6 75.2 66.2 66.2 86.0 69.80000000000001 77.0
NOTE 將轉成華氏的結果存在一個 list
中
Temperature_Taipei = [22, 24, 19, 19, 30, 21, 25]
Temperature_Taipei_Fahrenheit = []
for i in Temperature_Taipei:
Temperature_Taipei_Fahrenheit.append( i * (9/5) + 32 )
print(Temperature_Taipei_Fahrenheit)
[71.6, 75.2, 66.2, 66.2, 86.0, 69.80000000000001, 77.0]
for
迴圈,計算 1 加到 100¶公式: $ Total = \sum_{i=1}^{100} x_i $
NOTE
range
來建立一個 1 到 100 的 list
sum()
對答案sum( )
對答案¶sum( range(1,101) )
5050
total = 0
for i in range(1,101):
total = total+i
print(total)
5050
sum( )
對答案¶sum( range(1,101) )
5050
total = 0
for i in range(1,101):
total +=i
print(total)
5050
for
迴圈,計算 $5!$¶公式: $ Factorial = \prod_{k=1}^{5} k $
factorial = 1
for i in range(1,6):
factorial = factorial * i
print(factorial)
120
factorial = 1
for i in range(1,6):
factorial *= i
print(factorial)
120
NOTE 所有的 for 迴圈,都可以改用 while 迴圈;反之則不一定
total = 0
i=0
while i < len(range(1,101)):
total += range(1,101)[i]
i += 1
print(i)
print(total)
100 5050
for iterating_var in sequence:
for iterating_var in sequence:
statements
statements
while expression:
while expression:
statements
statements
for i in range(1,10):
for j in range(1,10):
k=i*j
print (k , end=' ')
print()
1 2 3 4 5 6 7 8 9 2 4 6 8 10 12 14 16 18 3 6 9 12 15 18 21 24 27 4 8 12 16 20 24 28 32 36 5 10 15 20 25 30 35 40 45 6 12 18 24 30 36 42 48 54 7 14 21 28 35 42 49 56 63 8 16 24 32 40 48 56 64 72 9 18 27 36 45 54 63 72 81
NOTE 輸出樣式 : 1 是奇數
for i in range(0,21):
if ( i % 2 == 0):
print(i,'是偶數')
else:
print(i,'是奇數')
0 是偶數 1 是奇數 2 是偶數 3 是奇數 4 是偶數 5 是奇數 6 是偶數 7 是奇數 8 是偶數 9 是奇數 10 是偶數 11 是奇數 12 是偶數 13 是奇數 14 是偶數 15 是奇數 16 是偶數 17 是奇數 18 是偶數 19 是奇數 20 是偶數
Temperature_Taipei = [22, 24, 19, 19, 30, 21, 25]
Temperature_Taipei = [22, 24, 19, 19, 30, 21, 25]
for i in Temperature_Taipei :
if i <= 22:
print('在家')
else :
print('出門')
在家 出門 在家 在家 出門 在家 出門
Temperature_Taipei = [22 , 24 , 19, 19, 30, 21, 25]
Temperature_Taichung = [23 , 22 , 19, 20, 29, 19, 27]
for Taipei, Taichung in zip(Temperature_Taipei, Temperature_Taichung) :
if Taipei > Taichung :
print('Taipei 好熱')
elif Taipei < Taichung :
print('Taichung 好熱')
else :
print('Taipei Taichung 都好熱')
Taichung 好熱 Taipei 好熱 Taipei Taichung 都好熱 Taichung 好熱 Taipei 好熱 Taipei 好熱 Taichung 好熱
Weather_Taipei = ['sunny','sunny','cloudy','sunny','rainy','cloudy','rainy']
for day in Weather_Taipei :
if day !='sunny':
break
else :
print(day)
sunny sunny
Weather_Taipei = ['sunny','sunny','cloudy','sunny','rainy','cloudy','rainy']
for day in Weather_Taipei :
if day !='sunny':
continue
else :
print(day)
sunny sunny sunny
list
分別是 未來一週 Taipei 的天氣以及星期,請找出未來一週天氣為 sunny 的星期¶Week = ['SUN','MON','TUE','WED','THU','FRI','SAT']
Weather_Taipei = ['sunny','sunny','cloudy','sunny','rainy','cloudy','rainy']
NOTE 提示:可以利用 zip
Week = ['SUN','MON','TUE','WED','THU','FRI','SAT']
Weather_Taipei = ['sunny','sunny','cloudy','sunny','rainy','cloudy','rainy']
for i , j in zip(Week,Weather_Taipei) :
if j != 'sunny':
continue
else :
print(i)
SUN MON WED