Python數(shù)據(jù)類型最全知識總結(jié)
一、什么是數(shù)據(jù)類型
其實可以明白數(shù)據(jù)類型指的就是變量值的不同類型,姓名可能是一種數(shù)據(jù)類型、年齡可能是一種數(shù)據(jù)類型、愛好可能又是另一種數(shù)據(jù)類型
二、字符串類型
字符串類型所表示的數(shù)據(jù)是常量,它是一種不可變數(shù)據(jù)類型
如何表示
str = 'zhangsan' str = "zhangsan" str = '''zhangsan''' # 可以實現(xiàn)換行 str = """zhangsan""" # 可以實現(xiàn)換行 str = r'zhangsan\n' # r可以取消轉(zhuǎn)移字符的功能
相關(guān)方法

舉例:
find('str') # 找不到為-1
index('str') # 找不到報錯
isalnum('str') # 由字母或數(shù)字組成
isspace('str') # 由空格組成
split('str') # 分割之后是列表
rsplit('str',2) # 當分割次數(shù)有限制時與split有區(qū)別
partition('str') # 分割之后是三元組
capitalize() # 字符串首字母大寫
title() # 每個單詞首字母大寫
ljust() # 左對齊加空格
取值與切片操作
str = '123456789‘ # 下標取值 str[0] # 1 str[-1] # 9 # 切片語法 m[start, end, step] str[1:3] # 23 str[3:1] # 空 str[0:] # 123456789 str[:3] # 123 str[::] # 123456789 str[::-1] # 987654321 str[0:4:1] # 1234 str[0:4:2] # 13 str[0:4:0] # 報錯 str[0:4:-1] # 空 str[4:0:-1] # 5432 str[-3:-1] # 78 str[-1:-3] # 空 str[-3:-1:-1] # 空 str[-3:-1:1] # 78 str[-1:-3:-1] # 98 str[-1:-3:1] # 空
編碼與解碼操作
chr(65) # 編碼轉(zhuǎn)為字符
ord('我') # 字符轉(zhuǎn)為編碼
'str'.encode('utf-8') # 將字符串轉(zhuǎn)編碼
'str'.decode('utf-8') # 將字符串轉(zhuǎn)解碼
格式化輸出操作
普通格式化
%s(字符串)、 %d(整形)、 %f(浮點型)、 %%(%)、 %x(十六進制)、 %X(十六進制)
name = 'zhangsan'
age = 18
print('姓名:', name, ',年齡:', age, sep='')
print('姓名:%s,年齡:%d' %(name,age))
————————————————
版權(quán)聲明:本文為CSDN博主「ProChick」的原創(chuàng)文章,遵循CC 4.0 BY-SA版權(quán)協(xié)議,轉(zhuǎn)載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/qq_45747519/article/details/117379242
format格式化
# 默認
print('姓名{},年齡{}'.format('張三',18))
# 下標賦值
print('姓名{1},年齡{0}'.format(18,'張三'))
# 變量名賦值
print('姓名{name},年齡{age}'.format(name='zhangsan',age=18))
# 列表賦值
list = ['zhangsan',18]
print('姓名{},年齡{}'.format(*list))
# 字典賦值
dict = {"name":'zhangsan',"age":18}
print('姓名{name},年齡{age}'.format(**dict))
三、列表類型
列表類型所代表的數(shù)據(jù)元素有序且可以重復(fù)、可以修改
如何表示
mylist = ['張三',18]
mylist = list( ('zhangsan',18) ) # 將可迭代對象轉(zhuǎn)化為列表
相關(guān)方法
添加元素
list = [1,2,3] # 追加 list.append(4) # [1,2,3,4] # 插入 list.insert(0,0) # [0,1,2,3,4]
修改元素
list = [1,2,3] # 修改指定位置元素 list[0] = 0 # [0,2,3] list[2] = 0 # [0,2,0]
刪除元素
list = [1,2,3,4,5,6] # 刪除最后一個 list.pop() # [1,2,3,4,5] # 刪除指定位置 list.pop(0) # [2,3,4,5] # 刪除指定元素 list.remove(2) # [3,4,5] # 清空 list.clear() # []
查詢元素
list = [1, 2, 3, 2, 1] # 查找元素位置 list.index(1) # 0 # 查找元素個數(shù) list.count(1) # 2
合并列表
list1 = [1,2,3] list2 = [4,5,6] # 合并 list1.extend(list2) # [1,2,3,4,5,6] print(list1+list2) # [1,2,3,4,5,6]
排序
list = [2, 3, 1]
# 正序
list.sort() # [1,2,3]
# 產(chǎn)生新對象并正序
new_list = sorted(list) # [1,2,3]
# 倒序
list.sort(reverse=True) # [3,2,1]
# 倒序
list.reverse() # [3,2,1]
# 自定義排序規(guī)則(一般用于字典類型的比較)
list = [
{'name':'zhangsan',age:18},
{'name':'lisi',age:20},
{'name':'wangwu',age:19}
]
list.sort(key = lambda item : item['age'])
拷貝
list = [1, 2, 3] # 是淺拷貝 new_list = list.copy() # [1, 2, 3]
嵌套
# 相當于二維數(shù)組 list = [[1,2],[3,4],[5,6]]
推導式
list = [i for i in range(1,3)] # [1,2] list = [(i,j) for i in range(1,3) for j in range(1)] # [(1,0),(2,0)]
四、元組類型
元組類型所表示的數(shù)據(jù)元素有序且可以重復(fù),但不可以修改
如何表示
# 表示多個元素 tuple = (1,2,'3') # 表示1個元素 tuple = (True,)
相關(guān)方法
查詢元素
tuple = (1,True,'3',True) tuple.index(0) # 1 tuple.count(True) # 2
合并
tuple1 = (1,2) tuple2 = (True,False) print(tuple1+tuple2) # (1,2,True,False)
五、字典類型
字典類型所表示的數(shù)據(jù)元素無序,Key不可以重復(fù)(只能是不可變數(shù)據(jù)類型),Value可以修改(可以為任意數(shù)據(jù)類型)
如何表示
student = {"name":'zhangsan',"age":20}
相關(guān)方法
查詢元素
student = {"name":'zhangsan',"age":20}
print(student["age"]) # 20
print(student["birth"]) # 報錯
print(student.get("age")) # 20
print(student.get("birth")) # None
print(student.get("birth",'2000-10-10')) # 2000-10-10d
# 獲取所有Key
print(student.keys()) # ['name','age']
# 獲取所有Value
print(student.values()) # ['zhangsan',20]
# 獲取所有Key-Value
print(student.items()) # [('name':'zhangsan'),('age':20)]
添加、修改元素
student = {"name":'zhangsan',"age":20}
student["name"] = 'lisi'
print(student) # student = {"name":'lisi',"age":20}
student["sex"] = '男'
print(student) # student = {"name":'lisi',"age":20,"sex":'男'}
刪除元素
student = {"name":'zhangsan',"age":20}
# 刪除Key-Value,返回Value
result = student.pop("name")
print(student) # student = {"age":20}
print(result) # zhangsan
# 刪除Key-Value,返回Key-Value
result = student.popitem("name")
print(student) # student = {"age":20}
print(result) # ('name','zhangsan')
# 清空
student.clear()
print(result) # {}
合并
student = {"name":'zhangsan',"age":20}
student_add = {"sex":'男'}
student.update(student_add)
print(student) # {"name":'zhangsan',"age":20,"sex":'男'}
推導式
student = {"name":'zhangsan',"age":20}
student_reverse = {v:k for k,v in student.items()}
print(student_reverse) # {"zhangsan":'name',"20":age}
六、集合類型
集合類型所表示的數(shù)據(jù)元素無序且不可以重復(fù),不可以修改
如何表示
# 有元素的集合
set = {1,'我',True}
# 空集合
set()
相關(guān)方法
添加元素
set = {1,'我',True}
set.add('zhangsan')
print(set) # {1,'我',True,'zhangsan'}
刪除元素
set = {1,'我',True}
# 隨機刪除一個元素
set.pop()
print(set) # {'我',True}
# 刪除指定元素
set.remove('True')
print(set) # {1,'我'}
# 清空
set.clear()
print(set) # set()
合并
set = {1,'我',True}
# 兩個集合合并后產(chǎn)生新的集合
new_set = set.union( {True,False} )
print(new_set) # {1,'我',True,False}
# 將一個可迭代對象合并到原有集合中
set.update(['False'])
print(set) # {1,'我',True,'False'}
運算
set1 = {1,2,3}
set2 = {3,4,5}
# 差集
print(set1 - ste2) # {1,2}
print(set2 - ste1) # {4,5}
# 交集
print(set1 & ste2) # {3}
# 并集
print(set1 | ste2) # {1,2,3,4,5}
# 差并集
print(set1 ^ ste2) # {1,2,4,5}
七、五種數(shù)據(jù)類型所支持的運算符比較

八、數(shù)據(jù)的序列化和反序列化
序列化操作
將數(shù)據(jù)從內(nèi)存持久化保存到硬盤的過程
----(將數(shù)據(jù)轉(zhuǎn)化為字符串)----
import json
list = ['name','age','city']
file = open('test.txt','w',encoding='utf8')
file.write(repr(list)) # 第一種方式
file.write(str(list)) # 第二種方式
file.write(json.dumps(list)) # 第三種方式
json.dump(list,file) # 第四種方式
file.close()
----(將數(shù)據(jù)轉(zhuǎn)化為二進制)----
import pickle
list = ['name','age','city']
file = open('test.txt','wb',encoding='utf8')
file.write(pickle.dumps(list)) # 第一種方式
pickle.dump(list,file) # 第二種方式
file.close()
反序列化操作
將數(shù)據(jù)從硬盤加載到內(nèi)存的過程
# test.txt ["name","age","city"]
----(將字符串轉(zhuǎn)化為數(shù)據(jù))----
import json
file = open('test.txt','r',encoding='utf8')
list1 = json.load(file) # 第一種方式
print(list1) # ['name','age','city']
list2 = json.loads(file.read()) # 第二種方式
print(list2) # ['name','age','city']
file.close()
----(將二進制轉(zhuǎn)化為數(shù)據(jù))----
import pickle
file = open('test.txt','rb',encoding='utf8')
list1 = pickle.loads(file.read()) # 第一種方式
print(list1) # ['name','age','city']
list2 = pickle.load(file) # 第二種方式
print(list2) # ['name','age','city']
file.close()
到此這篇關(guān)于Python數(shù)據(jù)類型最全知識總結(jié)的文章就介紹到這了,更多相關(guān)Python數(shù)據(jù)類型內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python數(shù)據(jù)類型和常用操作
- Python常用數(shù)據(jù)類型之列表使用詳解
- Python基本數(shù)據(jù)類型及內(nèi)置方法
- Python數(shù)據(jù)類型及常用方法
- Python數(shù)據(jù)類型轉(zhuǎn)換匯總
- Python語言內(nèi)置數(shù)據(jù)類型
- python數(shù)據(jù)結(jié)構(gòu):數(shù)據(jù)類型
- 學好python基本數(shù)據(jù)類型
- Python基礎(chǔ)之數(shù)據(jù)類型相關(guān)知識總結(jié)
- Python基礎(chǔ)之數(shù)據(jù)類型詳解
- Python中的基本數(shù)據(jù)類型講解
相關(guān)文章
Python打包模塊wheel的使用方法與將python包發(fā)布到PyPI的方法詳解
這篇文章主要介紹了Python打包模塊wheel的使用方法與將python包發(fā)布到PyPI的方法詳解,需要的朋友可以參考下2020-02-02
Python數(shù)據(jù)分析之?Pandas?Dataframe合并和去重操作
這篇文章主要介紹了Python數(shù)據(jù)分析之?Pandas?Dataframe合并和去重操作,文章基于python的相關(guān)資料展開詳細的內(nèi)容介紹,需要的小伙伴可以參考一下2022-05-05
networkx庫繪制帶權(quán)圖給無權(quán)圖加權(quán)重輸出
這篇文章主要為大家介紹了Python?networkx庫繪制帶權(quán)圖給無權(quán)圖加權(quán)重并輸出權(quán)重的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05
在Apache服務(wù)器上同時運行多個Django程序的方法
這篇文章主要介紹了在Apache服務(wù)器上同時運行多個Django程序的方法,Django是Python各色高人氣web框架中最為著名的一個,需要的朋友可以參考下2015-07-07

