一、number數(shù)字類型
1、int整數(shù)型:
a =10
print(type(a))
>>><class 'int'>
2、算數(shù)運算:
+(加) -(減) *(乘) /(除) //(商取整) %(商取余) **(次冪)
a =10
b =8
print(a+b, a-b, a*b, a/b, a//b, a%b, a**b)
>>>18 2 80 1.25 1 2 100000000
3、比較運算:
==(等于) !=(不等于) >(大于) <(小于) >=(大于等于) <=(小于等于)
a =10
b =8
print(a == b, a != b, a > b, a < b, a >= b, a <= b)
>>>False True True False False False
4、賦值運算:
+=(a+=b,表示a=a+b)
-=(a-=b,表示a=a-b)
=(a=b,表示a=a*b)
/=(a/=b,表示a=a/b)
a+=b
print(a)
>>>18
a-=b
print(a)
>>>2
a*=b
print(a)
>>>80
a/=b
print(a)
>>>1.25
5、float浮點型(小數(shù)):
a =10.33
print(type(a))
>>><class 'float'>
float()函數(shù),將整數(shù)/字符串轉(zhuǎn)換為浮點型數(shù)據(jù),默認(rèn)保留一位小數(shù)
a =10
print(float(a))
>>>10.0
6、Boolean布爾型:
Ture,F(xiàn)alse
a = 1==1
b = 1!=1
print(a, b)
>>>True False
7、保留多少位小數(shù):round(x,2)
a =10.1234
print(round(a,2))
>>>10.12
8、取絕對值:abs(x)
print(abs(-1))
>>>1
9、取整:
向下取整:math.floor(x),向上取整:math.celi(x) , 注:需導(dǎo)入math模塊
import math
a =10.51
print(math.floor(a), math.ceil(a))
>>>10 11
10、隨機返回數(shù)值:
random.randint(0,10) ,返回的是0-10之間的隨機整數(shù) , 注:需要導(dǎo)入random模塊
import random
print(random.randint(1,10))
二、string字符串類型:
1、字符串標(biāo)識:
使用單引號/雙引號/三引號括起來的內(nèi)容
a =10
b ='10'
c ="10"
d ="""10"""
print(type(a),type(b),type(c),type(d))
>>><class 'int'> <class 'str'> <class 'str'> <class 'str'>
type()函數(shù)可以查看數(shù)據(jù)類型
2、切片取值:
將字符串內(nèi)的元素進行提取,表達式是str[開始值:結(jié)束值:步長]
開始值:從0開始,結(jié)束值:取值范圍的結(jié)束點,步長:決定取值方向(正數(shù)為從左至右,復(fù)數(shù)為從右至左)、取值跨度。
步長可省略,默認(rèn)步長為1。
切片的取值范圍是左閉右開(取頭不取尾),如a[0:3:1]:字符串a(chǎn)從t開始至t結(jié)束,向右取每個字符;a[11:0:-2]:從o開始至t結(jié)束,向左每隔一個元素取值
a ='test123hello'
print(a[0:3:1], a[11:0:-2])
>>>tes olh2te
切片常用表達式:取最后一個[-1],取除了最后一個[:-1],取全部[:],隔一個取一個[::2],倒序排列[::-1]
a ='test123hello'
print(a[-1], a[:-1], a[:], a[::2], a[::-1])
>>>o test123hell test123hello ts13el olleh321tset
2、字符串路徑轉(zhuǎn)義
在python中\(zhòng)n表示換行,但有時會遇到路徑中含有\(zhòng)n情況,如'D:\levi\new\python',此時'\n'就表示為換行的意思,需要轉(zhuǎn)義進行使用,r'D:\levi\new\python'
a ='D:\levi\new\python'
print(a)
>>>D:\levi
>>>ew\python
a =r'D:\levi\new\python'
print(a)
>>>D:\levi\new\python
3、替換字符串內(nèi)容:
.replace("a","b"),將a替換成b
a ='test123hello'
print(a.replace('123','456'))
>>>test456hello
4、字符串的統(tǒng)計:
len(a)、max(a)、min(a),長度、最大值、最小值(python中字符大小排序規(guī)則為a-z>0-9)
a ='test123hello'
print(len(a),max(a),min(a))
>>>12 t 1
5、字符串索引查找:
.find('x',0,len(a)) x表示字符串中查找的元素,0表示起始索引,len(a)表示結(jié)束索引;結(jié)果返回的是索引位置,沒有這個元素時返回的是-1 注:此函數(shù)只能在字符串中查找
a ='test123hello'
print(a.find('h',0,len(a)), a.find('z',0,len(a)))
>>>7 -1
6、字符串大小寫轉(zhuǎn)換:
.lower()轉(zhuǎn)換小寫,.upper()轉(zhuǎn)換小寫,.capitalize()首字母大寫
a ='Test123Hello'
print(a.lower(), a.upper(), a.capitalize())
>>>test123hello TEST123HELLO Test123hello
7、字符串的拼接:
' '.join(x),將字符串x中的元素拼接起來 注:.join只能在字符串中使用,所以拼接的時候前面需要帶引號
a ='Test123Hello'
b = ['test','123','hello']
print('-'.join(a))print(' '.join(b))
>>>T-e-s-t-1-2-3-H-e-l-l-o
>>>test123hello
8、字符串去掉頭尾:
.strip(),方法用于移除字符串頭尾指定的字符(默認(rèn)為空格或換行符)或字符序列 注:該方法只能刪除開頭或是結(jié)尾的字符,不能刪除中間部分的字符
a =' Test123Hello\n'
print(a.strip())
>>>Test123Hello
9、字符串的運算:
拼接+,復(fù)制*
a ='Test123Hello'
print('拼接:'+a)
>>>拼接:Test123Hello
a ='Test123Hello'
print(a*2)
>>>Test123HelloTest123Hello
10、返回字符串的表達式結(jié)果:
eval(x),返回傳入字符串的表達式的結(jié)果;將字符串a(chǎn)內(nèi)的參數(shù)進行運算,并返回結(jié)果
a ='10'+'*'+'20'
print(a,eval(a))
>>>10*20 200
11、分隔符:
split(‘‘,x),’‘表示以什么進行分割,x表示分割次數(shù);將字符串中的數(shù)據(jù)通過指定的分隔符號進行拆分,返回列表
a ='www.baidu.com'
print(a.split('.'))
>>>['www','baidu','com']
print(a.split('.',1))
>>>['www','baidu.com']
在日常使用中,有時會遇到采用舍位法保留兩位小數(shù),如1.559只保留1.55,常規(guī)保留兩位小數(shù)會是1.56,如果需要保留1.55可以使用.split('.',1)分隔符,將'.'兩邊的數(shù)分割出來后在進行拼接
a =1.559
x =str(a).split('.')[0] # 取小數(shù)點左邊的數(shù)字
y =str(a).split('.')[1][:2] # 取小數(shù)點右邊的數(shù)字的前兩位
print(round(a,2))
>>>1.56
print(x +'.'+ y)
>>>1.55
12、判斷是否為數(shù)字:
str.isdigit(),判斷字符串中是否只包含數(shù)字
a ='123'
b ='test123'
print(a.isdigit(), b.isdigit())
>>>True False
三、tuple元組:
1、元組定義
用來存儲一串?dāng)?shù)據(jù),用()標(biāo)識,不可對元組內(nèi)數(shù)據(jù)進行添加、修改、刪除等操作;元組內(nèi)數(shù)據(jù)可以是多個,數(shù)據(jù)類型可以為任意類型;只有一個元素的元組為了保證為元組,可以在括號內(nèi)加個逗號(1,),不加逗號會變成數(shù)據(jù)本身的類型;元組內(nèi)取值同樣可以采用切片的方式取值
2、元組內(nèi)元素更新:
可以根據(jù)拼接的方式來更新元組內(nèi)的元素,將不需要更新的部分切出來+重新定義需要更新部分的變量
tuple1 = ('張三','李四')
tuple2 = ('王二',)
tuple3 = tuple1 + tuple2
print(tuple3)
>>>('張三','李四','王二')
3、元組內(nèi)元素不可刪除:
元組內(nèi)元素不可以刪除,可以刪除整個元組對象:del 元組名
4、查看元素的索引位置:
x.index(y,0,-1),查找tuple x內(nèi)元素y的位置,返回的是索引
tuple1 = ('張三','李四')
print(tuple1.index('張三'))
>>>0
總結(jié)
不可變數(shù)據(jù)類型為:數(shù)字類型、字符串類型、元組類型;此篇內(nèi)容主要講解不可變數(shù)據(jù)類型的常用操作,各知識點需要自己不斷理解,反復(fù)練習(xí),在實際運用中才能得心應(yīng)手