iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 30
1
自我挑戰組

從不懂,到玩上手的Python語言系列 第 30

Day30-總結30 天

型別、變數、常數、運算子、基本輸出/輸入

  • 1. 型別 (數值、字串…)

    • 1.1 Python 為動態型別程式語言,無需事先宣告資料型別

    • 1.2 未經明確定義的不能執行操作

    https://ithelp.ithome.com.tw/upload/images/20191016/20120926Z9NlpLu2vQ.png
  • 2. 數值型別

    • 2.1 int (整數,EX: 25、66)

    • 2.2 float (浮點數,EX: 12.345)

      • 2.2.1 >>> 2.5E3 # 2500

      • 2.2.2 >>> 2.6e-2 # 0.026

      https://ithelp.ithome.com.tw/upload/images/20191016/20120926BN667WmXyR.png
      • 2.2.3 overflow (溢味;超出最大數值範圍,約 10308)

    • 2.3 complex (複數,EX: 2+3j)

    • 2.4 bool (布林: True(1)、False(0),第一字母 T、F 為大寫)

      • 2.4.1 >>> 2 < 3 # True

      • 2.4.2 >>> 3>4 # False

      • 2.4.3 >>>a=True

  • 3 字串型別 (str)

    • 3.1 'test1'

    • 3.2 "test2"

    • 3.3 ''' this is a test, this is a test, this is a test,this is a test, this is a test,'''

    • 3.4 """ this is a test, this is a test, this is a test,this is a test, this is a test,"""

    https://ithelp.ithome.com.tw/upload/images/20191016/20120926UAyKVDxTG8.png
  • 4 變數

    • 4.1 程式設定某資料在記憶體暫存,須透過識別字對應存取,變數為識別字的一種。( EX: a = 3 )

    • 4.2 變數命名規則

      • 4.2.1 第一個字元須為英文字母或是底線

      • 4.2.2 其餘字元可為其他英文字母、數字、底線、中文

      • 4.2.3 不能使用關鍵字、保留字 (會有錯誤訊息)

      • 4.2.4 英文字母大小寫有區別

    • 4.3 變數值的指派

      • 4.3.1 變數名稱 = 變數值

      • 4.3.2 指派「=」的意義,不是數學上的"等於"

      https://ithelp.ithome.com.tw/upload/images/20191016/20120926UKoTG2RRKi.png
  • 5 常數

    • 5.1 用法類似變數

    • 5.2 為識別字的一種,若程式當中多處會用到同一固定常數,可設定常數識別字,在程式中使用該常數識別字,省去重複輸入可能複雜的一串數字。若須修改,也不須各處修改,只需修改宣告值即可。

  • 6 運算子

    • 6.1 算數運算子

    https://ithelp.ithome.com.tw/upload/images/20191016/201209267AGkRHmMqV.png
  • 6.2 指派運算子

https://ithelp.ithome.com.tw/upload/images/20191016/20120926boD5gG2Wgs.png

  • 6.3 比較運算子

https://ithelp.ithome.com.tw/upload/images/20191016/20120926k6UTyk8mCT.png

  • 6.4 邏輯運算子

    • 6.4.1 and (EX: >>>5>4 and 3>4# False )

    https://ithelp.ithome.com.tw/upload/images/20191016/20120926cH0UWLOxCU.png

    • 6.4.2 or(EX: >>>5>4 or 3>4 # True )

    https://ithelp.ithome.com.tw/upload/images/20191016/20120926CRelBe9Gxq.png

    • 6.4.3 not (EX: >>>not 5>4 # False )

    https://ithelp.ithome.com.tw/upload/images/20191016/20120926RpUBJzmToY.png

  • 6.5 運算子優先順序

https://ithelp.ithome.com.tw/upload/images/20191016/20120926F81Heumy3V.png

  • 7 基本輸出 print

    • 7.1 列印結束符號,預設為'\n'換列字元,可用參數 end=來自訂

    • 7.2 列印當中分隔符號,預設為' '空白字元,可用參數 sep=來自訂

    • 7.3 >>>print('abc', 'ijk'); print('abc', 'ijk')

    • 7.4 >>>print('abc', 'ijk', sep=',',end=''); print('abc', 'ijk')

    • 7.5 print(), 表示增加一空白列

  • 8 基本輸入 input #輸入內容預設是被解讀為字串格式

    • 8.1 >>>input() #請測試輸入 456,回傳'456'

    • 8.2 >>>input() #請測試輸入'abc',回傳"'abc'"

    • 8.3 >>>input() #請測試輸入"abc",回傳'"abc"'

    • 8.4 >>>input('please input your name: ') #提示作用

  • 9 eval(),將取得的字串轉為數值

    • 9.1 >>>eval('456') # 回傳 456

    • 9.2 >>>eval('12+34') # 回傳 12+34 的結果

    • 9.3 abc = eval(input('pls input abc: '))

  • 10 format(item, format-specifier) #將 item 設為指定格式的字串

    • 10.1 [ [fill ] align ] [ sign] [0][width][,][.precision][type] #注意順序

    https://ithelp.ithome.com.tw/upload/images/20191016/20120926vHpw431imP.png

    • 10.2 常用範例

    https://ithelp.ithome.com.tw/upload/images/20191016/20120926LygBN9yqzE.png

    • 10.3 >>>print(format(50,'5d'))

    • 10.4 >>>print(format("abc",'5s'))

    • 10.5 >>>print(format(12.345,'6.2f'))

    • 10.6 >>>print(format(45,'6d'),55,format("AA",'<5s'),'a')

    • 10.7 注意:format (…)並非一定要搭配 print 使用

    • 10.8 >>> a =format("asdf",'10') # a='asdf

    • 10.9 其他範例

    • 10.10 >>>format(123,'<12')

    • 10.11 >>>format(123,'>12')

    • 10.12 >>>format(123,'^12')

    • 10.13 >>>format(123,'$^12')

    • 10.14 >>>format(12345678, ',')

    • 10.15 >>>format(12345678,'15,.2f')

    • 10.16 >>>format(123,'=+010')

    • 10.17 >>>format(1234.5678,'12.2f')

    • 10.18 >>>format(1234.5678,'12.2e')

    • 10.19 >>>format(12,'12.2e')

    • 10.20 >>>format(12,'12.2%')

    • 10.21 >>>format(1234.5678,'<12.2f')

    • 10.22 >>>format(1234.5678,'^12.2f')

    • 10.23 >>>format('this is a test','30')

    • 10.24 >>>format('this is a test','>30')

    • 10.25 >>>format('this is a test','^30')

    • 10.26 >>>format('this is a test','10')

數值與字串基本函數

  • 1 函數(function)?

  • 2 內建數值函數

    • 2.1 abs(x),傳回 x 的絕對值 #>>>a=abs(-13)

    • 2.2 min(x1, x2[,x3…]),最小值

    • 2.3 max(x1, x2[,x3…]),最大值

    • 2.4 int(x),x 的整數部分

    • 2.5 round(x[,precision]),取 x 四捨五入到小數後指定的精確位數,若是忽略 precision 則表示 0,就是到整數位

    • 2.6 pow(x, y),x 的 y 次方

    • 2.7 float(x),x 字串轉為浮點數的結果

    • 2.8 complex(x),x 字串轉為複數的結果

  • 3 載入 math 模組 (import math)

    • 3.1 math.pi,圓周率

    • 3.2 math.e,自然對數的底數 e

    • 3.3 math.ceil(x),比 x 大 1 的整數

    • 3.4 math.fabs(x),x 的浮點數的絕對值

    • 3.5 math.factorial(x),x 的階承值 ( x! )

    • 3.6 math.floor(x),比 x 小 1 的整數

    • 3.7 math.gcd(x, y),x 與 y 的最大公因數

    • 3.8 math.exp(x),自然對數的底數 e 之 x 次方

    • 3.9 math.log(x[,base]),x 的自然對數值,預設底數為 e

    • 3.10 math.sqrt(x),x 的平方根

    • 3.11 math.radians(x),角度 x 換算為多少弳度

    • 3.12 math.degrees(x),弳度 x 換算為多少角度

    • 3.13 math.cos、math.sin、math.tan、math.acos、math.asin、math.ctan #參數須為弳度,不是角度


上一篇
Day29- 例外處理 (Exception)
系列文
從不懂,到玩上手的Python語言30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
阿展展展
iT邦好手 1 級 ‧ 2020-02-04 09:04:00

恭喜完賽 \0.0/
結果才說到運算子三十天就過了XD

我要留言

立即登入留言