Python Tutorial 第二堂(1)數值與字串型態
Python Tutorial 第一堂(4)Unicode 支援、基本 I/O << 前情 此文件已有新版,詳見〈Python 3 Tutorial 第二堂(2)數值與字串型態〉。 第二堂上課囉!在知道怎麼撰寫、執行第一個 Python 程式之後,接下來就要瞭解一下 Python 這個程式語言,只不過我們只剩五個小時,因而採取的方式是,在這一個小時很快地瀏覽過 Python 語言的重要基本元素,之後四個小時再從更多實際的範例中瞭解 Python 語言。 那麼,哪些東西才是語言中重要而基本的元素呢?Pascal 之父 Niklaus E. Writh 曾說過: Algorithms + Data Structures = Programs 演算法與資料結構就等於程式,而一門語言提供的資料型態(Data type)、運算子(Operator)、程式碼封裝方式等,會影響演算法與資料結構的實作方式,因此這一堂對於 Python 語言講解的重點將選定在:
內建型態在 Python 中,萬物皆物件!不過,物件導向並非 Python 的主要典範(Paradigm),Python 之父 Guido van Rossum 曾言,自己並非物件導向之信徒,在《Masterminds of Programming》書中,Guido van Rossum 說到: Python 支援程序式的(Procedural)程式設計以及(某些程度)物件導向。這兩者沒太大不同,然而 Python 的程序式風格仍強烈受到物件影響(因為基礎的資料型態都是物件)。Python 支援小部份函數式(Functional)程式設計 — 不過它不像任何真正的函數式語言。 無論如何,接下來要認識的內建型態都是物件,像是:
這篇文章中,我們要先來認識數值型態與字串型態 … 數值型態我們直接進入 Python 互動環境來瞭解這些型態吧!可以使用 ~$ python Python 2.7.3 (default, Aug 1 2012, 05:16:07) [GCC 4.6.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> type(1) # 1 是什麼型態? <type 'int'> >>> type(1L) # 加上 L 呢? <type 'long'> >>> type(111111111111111111111111111111111) # 太長的整數會自動使用 long 型態 <type 'long'> >>> type(3.14) # 浮點數是 float 型態 <type 'float'> >>> type(True) # 布林值是 bool 型態 <type 'bool'> >>> type(3 + 4j) # 支援複數的 complex 型態 <type 'complex'> >>> 2 ** 100 # 2 的 100 次方 1267650600228229401496703205376L >>> 上面的範例中使用了 >>> 10 / 3 3 >>> 10 // 3 3 >>> 10 / 3.0 3.3333333333333335 >>> 10 // 3.0 3.0 >>> 然而 >>> 10 / 3 3.3333333333333335 >>> 10 // 3 3 >>> 10 / 3.0 3.3333333333333335 >>> 10 // 3.0 3.0 >>> 在浮點數精確度的表現上,也有必須注意的地方。例如: >>> 1.0 - 0.8 0.19999999999999996 >>> print (1.0 - 0.8) 0.2 >>> 多數 CPU 與浮點運算器多採用 IEEE754 浮點數運算(IEEE 754 floating-point arithmetic),某些浮點數本身就有誤差,這是每個程式人都應該知道的事。Python 互動環境在顯示值時,採用制式的(Offical)字串表示,而 >>> repr(1.0 - 0.8) '0.19999999999999996' >>> str(1.0 - 0.8) '0.2' >>> 雖然你也可以用 簡單來說,用 想要精確地表示浮點數,語言都會提供程式庫,Python 中使用 >>> import decimal >>> a = decimal.Decimal('1.0') >>> b = decimal.Decimal('0.8') >>> a - b Decimal('0.2') >>> print (a - b) 0.2 >>> 字串型態如果要在 Python 中表示字串,可以使用 >>> "Just'in" "Just'in" >>> 'Just"in' 'Just"in' >>> 'c:\workspace' 'c:\\workspace' >>> "c:\workspace" 'c:\\workspace' >>> 可以看到,在某些情況下,你不用特別略過(Escape) >>> 'c:\todo' 'c:\todo' >>> print 'c:\todo' c: odo >>> print 'c:\\todo' c:\todo >>> 可以在字串前加上 >>> r'c:\todo' 'c:\\todo' >>> print r'c:\todo' c:\todo >>> Python 中的字串不可變(Immutable),你無法改變已建立的字串內容;想得知字串實際的位元組序列長度(不一定是字元長度),可以使用 >>> name = 'Justin' >>> len(name) 6 >>> for ch in name: ... print ch ... J u s t i n >>> 'Just' in name True >>> name + name 'JustinJustin' >>> name * 3 'JustinJustinJustin' >>> 可以使用 >>> lang = 'Python' >>> lang[0] 'P' >>> lang[-1] 'n' >>>
>>> lang[1:5] # 取得索引 1 至 5(包括 1 但不包括 5)的子字串 'ytho' >>> lang[0:] # 省略結尾索引,表示取至尾端 'Python' >>> lang[:6] # 省略起始索引,表示從 0 開始 'Python' >>>
>>> lang[0:6:2] 'Pto' >>>
>>> lang[::-1] 'nohtyP' >>> 如果要進行字串格式化,以下是舊式寫法: >>> '%d %.2f %s' % (1, 99.3, 'Justin') '1 99.30 Justin' >>> '%(real)s is %(nick)s' % {'real' : 'Justin', 'nick' : 'caterpillar'} 'Justin is caterpillar' >>> 技術上來說,字串物件將 >>> '{0} is {1}'.format('Justin', 'caterpillar') 'Justin is caterpillar' >>> '{real} is {nick}'.format(real = 'Justin', nick = 'caterpillar') 'Justin is caterpillar' >>> '{0} is {nick}'.format('Justin', nick = 'caterpillar') 'Justin is caterpillar' >>> import sys >>> 'My platform is {pc.platform}'.format(pc = sys) 'My platform is linux2' >>> 容器型態及 |