close

1.資料型態(整數與字串):

ex:

>>>10+10

20                         #自動判斷為整數

>>>'10+10'

'10+10'                #自動判斷為字串

 

10+'10'

err                       #整數與字串無法運算

 

2.變數(儲存空間):

ex:

>>>x = 5           #定義x為5

>>>x                 #輸入x則顯示5

5

>>>x = 10        #此時定義x為10,則會覆蓋前次變數

>>>x

10

ex2:

>>>y = 20       #python變數應用

>>>x + y

30

 

3.內建功能(print(),str(),int()):

ex:

>>>print('Hello Word!')                 #輸出Hello Word!

Hello Word!

ex2:

>>>year = 2017                            #指定 year 為2017

>>>print('This year is ' + year)

err                                                  #print('This year is '為字串 year 是整數不是字串,故要將year轉換為字串才能一起輸出

>>>print('This year is ' + str(year))

This year is 2017

>>>year = '2017'

>>>print('Next year will be ' + str(int(year) + 1))             #定義year為字串時,要先將year 轉換為int才能運算,運算完畢要再轉換回str才能與字串'Next year will be'一起輸出

Next year will be 2018

 

4.內建功能(format()):

>>>year = 2017

>>>print('This year is ' + str(year))

This year is 2017

>>>print('This year is {}' .format(year))                            #使用format會自動把整數轉換為字串,並帶入{}中

This year is 2017

>>>print('This year is {},and next year will be {}' .format(year ,year + 1))                                  #.format(year ,year + 1 ) year會帶入第一個}{},year + 1 會帶入第二個{}

This year is 2017,and  next year will be 2018

>>>print('This year is {y},and I will go abroad at the end of {y}' .format(y = year))                  #變數應用也可導入format

This year is 2017,and I will go abroad at the end of 2017

 

=======================================================================================================

此內容取自 Raymond Chung  與  Kevin He 兩位講師的 實作6個APP快速入門課程

arrow
arrow
    文章標籤
    python 實作 BMI
    全站熱搜

    Gavem 發表在 痞客邦 留言(0) 人氣()