TKU STAT × Python Basic

HW 1


Kristen Chan

Note

作業繳交規定

  • 寄到 kristenchan.py@gmail.com
  • 寄信主旨 : HW_1_你的姓名
  • 檔案 : HW_1_你的姓名.ipynb
  • 請在 jupyter notebook 中標示清楚該段程式是屬於哪一題

注意

嚴禁抄作業 不要浪費彼此的時間 !!!

HW 1 : Question 1

下面共有三個 list 分別紀錄了 10 個受測者的姓名(name),身高(height),體重(weight)

體重(weight): kg , 身高(height):cm

name = ['Emma', 'Eathon', 'Sophia', 'Mia', 'Aria', 'Jackson', 'Easton', 'Vivian', 'Chase', 'Jason']
height = [157, 164, 158, 169, 178, 159, 169, 171, 165, 168]
weight = [52, 59, 70, 60, 65, 74, 40, 63, 66, 59]

HW 1 : Question 1

(1) 請計算這些受測者的 BMI 指標,並存放在一個名為 bmi_list 的 list 中,計算結果如下 :

print(bmi_list)
[21.096190514828187, 21.936347412254616, 28.040378144528116, 21.007667798746546, 20.515086478979924, 29.270994027135, 14.00511186583103, 21.545090797168363, 24.242424242424246, 20.904195011337873]

HW 1 : Question 1

(2) 請分別判斷這 10 個受測者的 BMI,並存放在一個名為 fat_list 的 list 中,計算結果如下 :

print(fat_list)
['體重正常', '體重正常', '肥胖', '體重正常', '體重正常', '肥胖', '體重過低', '體重正常', '超重', '體重正常']

Note

  • 體重過低 : <18.5
  • 體重正常 : 18.5-23.9
  • 超重 : 24-27.9
  • 肥胖 : ≥28

HW 1 : Question 1

(3) 請找出這 10 個受測者中 BMI 屬於肥胖的人,並輸出 :

Sophia
Jackson

(Answer) HW 1 : Question 1

In [1]:
name = ['Emma', 'Eathon', 'Sophia', 'Mia', 'Aria', 'Jackson', 'Easton', 'Vivian', 'Chase', 'Jason']
height = [157, 164, 158, 169, 178, 159, 169, 171, 165, 168]
weight = [52, 59, 70, 60, 65, 74, 40, 63, 66, 59]
In [2]:
# 1.(1) & 1.(2) & 1.(3)
bmi_list = []
fat_list = []
fat_name = []
for i ,j ,k in zip(weight,height,name):
        bmi = (i) / ((j/100)**2)
        bmi_list.append( bmi )
        if bmi < 18.5:
            fat_list.append( '體重過低' )
        elif bmi >= 18.5 and bmi <= 23.9 :
            fat_list.append( '體重正常' )
        elif bmi >= 24 and bmi <= 27.9 :
            fat_list.append( '超重' )
        else : 
            fat_list.append( '肥胖' )
            fat_name.append( k )
            
print("BMI List : " ,bmi_list)
print("-------------------------------------------------")
print("FAT List : " ,fat_list)
print("-------------------------------------------------")
print("FAT NAME : " ,fat_name)
BMI List :  [21.096190514828187, 21.936347412254616, 28.040378144528116, 21.007667798746546, 20.515086478979924, 29.270994027135, 14.00511186583103, 21.545090797168363, 24.242424242424246, 20.904195011337873]
-------------------------------------------------
FAT List :  ['體重正常', '體重正常', '肥胖', '體重正常', '體重正常', '肥胖', '體重過低', '體重正常', '超重', '體重正常']
-------------------------------------------------
FAT NAME :  ['Sophia', 'Jackson']

(Answer) HW 1 : Question 1

In [3]:
# 1.(3)
for i ,j in zip(fat_list,name):
    if i =='肥胖':
        print(j)
    else:
        continue
Sophia
Jackson

HW 1 : Question 2

下面是兩則世大運相關的新聞

news1 = "Taipei - The Universiade-themed interior design featured in some Taipei Metro trains have attracted attention of foreign media. Fancy a swim or a game of basketball in the Taipei Metro ? The Singapore-based Strait Times asked in its report of the ad . Now you can - thanks to the help of some realistic 3D artwork that has transformed a six-car subway train into six different sporting venues , the newpaper report. There is a swimming pool , a football pitch , a baseball diamond , a basketball court, a running track and a field for throwing sports . The car with the swimming pool venue , which looks like a real water surface , has proven to be the most popular with commuters and tourists on social media , Strait Times said . Several commuters , decked out in swim gear , have also been spotted taking advantage of the Instagram-worthy photo opportunities , the paper continued . The artwork was also covered by the CNN , which interviewed the local water sports athlete Hsu Tzu-Hsiung who took photo of himself swimming in the metro car . Hsu told CNN : I waited until most of the passengers got off the train to take pictures . I think it's a fun campaign to raise awareness about sport in general among Taiwanese and get the public to get involved in the games ."
news2 = "International University Sports Federation ( FISU ) president Oleg Matytsin yesterday said the term Chinese Taipei has been used for Taiwan's Taipei Summer Universiade teams because the federation must conform to the International Olympic Committee's ( IOC ) Olympic Movement rules. Matytsin made the remark at a news conference in Taipei yesterday, a few hours before the Universiade opening ceremony. He said that delegations from 141 countries , including 7,746 athletes and 3,796 support staff , would be attending the Universiade this year . As of Friday , 119 delegations, including 5,477 athletes and 2,619 support staff , had arrived . The official Universiade media guide had used Chinese Taipei , but later changed it to Taiwan after legislators and the public expressed their discontent . Earlier this month, the Universiade Organizing Committee said that the term Chinese Taipei was changed by the federation during a content review , but the committee changed it back to Taiwan in the geographical introduction section of the guide, and informed the federation . Yesterday , Matytsin said the media guide was not corrected and the change to Taiwan was decided on to introduce the country and its outlying islands . The federation is an IOC-recognized organization , so it has to conform to the Olympic Movement rules and regulations , he said , adding that he knows many people are disappointed about using Chinese Taipei , but the rules must be followed . The flag and team logo must remain as Chinese Taipei . Respect the rules , Matytsin said . Today , I believe it's very important to educate people how to play according to the rules . Matytsin said Taipei has provided high-quality accommodation to athletes and support staff from around the world , and the passion and hospitality shown in Taipei are considered gold-medal standard . For the young athletes attending the Universiade it will be a positive experience and they will likely remember Taipei if they go on to become leaders in sports, business or politics , he said ."

HW 1 : Question 2

(1) 試寫出一個輸入新聞及查詢字串 function ,此最後會回傳該查詢字串出現的次數

Function 可以定義為以下形式:

def check_news_wordcount( news, word )

HW 1 : Question 2

(2) 利用剛剛建立的 function check_news_wordcount 分別計算 news1 及 news2 中 Taipei 出現的次數

  • news1 的結果 :
    print( check_news_wordcount( news1, 'Taipei' ) )
    
    3
  • news2 的結果 :
    print( check_news_wordcount( news2, 'Taipei' ) )
    
    10

(Answer) HW 1 : Question 2

In [4]:
news1 = "Taipei - The Universiade-themed interior design featured in some Taipei Metro trains have attracted attention of foreign media. Fancy a swim or a game of basketball in the Taipei Metro ? The Singapore-based Strait Times asked in its report of the ad . Now you can - thanks to the help of some realistic 3D artwork that has transformed a six-car subway train into six different sporting venues , the newpaper report. There is a swimming pool , a football pitch , a baseball diamond , a basketball court, a running track and a field for throwing sports . The car with the swimming pool venue , which looks like a real water surface , has proven to be the most popular with commuters and tourists on social media , Strait Times said . Several commuters , decked out in swim gear , have also been spotted taking advantage of the Instagram-worthy photo opportunities , the paper continued . The artwork was also covered by the CNN , which interviewed the local water sports athlete Hsu Tzu-Hsiung who took photo of himself swimming in the metro car . Hsu told CNN : I waited until most of the passengers got off the train to take pictures . I think it's a fun campaign to raise awareness about sport in general among Taiwanese and get the public to get involved in the games ."
news2 = "International University Sports Federation ( FISU ) president Oleg Matytsin yesterday said the term Chinese Taipei has been used for Taiwan's Taipei Summer Universiade teams because the federation must conform to the International Olympic Committee's ( IOC ) Olympic Movement rules. Matytsin made the remark at a news conference in Taipei yesterday, a few hours before the Universiade opening ceremony. He said that delegations from 141 countries , including 7,746 athletes and 3,796 support staff , would be attending the Universiade this year . As of Friday , 119 delegations, including 5,477 athletes and 2,619 support staff , had arrived . The official Universiade media guide had used Chinese Taipei , but later changed it to Taiwan after legislators and the public expressed their discontent . Earlier this month, the Universiade Organizing Committee said that the term Chinese Taipei was changed by the federation during a content review , but the committee changed it back to Taiwan in the geographical introduction section of the guide, and informed the federation . Yesterday , Matytsin said the media guide was not corrected and the change to Taiwan was decided on to introduce the country and its outlying islands . The federation is an IOC-recognized organization , so it has to conform to the Olympic Movement rules and regulations , he said , adding that he knows many people are disappointed about using Chinese Taipei , but the rules must be followed . The flag and team logo must remain as Chinese Taipei . Respect the rules , Matytsin said . Today , I believe it's very important to educate people how to play according to the rules . Matytsin said Taipei has provided high-quality accommodation to athletes and support staff from around the world , and the passion and hospitality shown in Taipei are considered gold-medal standard . For the young athletes attending the Universiade it will be a positive experience and they will likely remember Taipei if they go on to become leaders in sports, business or politics , he said ."

(Answer) HW 1 : Question 2

In [5]:
# 2.(1)
def check_news_wordcount( news, word ):
    ''' 
    第一個參數 news: 輸入一則文章
    第二個參數 word: 輸入想計算次數的文字
    回傳:word 在該篇文之的次數
    '''
    news_split = news.split(" ")
    word_count = 0
    for i in news_split:
        if i== word:
            word_count += 1  
    return word_count
In [6]:
# 2.(2)
print( check_news_wordcount( news1, 'Taipei' ) )
print( check_news_wordcount( news2, 'Taipei' ) )
3
10

HW 1 : Question 3

下面是 Taipei , Kaohsiung 不同月份溫度資料

資料格式依序為: '月份' : [月均溫,最低溫日期,最低溫,最高溫日期,最高溫]

Taipei = {     'FEB' : [17.0,'2017/02/19',27.9,'2017/02/10',10.4] ,
               'JAN' : [8.3,'2017/01/04',27.3,'2017/01/20',12.2] , 
               'DEC' : [20.1,'2016/12/21',30.6,'2016/12/28',12.6] , 
               'NOV' : [22.6,'2016/11/13',32.5,'2016/11/24',17.6] , 
               'OCT' : [27.0,'2016/10/02',35.4,'2016/10/30',21.4] , 
               'SEP' : [27.8,'2016/09/16',35.9,'2016/09/20',23.6] , 
               'AUG' : [30.1,'2016/08/04',37.8,'2016/08/31',24.6] , 
               'JUL' : [30.3,'2016/07/28',38.5,'2016/07/18',23.9] , 
               'JUN' : [29.4,'2016/06/01',38.7,'2016/06/15',24.1] , 
               'MAY' : [27.1,'2016/05/31',37.3,'2016/05/17',19.0] , 
               'APR' : [24.0,'2016/04/23',32.2,'2016/04/18',17.1] , 
               'MAR' : [17.5,'2016/03/5',29.4,'2016/03/28',9.4]                                                                                                        
             }  

Kaohsiung = {  'FEB' : [21.1,'2017/02/22',29.8,'2017/02/10',13.8] ,
               'JAN' : [21.7,'2017/01/08',28.5,'2017/01/26',14.7] , 
               'DEC' : [22.9,'2016/12/05',30.0,'2016/12/16',16.2] , 
               'NOV' : [25.7,'2016/11/19',31.9,'2016/11/27',19.7] , 
               'OCT' : [28.4,'2016/10/21',33.0,'2016/10/10',24.0] , 
               'SEP' : [28.4,'2016/09/12',33.9,'2016/09/15',23.4] , 
               'AUG' : [29.4,'2016/08/02',34.2,'2016/08/30',24.8] , 
               'JUL' : [30.2,'2016/07/04',35.1,'2016/07/10',25.4] , 
               'JUN' : [29.7,'2016/06/27',34.8,'2016/06/11',24.6] , 
               'MAY' : [28.9,'2016/05/27',33.6,'2016/05/11',24.8] , 
               'APR' : [26.9,'2016/04/18',33.0,'2016/04/19',21.8] , 
               'MAR' : [21.6,'2016/03/19',29.7,'2016/03/26',12.8]                                                                                                        
             }

HW 1 : Question 3

(1) 分別取出 Taipei , Kaohsiung 的月均溫的部分,並存成 list

print('Taipei 月均溫' ,Taipei_average)
Taipei 月均溫 [17.0, 8.3, 20.1, 22.6, 27.0, 27.8, 30.1, 30.3, 29.4, 27.1, 24.0, 17.5]
print('Kaohsiung 月均溫' ,Kaohsiung_average)
Kaohsiung 月均溫 [21.1, 21.7, 22.9, 25.7, 28.4, 28.4, 29.4, 30.2, 29.7, 28.9, 26.9, 21.6]

HW 1 : Question 3

(2) 建立一個可計算標準差的函數,名為 my_std

標準差公式 $$ Std = \sqrt{\frac{\sum_{i=1}^n (x_i - \mu)^2 }{n}} $$

HW 1 : Question 3

(3) 利用 my_std 分別計算 Taipei , Kaohsiung 月均溫的標準差

print("Taipei 月均溫的標準差 : ",my_std( Taipei_average ))
Taipei 月均溫的標準差 :  6.391183162938003
print("Kaohsiung  月均溫的標準差 : ",my_std( Kaohsiung_average ))
Kaohsiung  月均溫的標準差 :  3.3477500736398396

(Answer) HW 1 : Question 3

In [7]:
Taipei = {     'FEB' : [17.0,'2017/02/19',27.9,'2017/02/10',10.4] ,
               'JAN' : [8.3,'2017/01/04',27.3,'2017/01/20',12.2] , 
               'DEC' : [20.1,'2016/12/21',30.6,'2016/12/28',12.6] , 
               'NOV' : [22.6,'2016/11/13',32.5,'2016/11/24',17.6] , 
               'OCT' : [27.0,'2016/10/02',35.4,'2016/10/30',21.4] , 
               'SEP' : [27.8,'2016/09/16',35.9,'2016/09/20',23.6] , 
               'AUG' : [30.1,'2016/08/04',37.8,'2016/08/31',24.6] , 
               'JUL' : [30.3,'2016/07/28',38.5,'2016/07/18',23.9] , 
               'JUN' : [29.4,'2016/06/01',38.7,'2016/06/15',24.1] , 
               'MAY' : [27.1,'2016/05/31',37.3,'2016/05/17',19.0] , 
               'APR' : [24.0,'2016/04/23',32.2,'2016/04/18',17.1] , 
               'MAR' : [17.5,'2016/03/5',29.4,'2016/03/28',9.4]                                                                                                        
             }  

Kaohsiung = {  'FEB' : [21.1,'2017/02/22',29.8,'2017/02/10',13.8] ,
               'JAN' : [21.7,'2017/01/08',28.5,'2017/01/26',14.7] , 
               'DEC' : [22.9,'2016/12/05',30.0,'2016/12/16',16.2] , 
               'NOV' : [25.7,'2016/11/19',31.9,'2016/11/27',19.7] , 
               'OCT' : [28.4,'2016/10/21',33.0,'2016/10/10',24.0] , 
               'SEP' : [28.4,'2016/09/12',33.9,'2016/09/15',23.4] , 
               'AUG' : [29.4,'2016/08/02',34.2,'2016/08/30',24.8] , 
               'JUL' : [30.2,'2016/07/04',35.1,'2016/07/10',25.4] , 
               'JUN' : [29.7,'2016/06/27',34.8,'2016/06/11',24.6] , 
               'MAY' : [28.9,'2016/05/27',33.6,'2016/05/11',24.8] , 
               'APR' : [26.9,'2016/04/18',33.0,'2016/04/19',21.8] , 
               'MAR' : [21.6,'2016/03/19',29.7,'2016/03/26',12.8]                                                                                                        
             }

(Answer) HW 1 : Question 3

In [8]:
# 3.(1)
Taipei_average = []
Kaohsiung_average = []

for (month, value) in Taipei.items():
    Taipei_average.append( value[0] )
    
for (month, value) in Kaohsiung.items():
    Kaohsiung_average.append( value[0] )    
In [9]:
print('Taipei 月均溫' ,Taipei_average)
print('Kaohsiung 月均溫' ,Kaohsiung_average)
Taipei 月均溫 [17.0, 8.3, 20.1, 22.6, 27.0, 27.8, 30.1, 30.3, 29.4, 27.1, 24.0, 17.5]
Kaohsiung 月均溫 [21.1, 21.7, 22.9, 25.7, 28.4, 28.4, 29.4, 30.2, 29.7, 28.9, 26.9, 21.6]

(Answer) HW 1 : Question 3

In [10]:
# 3.(2)
def my_std( x ):
    "Standard deviation"
    def my_sum( x ):
        "Total"
        temp_total = 0
        for i in x:
            temp_total = temp_total+i
        return temp_total    
    def my_count( x ):
        "Count"
        temp_count = 0
        for i in x :
            temp_count += 1
        return(temp_count)   
    
    temp_mean = my_sum( x ) / my_count( x )
    
    temp = 0
    for i in x:
        temp = temp + ( i-temp_mean )**2
    
    temp_std = ( temp / my_count( x ) ) **(1/2)
    return temp_std

(Answer) HW 1 : Question 3

In [11]:
# 3.(3)
print("Taipei 月均溫的標準差 : ",my_std( Taipei_average ))
print("Kaohsiung  月均溫的標準差 : ",my_std( Kaohsiung_average ))
Taipei 月均溫的標準差 :  6.391183162938003
Kaohsiung  月均溫的標準差 :  3.3477500736398396

HW 1 : Question 4

建立一個 is_prime.py 的檔

可以用來判斷一整數是否為質數的程式,當你在 Terminal / Anaconda Prompt 中給定一個整數時,會回傳你 TrueFalse

HW1

(Answer) HW 1 : Question 4

import sys

try:
    num = int(sys.argv[1])

    if num > 2:
        for i in range(2,num):
            if (num % i) == 0:
                result = False
                break
            else:
                result = True
    else:
        result = True

    print(result)

except ValueError as e:
    error = "請輸入一個整數的數字" + "\n" + 'Error message: ' + str(e)
    print(error)