Python
這部分主要是參考The Python Standard Library的內容所撰寫。
程式語言的3個基礎
資料型態(Data Type)
在開始介紹資料型態之前,先介紹Python內建的常數,注意這些常數所屬的資料型別,總共有6個:
False: types.BooleanType
True: types.BooleanType
None: types.NoneType
NotImplemented: types.NotImplementedType
Ellipsis: types.EllipsisType
__debug__: types.BooleanType,其值一般是True,可以使用「-O」命令列選項改變為False,意思是最佳化Python的bytecode。
另外,site模組也有定義5個常數,一般而言Python都會預設匯入使用。若執行python不要匯入site模組,則使用「-S」命令列選項。
quit: site.Quitter,這個常數是可呼叫的(callable),定義在Quitter類別。
exit: site.Quitter,這個常數是可呼叫的(callable),定義在Quitter類別。
copyritht: site._Printer
license: site._Printer
credits: site._Printer
Python內建資料型別的數量可以參照types模組所定義的內建型別名稱(name),使用dir(types)就可以知道有哪些型別名稱,總共有37個名稱,實際上使用的資料型別只有28個,若要將資料型別分類,大致上可以分成下列5類:
數值(numeric)型別:bool, int, float, long, complex
序列(sequence)型別:str, unicode, (basestring), list, tuple, (enumerate), bytearray, buffer, xrange
集合(set)型別:set, frozenset
對應(mapping)型別:dict
其他:object, type, file, memoryview, module, class, function, method, (property, classmethod, staticmethod, super, buffer)
這些資料型別當中,依據操作方式可以分成「可變更(mutable)」和「不可變更(immutable)」兩種型別:
mutable:list, bytearray, set, dict
immutable:str, unicode, list, frozenset
資料型別清單:
basestring
bool
buffer
bytearray
bytes
classmethod
complex
dict
enumerate
file
float
frozenset
int
list
long
memoryview
object
property
reversed
set
slice
staticmethod
str
super
tuple
type
unicode
xrange
內建例外總共有48個,但在Windows平台中多一個WindowsError例外。
ArithmeticError
AssertionError
AttributeError
BaseException
BufferError
BytesWarning
DeprecationWarning
EOFError
EnvironmentError
Exception
FloatingPointError
FutureWarning
GeneratorExit
IOError
ImportError
ImportWarning
IndentationError
IndexError
KeyError
KeyboardInterrupt
LookupError
MemoryError
NameError
NotImplementedError
OSError
OverflowError
PendingDeprecationWarning
ReferenceError
RuntimeError
RuntimeWarning
StandardError
StopIteration
SyntaxError
SyntaxWarning
SystemError
SystemExit
TabError
TypeError
UnboundLocalError
UnicodeDecodeError
UnicodeEncodeError
UnicodeError
UnicodeTranslateError
UnicodeWarning
UserWarning
ValueError
Warning
WindowsError
ZeroDivisionError
上述這些Python預設常數、函式和例外等,都是藉由__builtin__模組取得,或是直接使用dir(__builtins__)。目前2.7.3版本利用dir(__builtins__)指令會列出144個(Windows系統是145個,多一個WindowsError),其中bytes型別是Python 3.0才正式新增,但目前已經加入。144中去除_, __doc__, __name__, __package__這四個屬性,僅有140個。
分支(Branch)
Python語言的分支語法有兩大類「if陳述」和「try陳述」。
if陳述(if statement)
if_stmt ::= "if" expression ":" suite
( "elif" expression ":" suite )*
["else" ":" suite]
try陳述(try statement)
try_stmt ::= try1_stmt | try2_stmt
try1_stmt ::= "try" ":" suite
("except" [expression [("as" | ",") target]] ":" suite)+
["else" ":" suite]
["finally" ":" suite]
try2_stmt ::= "try" ":" suite
"finally" ":" suite
迴圈(Loop)
Python語言的迴圈語法有兩大類「while陳述」和「for陳述」。
while陳述(while statement)
while_stmt ::= "while" expression ":" suite
["else" ":" suite]
for陳述(for statement)
for_stmt ::= "for" target_list "in" expression_list ":" suite
["else" ":" suite]
再利用的問題
函式(Function)
Pyhton內建的函式總共有80個,結構上分為兩大類,其中之一是內建函式,另一類是型別函式,當呼叫型別函式時,其回傳值是一種內建型別,其實型別函式就是型別的建構子。內建函式總共52個,而型別函式的數目等於資料型別數量,即27個,此外,還有一個help函式。
注意,input和raw_input若不是在Interactive Mode的話,它的型別將會是FunctionType。
abs(), 內建函式
all(), 內建函式
any(), 內建函式
basestring(), 型別
bin(), 內建函式
bool(), 型別
bytearray(), 型別
callable(), 內建函式
chr(), 內建函式
classmethod(), 型別
cmp(), 內建函式
compile(), 內建函式
complex(), 型別
delattr(), 內建函式
dict(), 型別
dir(), 內建函式
divmod(), 內建函式
enumerate(), 型別
eval(), 內建函式
execfile(), 內建函式
file(), 型別
filter(), 內建函式
float(), 型別
format(), 內建函式
frozenset(), 型別
getattr(), 內建函式
globals(), 內建函式
hasattr(), 內建函式
hash(), 內建函式
help(),由site模組加入至built-in名稱空間之中,是site模組中定義的一個函式。
hex(), 內建函式
id(), 內建函式
input(), 內建函式
int(), 型別
isinstance(), 內建函式
issubclass(), 內建函式
iter(), 內建函式
len(), 內建函式
list(), 型別
locals(), 內建函式
long(), 型別
map(), 內建函式
max(), 內建函式
memoryview(), 型別
min(), 內建函式
next(), 內建函式
object(), 型別
oct(), 內建函式
open(), 內建函式
ord(), 內建函式
pow(), 內建函式
print(), 內建函式
property(), 型別
range(), 內建函式
raw_input(), 內建函式
reduce(), 內建函式
reload(), 內建函式
repr(), 內建函式
reversed(), 型別
round(), 內建函式
set(), 型別
setattr(), 內建函式
slice(), 型別
sorted(), 內建函式
staticmethod(), 型別
str(), 型別
sum(), 內建函式
super(), 型別
tuple(), 型別
type(), 型別
unichr(), 內建函式
unicode(), 型別
vars(), 內建函式
xrange(), 型別
zip(), 內建函式
__import__(), 內建函式
apply(), 內建函式, 非必需的(Non-essential)
buffer(), 型別, 非必需的(Non-essential)
coerce(), 內建函式, 非必需的(Non-essential)
intern(), 內建函式, 非必需的(Non-essential)
程式庫(Library)
除了內建的程式庫之外,Pyhton提供的標準程式庫相當多,去除特定平台,大致上可以分成26類服務。
String Services
Data Types
Numeric and Mathematical Modules
File and Directory Access
Data Persistence
Data Compression and Archiving
File Formats
Cryptographic Services
Generic Operating System Services
Optional Operating System Services
Interprocess Communication and Networking
Internet Data Handling
Structured Markup Processing Tools
Internet Protocols and Support
Multimedia Services
Internationalization
Program Frameworks
Graphical User Interfaces with Tk
Development Tools
Debugging and Profiling
Python Runtime Services
Custom Python Interpreters
Restricted Execution
Importing Modules
Python Language Services
Miscellaneous Services
Python documentation
The Python Tutorial
The Python Standard Library
The Python Language Reference
Built-in Types
Built-in Functions