1. 程式人生 > >python 筆記 (2):核心物件型別

python 筆記 (2):核心物件型別

1. 核心資料型別(core data types): Numbers, Strings,Lists, Dictionaries, Tuples, Files, Sets, other core types(Booleans, Types, None), 

others; Program unit types(Functions, modules, classes),  Implementation-related types(Compiled code, stack tracebacks)

2. everything in python program is an object.; python is dynamicly typed(不需要事先宣告程式碼型別,但需要賦值),strongly typed(只有物件型別合法的操作才能用)

3.strings(sequences:位置有序的物件的集合)

index: 值可以為正或者負,當為負的時候,從右邊往前倒著計數:-1是倒數第一個元素;S[-1] = S[len(S)-1]

slicing: X[I:J]:擷取X的從I開始到J但不包含J的部分;左邊界預設是0,右邊界預設是X的長度。因此, X[:J] = X[0:J];

X[I:] = X[I:len(X)]; X[:] = X[0:len(X)]; X[:-1] = X[0:len(X)-1]

concatenation:  "abc" + 'xyz‘

repetition:  ’spam' ** 8

immutability: strings cannot be changed..

4. string-specific methods: find, replace, split, upper, isalpha...

5. format:   expression: '%s: eggs, and %s" % ('spam', 'SPAM')

mehtod: '{0}, eggs and {1}'.format('spam', 'SPAM')

6:字串可以用單或者雙引號包含;跨行的用三個單或者雙引號

7. lists(sequence): mutable, 包含的物件型別可以不同;可以有相同的物件;comprehension: doubles = [c * 2 for c in 'spam']

{ord(x) for x in 'spaam'}  {x: ord(x) for x in 'spaam'}

8: dictionaries (mappings):mapping 通過key儲存資料;don't maintain left-to-right order; mutable;nesting(可以包含 maping, lists物件)

if tests:

D= {a':1, 'b':2, 'c':3}

if 'd' in D:

print(D['d'])

tuples:  sequence, imutable, 元素型別可以是任意型別,可以任意巢狀