python的基本數(shù)據(jù)類型包括:
數(shù)字(int)
布爾值(bool)
字符串(str)
集合(set)
列表(list)
元組(tuple)
字典(dict)
數(shù)字(int)
數(shù)字類型包括:浮點(diǎn)型(float)、整數(shù)型(int)、長(zhǎng)整型(long)、復(fù)數(shù)型(complex)
用int函數(shù)將字符串按指定進(jìn)制轉(zhuǎn)換為整數(shù):
>>> int('111') #默認(rèn)按十進(jìn)制裝換
111
>>> int('111',2) #按二進(jìn)制轉(zhuǎn)換
7
>>> int('111',8) #按八進(jìn)制轉(zhuǎn)換
73
>>> int('111',16) #按十六進(jìn)制轉(zhuǎn)換
273
整數(shù)轉(zhuǎn)換為對(duì)應(yīng)進(jìn)制字符串:
>>> bin(666) #轉(zhuǎn)換為二進(jìn)制字符串
'0b1010011010'
>>> oct(666) #轉(zhuǎn)換為八進(jìn)制字符串
'0o1232'
>>> hex(666) #轉(zhuǎn)換為十六進(jìn)制字符串
'0x29a'
字符串(str)
python字符串常量可用多種方式表達(dá):
-單引號(hào):'hello'
-雙引號(hào):"hello"
-三引號(hào):'''hello'''
-帶r或R前綴:r'hello'、R'hello'
-帶u或U前綴:u'hello'、U'hello'
字符串的創(chuàng)建:
>>> x=str('hello') #創(chuàng)建字符創(chuàng)對(duì)象
>>> x
'hello'
>>> type(x) #檢測(cè)字符串對(duì)象類型
<class 'str'>
轉(zhuǎn)義字符:
轉(zhuǎn)義字符 | 說(shuō)明 | 轉(zhuǎn)義字符 | 說(shuō)明 |
---|---|---|---|
\\ | 反斜杠 | \n | 換行符 |
\' | 單引號(hào) | \" | 雙引號(hào) |
\a | 響鈴符 | \b | 退格符 |
\r | 回車(chē)符 | \r | 換頁(yè)符 |
\t | 水平制表符 | \v | 垂直制表符 |
\0 | Null,空字符串 | \ooo | 八進(jìn)制值表示的ASCII碼對(duì)應(yīng)字符 |
\xhh | 十六進(jìn)制值表示的ASCII碼對(duì)應(yīng)字符 |
字符串的基本操作:
>>> len('hello') #求字符串的長(zhǎng)度
5
#---------------------
>>> x='hello' #包含的判斷性
>>> 'a' in x
False
>>> 'h' in x
True
#---------------------
>>> 'h''e''l''l''o' #字符串的連接
'hello'
>>> 'he'+'llo'
'hello'
>>> 'hello'*3
'hellohellohello'
#---------------------
>>> for i in 'hell':print(i) #字符串的迭代
h
e
l
l
#---------------------
>>> x="hello" #索引
>>> x[0]
'h'
>>> x[-1]
'o'
#---------------------
>>> x="hello" #分片
>>> x[0]
'h'
>>> x[-1]
'o'
>>> x="hello"
>>> x[1:3]
'el'
>>> x[1:]
'ello'
>>> x[:3]
'hel'
>>> x[:-1] #除最后一個(gè)字符,其余全部返回
'hell'
>>> x[:] #返回全部字符
'hello'
>>> x="123456789"
>>> x[1:7:2]
'246'
>>> x[::2]
'13579'
>>> x[7:1:-2]
'864'
>>> x[::-1] #將字符串反序返回
'987654321'
字符串轉(zhuǎn)換:
#將整數(shù)轉(zhuǎn)換為字符串
>>> str(123)
'123'
#將浮點(diǎn)數(shù)轉(zhuǎn)換為字符串
>>> str(1.23)
'1.23'
#將復(fù)數(shù)轉(zhuǎn)換為字符串
>>> str(1+2j)
'(1+2j)'
字符串的方法:
#將字符串第一個(gè)字母大寫(xiě),其余為小寫(xiě)
>>> 'hello'.capitalize()
'Hello'
#---------------------
#將字符串的字母全部轉(zhuǎn)成小寫(xiě)
>>> 'Hello world'.lower()
'hello world'
#---------------------
#將字符串的字母全部轉(zhuǎn)成大寫(xiě)
>>> 'Hello world'.upper()
'HELLO WORLD'
#---------------------
#查找子字符串出現(xiàn)的次數(shù)
>>> 'hello,hello'.count('el')
2
#---------------------
#判斷字符串是否以‘xxx’開(kāi)頭
>>> 'hello'.startswith('h')
True
#---------------------
#判斷字符串是否以‘xxx結(jié)尾’
>>> 'hello'.endswith('o')
True
#---------------------
#查找子字符串返回第一次出現(xiàn)位置的偏移量
>>> x='hellohello'
>>> x.find('lo')
3
>>> x.find('lo',2)
3
>>> x.find('loo')#沒(méi)找到時(shí)返回-1
-1
#---------------------
#查找子字符串返回最后一次出現(xiàn)位置的偏移量
>>> 'hellohello'.rfind('lo')
8
#---------------------
#與find()方法相同,只是未找到子字符串時(shí)產(chǎn)生ValueError異常
>>> x='hellohello'
>>> x.find('lo',2)
3
>>> x='hellohello'
>>> x.index('lo')
3
>>> x.index('lo',2)
3
>>> x,index('loo')
Traceback (most recent call last):
File "<pyshell#19>", line 1, in <module>
x,index('loo')
NameError: name 'index' is not defined
#---------------------
#字符串格式化,將字符串用{}定義的替換預(yù)一次用參數(shù)表示
>>> 'My name is {0},age is{1}'.format('Jayss',20)
'My name is Jayss,age is20'
>>> 'My name is {name},age is{age}'.format_map({'name':'Jayss','age':20})
'My name is Jayss,age is20'
#---------------------
#由數(shù)字或字母組成時(shí)返回True
>>> '123'.isalnum()
True
>>> '123abc'.isalnum()
True
>>> '123&abc'.isalnum()
False
#---------------------
#字符串字符全部是字母時(shí)返回True
>>> 'Hello'.isalpha()
True
>>> 'Hello&Hello'.isalpha()
False
#---------------------
#字符串字符全部是大寫(xiě)字母時(shí)返回True
>>> 'HELLO'.isupper()
True
>>> 'Hello'.isupper()
False
#---------------------
#字符串字符全部是小寫(xiě)字母時(shí)返回True
>>> 'hello'.islower()
True
>>> 'Hello'.islower()
False
#---------------------
#字符串字符全部是數(shù)字時(shí)返回True
>>> '123'.isdecimal()
True
>>> '123abc'.isdecimal()
False
集合
唯一、無(wú)序、不可改變
創(chuàng)建集合:
#直接使用集合常量
>>> x={1,2,3}
>>> x
{1, 2, 3}
#用集合常量做參數(shù)創(chuàng)建集合對(duì)象
>>> set({1,2,3})
{1, 2, 3}
#用列表常量做參數(shù)創(chuàng)建集合對(duì)象
>>> set([1,2,3])
{1, 2, 3}
#用字符串常量做參數(shù)創(chuàng)建集合對(duì)象
>>> set('123')
{'1', '2', '3'}
#創(chuàng)建空集合
>>> set()
set()
>>> type({})
<class 'dict'>
集合運(yùn)算:
#定義集合
>>> x={1,2,'a','b'}
>>> y={3,4,'c','d'}
#求集合中元素的個(gè)數(shù)
>>> len(x)
4
#判斷元素是否屬于集合
>>> 'a' in x
True
#求差集
>>> x - y
{1, 2, 'a', 'b'}
#求并集
>>> x|y
{1, 2, 3, 'd', 4, 'c', 'a', 'b'}
#求求交集
>>> x&y
set()
#求對(duì)稱差
>>> x^y
{1, 2, 3, 'd', 4, 'c', 'a', 'b'}
#比較運(yùn)算符可用于判斷子集或超集關(guān)系
>>> x<y
False
>>> {1,2}<x
True
基本操作:
#創(chuàng)建集合
>>> x={1,2}
>>> x
{1, 2}
#---------------------
#復(fù)制集合對(duì)象
>>> y=x.copy()
>>> y
{1, 2}
#---------------------
#危機(jī)和添加一個(gè)元素
>>> x.add('abc')
>>> x
{'abc', 1, 2}
#---------------------
#危機(jī)和添加多個(gè)元素
>>> x.update({'a','b'})
>>> x
{1, 2, 'abc', 'a', 'b'}
#---------------------
#從集合中刪除指定元素
>>> x.remove('abc')
>>> x
{1, 2, 'a', 'b'}
#---------------------
#從集合中刪除指定元素
>>> x.discard('a')
>>> x
{1, 2, 'b'}
#---------------------
#從集合中隨機(jī)刪除一個(gè)元素并返回該元素
>>> x.pop()
1
>>> x
{2, 'b'}
#---------------------
#刪除集合中的全部元素
>>> x.clear()
>>> x
set()
凍結(jié)集合:
凍結(jié)集合是一個(gè)不可改變的集合,可作為其它集合的元素
#創(chuàng)建凍結(jié)集合
>>> x=frozenset([1,2,3])
>>> x #凍結(jié)集合的打印格式與普通集合不同
frozenset({1, 2, 3})
#創(chuàng)建普通集合
>>> y=set([4,5])
>>> y
{4, 5}
#將凍結(jié)集合作為元素加入另一集合
>>> y.add(x)
>>> y
{frozenset({1, 2, 3}), 4, 5}
#為凍結(jié)集合添加元素,報(bào)錯(cuò)
>>> x.add(123)
Traceback (most recent call last):
File "<pyshell#143>", line 1, in <module>
x.add(123)
AttributeError: 'frozenset' object has no attribute 'add'
列表
有序、可變、可直接修改
創(chuàng)建列表:
#創(chuàng)建一個(gè)空列表對(duì)象
>>> []
[]
>>> list()
[]
#不同類型數(shù)據(jù)創(chuàng)建列表對(duì)象
>>> [1,'a',(1,2,3),[1,2,3]]
[1, 'a', (1, 2, 3), [1, 2, 3]]
#迭代對(duì)象創(chuàng)建列表對(duì)象
>>> list('abcdef')
['a', 'b', 'c', 'd', 'e', 'f']
#用連續(xù)整數(shù)創(chuàng)建列表對(duì)象
>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
#用元組創(chuàng)建列表對(duì)象
>>> list((1,2,3))
[1, 2, 3]
基本操作:
#求長(zhǎng)度
>>> len([])
0
>>> len([1,2,3])
3
#---------------------
#合并
>>> [1,2]+[3,4]
[1, 2, 3, 4]
#---------------------
#重復(fù)
>>> [1,2]*3
[1, 2, 1, 2, 1, 2]
#---------------------
#迭代
>>> x=[1,2,(1,2,3),[1,2,3]]
>>> for i in x :print(i)
1
2
(1, 2, 3)
[1, 2, 3]
#---------------------
#關(guān)系判斷
>>> 2 in [1,2,3]
True
>>> 'a' in [1,2,3]
False
#---------------------
#索引
>>> x=[1,2,['a','b','c']]
>>> x[0]
1
>>> x[2]
['a', 'b', 'c']
>>> x[2][1]
'b'
>>> x[0]=11 #修改第一個(gè)列表對(duì)象
>>> x
[11, 2, ['a', 'b', 'c']]
#---------------------
#分片
>>> x=list(range(10)) #創(chuàng)建列表對(duì)象
>>> x
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> x[2:5] #返回分片列表
[2, 3, 4]
>>> x[:5]
[0, 1, 2, 3, 4]
>>> x[2:]
[2, 3, 4, 5, 6, 7, 8, 9]
>>> x[2:2:7]
[]
>>> x[2:5]='abc' #通過(guò)分片替換多個(gè)對(duì)象
>>> x
[0, 1, 'a', 'b', 'c', 5, 6, 7, 8, 9]
#---------------------
#矩陣
>>> x=[[1,2,3],[4,5,6],[7,8,9]]
>>> x[0]
[1, 2, 3]
>>> x[0][0]
1
常用方法:
#添加單個(gè)對(duì)象:append()
>>> x=[1,2,3]
>>> x.append('abc')
>>> x
[1, 2, 3, 'abc']
#---------------------
#添加多個(gè)對(duì)象:extend()
>>> x=[1,2,3]
>>> x.extend(['a','b']) #添加多個(gè)對(duì)象時(shí),需要以列表為整體
>>> x
[1, 2, 3, 'a', 'b']
>>> x.extend('abc') #使用extend添加單個(gè)字符串時(shí),會(huì)被拆分
>>> x
[1, 2, 3, 'a', 'b', 'a', 'b', 'c']
#---------------------
#插入對(duì)象:insert()
>>> x=[1,2,3]
>>> x.insert(1,'abc') #1表示插入的偏移位置
>>> x
[1, 'abc', 2, 3]
#---------------------
#按值刪除對(duì)象:remove(),若有重復(fù),則刪除第一個(gè)
>>> x=[1,2,2,3]
>>> x.remove(2)
>>> x
[1, 2, 3]
#---------------------
#按位置刪除:pop()
>>> x=[1,2,3,4,5,6]
>>> x.pop() #刪除并返回最后一個(gè)對(duì)象
6
>>> x
[1, 2, 3, 4, 5]
>>> x.pop(1) #刪除并返回偏移量為1的對(duì)象
2
>>> x
[1, 3, 4, 5]
#---------------------
#用del語(yǔ)句刪除
>>> x=[1,2,3,4,5,6]
>>> del x[0] #刪除第一個(gè)對(duì)象
>>> x
[2, 3, 4, 5, 6]
>>> del x[2:4] #刪除偏移量為2、3的對(duì)象
>>> x
[2, 3, 6]
#---------------------
#刪除全部對(duì)象:clear()
>>> x=[1,2,3]
>>> x.clear()
>>> x
#---------------------
#復(fù)制列表:copy(),只是復(fù)制一維列表(沒(méi)有真正復(fù)制到里面的列表內(nèi)容)
>>> x=[1,2,3]
>>> y=x.copy()
>>> y
[1, 2, 3]
#深復(fù)制:追根溯源(要導(dǎo)入copy模塊)copy.deepcopy()
#---------------------
#列表排序:sort()(全為數(shù)字則從小到大排序,全為字符串則按字典書(shū)序排序,若包含多種類型則出錯(cuò))
>>> x=[1,3,5,8,2] #對(duì)全為數(shù)字列表排序
>>> x.sort()
>>> x
[1, 2, 3, 5, 8]
>>> x=['b','a','ddd','abc','AB','BA'] #對(duì)全為字符串列表排序
>>> x.sort()
>>> x
['AB', 'BA', 'a', 'abc', 'b', 'ddd'] #對(duì)全為字符串列表排序
>>> x=['abc','123']
>>> x.sort()
>>> x
['123', 'abc']
>>> x=[1,2,3,'abc'] #對(duì)混合類型列表排序時(shí),報(bào)錯(cuò)
>>> x.sort()
Traceback (most recent call last):
File "<pyshell#90>", line 1, in <module>
x.sort()
TypeError: '<' not supported between instances of 'str' and 'int'
#---------------------
#反轉(zhuǎn)對(duì)象順序:reverse()
>>> x=[1,2,3]
>>> x.reverse()
>>> x
[3, 2, 1]
元組
有序、不可改變、不可添加也不可刪除
創(chuàng)建元組:
#創(chuàng)建空的元組對(duì)象
>>> ()
()
>>> tuple()
()
#創(chuàng)建一個(gè)對(duì)象的元組(逗號(hào)不能少)
>>> (1,)
(1,)
#用字符串創(chuàng)建元組,可迭代對(duì)象均可用于創(chuàng)建元組
>>> tuple('hello')
('h', 'e', 'l', 'l', 'o')
#用解析結(jié)構(gòu)創(chuàng)建元組
>>> tuple(i*2 for i in range(5))
(0, 2, 4, 6, 8)
基本操作:
#求長(zhǎng)度
>>> len((0,1,2,3,4,5))
6
#---------------------
#合并
>>> (1,2)+('a','b')+(2.45,)
(1, 2, 'a', 'b', 2.45)
#---------------------
#重復(fù)
>>> (1,2)*3
(1, 2, 1, 2, 1, 2)
#---------------------
#迭代
>>> for x in (1,2.5,'hello',[1,2]):print(x)
1
2.5
hello
[1, 2]
#---------------------
#關(guān)系判斷
>>> 'a' in ('a','b','c')
True
#---------------------
#索引和分片
>>> x=tuple(range(10))
>>> x
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
>>> x[1]
1
>>> x[-1]
9
>>> x[3:6]
(3, 4, 5)
>>> x[1:7:2]
(1, 3, 5)
#---------------------
#矩陣
>>> x=((1,2,3),(4,5,6),(7,8,9))
>>> len(x)
3
>>> x[0]
(1, 2, 3)
>>> x[0][1]
2
常用方法:
#返回指定值在元組出現(xiàn)的次數(shù):count()
>>> x=(1,2)*3
>>> x
(1, 2, 1, 2, 1, 2)
>>> x.count(1)
3
>>> x.count(3)
0
#---------------------
#查找元組中指定的值:index()
>>> x=(1,2,3)*3
>>> x
(1, 2, 3, 1, 2, 3, 1, 2, 3)
>>> x.index(2) #默認(rèn)查找全部元組
1
>>> x.index(2,2) #從偏移量2到元組末尾中查找
4
>>> x.index(2,2,7) #在范圍[2:7]內(nèi)查找
4
>>> x.index(5) #若元組找不到指定值,報(bào)錯(cuò)
Traceback (most recent call last):
File "<pyshell#68>", line 1, in <module>
x.index(5)
ValueError: tuple.index(x): x not in tuple
字典
無(wú)序、長(zhǎng)度不可變、可添加也可刪除
創(chuàng)建字典:
#創(chuàng)建空字典
>>> {}
{}
>>> dict()
{}
#創(chuàng)建字典
>>> {'name':'jayss','age':20}
{'name': 'jayss', 'age': 20}
基本操作:
#求長(zhǎng)度
>>> x={'name':'jayss','age':20}
>>> len(x)
2
#---------------------
#關(guān)系判斷(判斷的是鍵)
>>> x={'name':'jayss','age':20}
>>> 'name' in x
True
>>> 'sex' in x
False
>>> 'jayss' in x
False
#---------------------
#索引
#通過(guò)鍵來(lái)索引映射的值
>>> x={'name':{'jayss':'male','ella':'female'},'age':20}
>>> x['name']
{'jayss': 'male', 'ella': 'female'}
>>> x['name']['jayss']
'male'
#通過(guò)索引修改映射值
>>> x=dict(name='jayss',age=20)
>>> x
{'name': 'jayss', 'age': 20}
>>> x['age']=21
>>> x
{'name': 'jayss', 'age': 21}
#通過(guò)索引刪除鍵值對(duì)
>>> x={'name':'jayss','age':20}
>>> del x['age']
>>> x
{'name': 'jayss'}
常用方法:
#刪除全部字典對(duì)象:clear()
>>> x={'name':'jayss','age':20}
>>> x.clear()
>>> x
{}
#---------------------
#復(fù)制字典對(duì)象:copy()
#例一
>>> x={'name':'jayss','age':20}
>>> y = x
>>> x,y
({'name': 'jayss', 'age': 20}, {'name': 'jayss', 'age': 20})
>>> y['name']='ella'
>>> x,y
({'name': 'ella', 'age': 20}, {'name': 'ella', 'age': 20})
>>> y is x
True
#例二
>>> x={'name':'jayss','age':20}
>>> y=x.copy()
>>> x,y
({'name': 'jayss', 'age': 20}, {'name': 'jayss', 'age': 20})
>>> y['name']='ella'
>>> x,y
({'name': 'jayss', 'age': 20}, {'name': 'ella', 'age': 20})
>>> y is x
False
#---------------------
#返回鍵key映射的值:get(key[,default]),如果鍵值不存在則返回空值,可用default指定不存在的返回值
>>> x={'name':'jayss','age':20}
>>> x.get('name')
'jayss'
>>> x.get('sex')
>>> x.get('sex','xxx')
'xxx'
#---------------------
#刪除鍵并返回映射值:pop(key[,default]),若鍵值不存在則返回default,未指定default則會(huì)報(bào)錯(cuò)
>>> x={'name':'jayss','age':20}
>>> x.pop('age') #刪除鍵并返回映射值
20
>>> x
{'name': 'jayss'}
>>> x.pop('sex','xxx') #刪除不存在的鍵
'xxx'
>>> x.pop('sex') #刪除不存在的鍵,未指定default參數(shù),報(bào)錯(cuò)
Traceback (most recent call last):
File "<pyshell#39>", line 1, in <module>
x.pop('sex')
KeyError: 'sex'
#---------------------
#刪除并返回鍵值對(duì)元組:popitem(),空字典調(diào)用該方法會(huì)產(chǎn)生KeyError錯(cuò)誤
>>> x={'name':'jayss','age':20}
>>> x.popitem() #刪除并返回鍵值對(duì)元組
('age', 20)
>>> x #x中剩余一個(gè)鍵值對(duì)
{'name': 'jayss'}
>>> x.popitem() #刪除并返回鍵值對(duì)元組
('name', 'jayss')
>>> x
{}
>>> x.popitem() #刪除為空的字典,報(bào)錯(cuò)
Traceback (most recent call last):
File "<pyshell#38>", line 1, in <module>
x.popitem()
KeyError: 'popitem(): dictionary is empty'
#---------------------
#返回映射值或者為字典添加鍵值對(duì):setdefault(key[,default])
>>> x={'name':'jayss','age':20}
>>> x.setdefault('name') #返回指定鍵的映射值
'jayss'
>>> x.setdefault('sex') #鍵不存在,為字典添加鍵值對(duì),映射值默認(rèn)為None
>>> x
{'name': 'jayss', 'age': 20, 'sex': None}
>>> x.setdefault('hobby','sleep') #添加鍵值對(duì)
'sleep'
>>> x
{'name': 'jayss', 'age': 20, 'sex': None, 'hobby': 'sleep'}
#---------------------
#為字典添加鍵值對(duì):update(other)
>>> x={'name':'jayss','age':20}
>>> x.update({'sex':'male'}) #添加鍵值對(duì)
>>> x
{'name': 'jayss', 'age': 20, 'sex': 'male'}
>>> x.update(name='Mike') #修改映射值
>>> x
{'name': 'Mike', 'age': 20, 'sex': 'male'}
>>> x.update(hobby='sleep') #添加鍵值對(duì)
>>> x
{'name': 'Mike', 'age': 20, 'sex': 'male', 'hobby': 'sleep'}
字典視圖:
#返回鍵值對(duì)視圖:items()
>>> x={'name':'jayss','age':20}
>>> y=x.items() #返回鍵值對(duì)視圖
>>> y
dict_items([('name', 'jayss'), ('age', 20)])
>>> for i in y:print(i) #迭代鍵值對(duì)視圖
('name', 'jayss')
('age', 20)
#---------------------
#返回字典中所有鍵的視圖:keys()
>>> x={'name':'jayss','age':20}
>>> y=x.keys()
>>> y
dict_keys(['name', 'age'])
#---------------------
#返回字典中所有值的視圖:values()
>>> x={'name':'jayss','age':20}
>>> y=x.values()
>>> y
dict_values(['jayss', 20])
#---------------------
#鍵視圖的集合操作
>>> x={'name':'jayss','age':20} #創(chuàng)建x字典
>>> kx=x.keys() #返回x的鍵視圖
>>> kx
dict_keys(['name', 'age'])
>>> y={'name':'ella','age':19} #創(chuàng)建y字典
>>> ky=y.keys() #返回y的鍵視圖
>>> ky
dict_keys(['name', 'age'])
>>> kx-ky #求差集
set()
>>> kx|ky #求并集
{'age', 'name'}
>>> kx&ky #求交集
{'age', 'name'}
>>> kx^ky #求對(duì)稱差集
set()
歡迎學(xué)術(shù)交流
WeChat......