Part1_3 python字符串,列表,元组,字典
生活随笔
收集整理的這篇文章主要介紹了
Part1_3 python字符串,列表,元组,字典
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
字符串
列表
元組
字典
字符串
print(word) print(sentence) print(paragraph)my_str1 = "I'm a student" print(my_str1)my_str2 = 'I\'m a student' #與上面雙引號的區別:當單引號中間出現單引號是要用\轉義,雙引號同理 print(my_str2)str = "chengdu" print(str) print(str[0:5]) #[起始位置:結束位置:步長] print(str[0:6:2])print(str[5:])print(str + "nihao") print("hello\n chengdu") #使用反斜杠,轉義字符 print(r"hello\n chengdu") #在雙引號前面加r,不轉義字符串常見操作
列表
namelist = ["小張","小王","小李"]name = [] #定義一個空的列表namelist = ["小張","小王","小李"]testlist = [1,"測試"] #列表中可以存儲混合類型print(type(testlist[0])) print(testlist[1])print(namelist[0]) print(namelist[1]) print(namelist[2])for name in namelist:print(name)print(len(namelist))length = len(namelist)i = 0 while i < length:print(namelist[i])i += 1#增加:[append] 在末尾增加一個元素print("-----增加前,名單列表的數據-----") for name in namelist:print(name)nametemp = input("請輸入添加學生的姓名:") namelist.append(nametemp)print("-----增加前,名單列表的數據-----") for name in namelist:print(name)a = [1,2] b = [3,4] a.append(b) #將列表當成一個元素加入a中 print(a)a.extend(b) print(a) #將列表中的每個元素,逐一追加到a列表中#增: [insert]a = [0,1,2] a.insert(1,3) #第一個變量表示下標,第二個表示元素對象 print(a) #指定下標位置插入元素#刪 [del] [pop] movename = ["加勒比海盜","駭客帝國","第一滴血","速度與激情","指環王","指環王"]print("-----刪除前,電影列表的數據-----") for name in movename:print(name)del movename[2] #在指定位置刪除一個元素#movename.pop() #在末尾彈出最后一個元素movename.remove("指環王") #刪除列表中指定的元素,注意:如果元素有多個,則刪除找到的第一個元素print("-----刪除后,名單列表的數據-----") for name in movename:print(name)#改:print("-----修改前,名單列表的數據-----") for name in namelist:print(name)namelist[1] = "小紅 " #指定修改元素的下標print("-----修改后,名單列表的數據-----") for name in namelist:print(name)#查 [in , not in]findName = input("請輸入你要查找的學生姓名:")if findName in namelist:print("在學員名單中找到了相同的名字") else:print("沒有找到")myList = ["a","b","c","a","d"]print(myList.index("a",1,4)) #可以查找指定下標范圍的元素,并返回找到對應數據的下標print(myList.index("a",1,3)) #范圍區間,左閉右開,[1,3),找不到會報錯myList = ["a","b","c","a","d"]print(myList.count("c"))#排序a = [1,4,2,3] print(a) a.reverse() #將列表所有元素反轉 print(a)a.sort() #升序 print(a) a.sort(reverse = True) print(a)#schoolNames = [[],[],[]] #有三個元素得空列表,每個元素都是一個空列表schoolName = [["北京大學","清華大學"],["南方科技大學","深圳大學"],["河南理工大學","河南科技大學"]]print(schoolName[0][0])import randomoffices = [[],[],[]] names = ["A","B","C","D","G","E","F","H"]for name in names:index = random.randint(0,2)offices[index].append(name)i = 1 for office in offices:print("辦公室%d的人數為:%d"%(i,len(office)))i += 1for name in office :print("%s"%name,end="\t")print("\n")print("-"*20)元組
tup1 = () #創建空的元組 print(type(tup1))tup2 = (50) #不是元組了 tup3 = (50,) #元組類型,元組的標志不是括號,而是逗號 tup4 = (50,60,70)print(type(tup2)) print(type(tup3)) print(type(tup4))tup1 = ("abc","def",2323,333,444,555)print(tup1[0]) print(tup1[-1]) #訪問最后一個元素 print(tup1[1:5]) #左閉右開#增 (連接) tup1 = (12,34,56) tup2 = ("abc","efg")tup = tup1 + tup2print(tup)#刪 tup1 = (12,3,4) print(tup1)del tup1print("刪除后") print(tup1)#改#tup1 = (12,34,56) #tup1[0] = 10 #不允許修改,會報錯字典
#字典的定義 info = {"name":"jeffchen","age":18}#字典的訪問 print(info["name"]) print(info["age"])#訪問了不存在的鍵 #print(info["gender"]) #直接訪問不存在的鍵,會報錯print(info.get("gender")) #使用get方法,沒有找到對應的鍵,默認返回Noneprint(info.get("age","20")) print(info.get("gender","m")) #沒找到的時候可以設定默認值info = {"name":"jeffchen","age":18} #增 newID = input("請輸入新的學號:") info["id"] = newIDprint(info["id"])#刪 [del] [clear]info = {"name":"jeffchen","age":18}print("刪除前:%s"%info["name"])del info["name"]print("刪除后:%s"%info["name"]) #刪除了一個鍵值對,運行會報錯info = {"name":"jeffchen","age":18} print("刪除前:%s"%info["name"]) del info print("刪除前:%s"%info["name"]) #刪除整個字典會報錯info = {"name":"jeffchen","age":18} print("清空前:%s"%info)info.clear()print("清空后:%s"%info)#改info = {"name":"jeffchen","age":18} info["age"] = 20 print(info["age"])#查 info = {"id":1,"name":"jeffchen","age":18} print(info.keys()) #得到所有的鍵 print(info.values()) #得到所有的值 print(info.items()) #得到所有的項(列表)#遍歷所有的值 info = {"id":1,"name":"jeffchen","age":18} for key,value in info.items():print("key=%s,value=%s"%(key,value)) '''# 枚舉類型函數,同事拿到下標和內容 myList = ["a","b","c","d"] for i,x in enumerate(myList):print(i,x)總結
以上是生活随笔為你收集整理的Part1_3 python字符串,列表,元组,字典的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Anaconda中安装pytorch,并
- 下一篇: Part1_4 python函数、文件操