天天看點

python學習筆記之标準資料類型

python中的資料類型有:

  1. Number(數字)
  2. String(字元串)
  3. List(清單)
  4. Tuple(元組)
  5. Dictionary(字典)

一、Python數字:

數字的分類:

1.int(整型)

2.long(長整型,python2.2以後的版本int溢出自動轉化為long,python3.x之後由int替代)

3.float(浮點型)

4.complex(複數)

使用:

age, height = 18, 175.2

print age
print height
print complex(age, height)
           

輸出:

18
175.2
(18+175.2j)
           

二、Python字元串:

字元串是文本常用資料類型。

python的字元串有兩種取值順序:

  1. 從左到右:索引預設0開始,最大範圍是字元串長度-1(類似Java)
  2. 從右到左:預設-1開始,最大範圍是字元串開頭(-length)

    使用方法:

content = "mynameisjack"

print content  # 輸出完整的字元串
print content[2: 12]    # 傳回正向下标2>12位置的字元串(左開右閉)
print content[-11: -1]  # 傳回反向下标-11>-1位置的字元串(左開右閉)
print content[0]    # 輸出0位置的字元串
print content[2:]   # 輸出2位置(包含)之後的所有字元串
print content * 2   # 輸出字元串2次
print content + "連接配接"    # 輸出拼接的字元串
           

輸出結果:

mynameisjack
nameisjack
ynameisjac
m
nameisjack
mynameisjackmynameisjack
mynameisjack連接配接
           

三、Python清單:

list = ['my', 123, 'name', 456, 'is', 789, 'jack']

tinylist = [000, 'test']

print list  # 輸出完整的清單
print list[2: 5]  # 傳回正向下标2>5位置的清單(左開右閉)
print list[-5: -1]  # 傳回反向下标-5>-1位置的清單(左開右閉)
print list[0]  # 輸出0位置的元素
print list[2:]  # 輸出2位置(包含)之後的所有清單
print list * 2  # 輸出清單2次
print list + tinylist  # 輸出拼接的清單
           

輸出結果:

['my', 123, 'name', 456, 'is', 789, 'jack']
['name', 456, 'is']
['name', 456, 'is', 789]
my
['name', 456, 'is', 789, 'jack']
['my', 123, 'name', 456, 'is', 789, 'jack', 'my', 123, 'name', 456, 'is', 789, 'jack']
['my', 123, 'name', 456, 'is', 789, 'jack', 0, 'test']
           

四、Python元組:

定義:

1.元組是另一個資料類型,類似于List。

2.元組用()辨別。

3.元組不能二次指派,相當于隻讀清單。

tuple = ('my', 123, 'name', 456, 'is', 789, 'jack')

tinytuple = (000, 'test')

print tuple  # 輸出完整的元組
print tuple[2: 5]  # 傳回正向下标2>5位置的元組(左開右閉)
print tuple[-5: -1]  # 傳回反向下标-5>-1位置的元組(左開右閉)
print tuple[0]  # 輸出0位置的元素
print tuple[2:]  # 輸出2位置(包含)之後的所有元組
print tuple * 2  # 輸出元組2次
print tuple + tinytuple  # 輸出拼接的元組
           

輸出結果:

('my', 123, 'name', 456, 'is', 789, 'jack')
('name', 456, 'is')
('name', 456, 'is', 789)
my
('name', 456, 'is', 789, 'jack')
('my', 123, 'name', 456, 'is', 789, 'jack', 'my', 123, 'name', 456, 'is', 789, 'jack')
('my', 123, 'name', 456, 'is', 789, 'jack', 0, 'test')
           

需要注意的是指派操作是無效的,比方說:

tuple = ('my', 123, 'name', 456, 'is', 789, 'jack')

list = ['my', 123, 'name', 456, 'is', 789, 'jack']

tuple[0] = 200  # 元組中是違法的
list[0] = 200   # 清單中是合法的
           

五、Python 字典:

定義:字典是無序的對象集合,以鍵值對的形式進行存取。字典用{}辨別。
dict = {}
dict['key'] = "this is value"
dict['jack'] = 18

tinydict = {'key': 'value', 'name': 'jack'}
print dict['key']   # 輸出'key'對應的值
print tinydict  # 輸出完整的字典(反向)
print tinydict.keys()   # 輸出字典中所有的鍵(反向)
print tinydict.values()     # 輸出字典中所有的值(反向)
           

輸出結果:

this is value
{'jack': 18, 'key': 'this is value'}
{'name': 'jack', 'key': 'value'}
['name', 'key']
['jack', 'value']
           

六、Python資料類型轉換:

函數 描述
int(x) 将x轉換為一個整數
long(x) 将x轉換為一個長整數
float(x) 将x轉換到一個浮點數
complex(x) 建立一個複數
str(x) 将對象 x 轉換為字元串
repr(x) 将對象 x 轉換為表達式字元串
eval(x) 用來計算在字元串中的有效Python表達式,并傳回一個對象
tuple(x) 将序列x轉換為一個元組
list(x) 将序列x轉換為一個清單
set(x) 轉換為可變集合
dict(x) 建立一個字典,x必須是一個序列(key,value)元組
frozenset(x) 轉換為不可變集合
chr(x) 将一個整數轉換為一個字元
unichr(x) 将一個整數轉換為Unicode字元
ord(x) 将一個字元轉換為它的整數值
hex(x) 将一個整數轉換為一個十六進制字元串
oct(x) 将一個整數轉換為一個八進制字元串