Python_基础_4
生活随笔
收集整理的這篇文章主要介紹了
Python_基础_4
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1元組
# 一個元組可以存儲多個數(shù)據(jù),但是元組內(nèi)的數(shù)據(jù)不可以修改 # 多個數(shù)據(jù)元組 t1 = (10, 20, 30) # 單個數(shù)據(jù)元組 t2 = (10,)# 查找 tuple1 = ('aa', 'bb', 'cc') print(tuple1[0]) # aa print(tuple1.index('aa')) # 0 找不到的話會報錯 print(tuple1.count('bb')) # 1 print(len(tuple1)) # 3# 修改 # 元組內(nèi)的直接數(shù)據(jù)如果修改會報錯 # 但是如果元組里面有列表,修改列表里面的數(shù)據(jù)則是可以的 tuple2 = (10, 20, ['aa', 'bb', 'cc'], 40, 50) tuple2[2][0] = 'aba' print(tuple2) # (10, 20, ['aba', 'bb', 'cc'], 40, 50)2字典
創(chuàng)建字典
# 字典里面的數(shù)據(jù)是以鍵值對形式出現(xiàn) # 有效字典 dict1 = {'name': 'Tom', 'age' : 20, 'gender' : '男'} # 空字典 dict2 = {} dict3 = dict();CRUD
# 增 如果key存在則修改,否則新增此鍵值對 dict1 = {'name': 'Tom', 'age' : 20, 'gender' : '男'} dict1['name'] = 'Rose' print(dict1) # {'name': 'Rose', 'age': 20, 'gender': '男'} dict1['id'] = 110 print(dict1) # {'name': 'Rose', 'age': 20, 'gender': '男', 'id': 110} # 刪 del()/del 刪除字典或刪除字典中指定鍵值對 dict1 = {'name': 'Tom', 'age' : 20, 'gender': '男'} del dict1['gender'] # del(dict1) 刪除字典 print(dict1) # {'name': 'Tom', 'age': 20}dict1.clear() print(dict1) # {} # 修改 dict1 = {'name': 'Tom', 'age' : 20, 'gender': '男'} dict1['name'] = 'gaopeng' print(dict1) dict1['id'] = 110 # 和增加一回事 # 查找 # 1 key值查找 dict1 = {'name': 'Tom', 'age' : 20, 'gender': '男'} print(dict1['name']) # Tom # print(dict1['id']) # 報錯# 2 get(key,默認(rèn)值) # 如果當(dāng)前查找的key不存在則返回第二個默認(rèn)值(默認(rèn)值),如果省略第二個參數(shù),則返回None print(dict1.get('name')) # print(dict1.get('id', '沒找到')) # 沒找到 print(dict1.get('id')) # None# 2 keys() print(dict1.keys()) # dict_keys(['name', 'age', 'gender'])# 3 values() print(dict1.values()) # dict_values(['Tom', 20, '男'])# 4 items() print(dict1.items()) # dict_items([('name', 'Tom'), ('age', 20), ('gender', '男')]) # 字典的循環(huán)遍歷 # 1 遍歷字典的key dict1 = {'name': 'Tom', 'age' : 20, 'gender': '男'} for key in dict1.keys():print(key, end=' ') else:print() # name age gender# 2 遍歷value for value in dict1.values():print(value, end=' ') else:print() # Tom 20 男# 3 遍歷字典的元素 for item in dict1.items():print(item, end=' ') else:print() # ('name', 'Tom') ('age', 20) ('gender', '男')# 4 遍歷字典的鍵值對(拆包) for key, value in dict1.items():print(f'{key} : {value}', end=' ') else:print() # name : Tom age : 20 gender : 男3集合
#創(chuàng)建集合 使用{}或者set() s1 = {10, 20, 30, 40, 50} s2 = set('abcdef') s3 = set() s4 = {} # 這個就不是集合了,是dict,字典 s1 = {10, 20} # 增加數(shù)據(jù) s1.add(100) s1.add(10) print(s1) # {100, 10, 20}# update() 追加的數(shù)據(jù)時序列 # s1.update(200) # 報錯 s1.update([200, 300]) s1.update('abc') print(s1) # {100, 200, 10, 300, 20, 'a', 'c', 'b'} # 刪除數(shù)據(jù) s1 = {10, 20, 39, 40}s1.remove(10) print(s1) # {40, 20, 39} # s1.remove(10) # print(s1) # 報錯# discard() 刪除集合中的指定數(shù)據(jù),如果數(shù)據(jù)不存在也不會報錯 s1.discard(20) print(s1) # {40, 39}s1.discard(20) print(s1) # {40, 39}# pop() 隨機(jī)刪除集合中的某個數(shù)據(jù),并返回這個數(shù)據(jù) # 查找數(shù)據(jù) # in not in s1 = {10, 20, 30, 40, 50}print(10 in s1) # True print(10 not in s1) # False總結(jié)
以上是生活随笔為你收集整理的Python_基础_4的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python_基础_3
- 下一篇: Python_基础_5