Python基础学习列表+元组+字典+集合
目录
一、列表二、元组三、字典四、集合五、总节前言:
这一章的知识紧接上一章,零基础的小伙伴可以从上一章学起来。当然,你也可以收藏起来慢慢学习,学习是不可操之过急的啦…
一、列表
print("-------------创建列表-------------"); list1 = ["JAVA", "Hello", "Python", "VS", 1, 2, 3] print(list1) list2 = list("Python") print(list2) list3 = [] print(list3) print("-------------访问列表中的值-------------"); print("list1[0]: ", list1[0]) print("list2[1:5]: ", list2[1:5]) print("-------------列表函数-------------"); list1.append("XYZ") #向 list1 增加"XYZ"对象 print(list1.count("Python")) #返回"Python"出现次数 list1.extend(list2) #将 list2 加到 list1 后面 print(list1) list1.remove("XYZ") #删除对象"XYZ" print(list1.pop()) # 删除列表的最后位置上的对象并返回 print(list1) D:\工作空间\Python\venv\Scripts\python.exe D:/工作空间/Python/main.py -------------创建列表------------- ["JAVA", "Hello", "Python", "VS", 1, 2, 3] ["P", "y", "t", "h", "o", "n"] [] -------------访问列表中的值------------- list1[0]: JAVA list2[1:5]: ["y", "t", "h", "o"] -------------列表函数------------- 1 ["JAVA", "Hello", "Python", "VS", 1, 2, 3, "XYZ", "P", "y", "t", "h", "o", "n"] n ["JAVA", "Hello", "Python", "VS", 1, 2, 3, "P", "y", "t", "h", "o"] 进程已结束,退出代码0
二、元组
print("-------------创建访问元祖-------------"); #coding=utf-8 tuple = ( "Java", 786 , 2.23, "john", 70.2 ) tinytuple = (123, "john") print(tuple) # 输出完整元组 print(tuple[0])# 输出元组的第一个元素 print(tuple[1:3]) # 输出第二个至第三个的元素 print(tuple[2:])# 输出从第三个开始至列表末尾的所有元素 print(tinytuple * 2) # 输出元组两次 print(tuple + tinytuple) # 打印组合的元组 D:\工作空间\Python\venv\Scripts\python.exe D:/工作空间/Python/main.py -------------创建访问元祖------------- ("Java", 786, 2.23, "john", 70.2) Java (786, 2.23) (2.23, "john", 70.2) (123, "john", 123, "john") ("Java", 786, 2.23, "john", 70.2, 123, "john") 进程已结束,退出代码0
三、字典
print("-------------创建字典-------------"); d1 = {} # 空字典 d2={"id":10,"tel":123456,"name":"小明"} print(d1) print(d2) print("-------------访问字典-------------"); dict2 = {"name": "小明","id":1, "dept": "计算机"} print(dict2["dept"]) print(dict2["name"]) print("-------------修改添加字典-------------"); dict1 = {"Name":"小明", "Age":19, "major":"计算机"}; dict1["Age"] = 18; # 字典中有"Age"键,更新现有元素 dict1["college"] = "Tech"; # 字典中无"college"键,执行添加操作 print("dict1["Age"]: ",dict1["Age"]) print("dict1["college"]: ",dict1["college"]) print("-------------删除字典-------------"); dict1={"stu_name":"小明","stu_id":1,"stu_age":24} del dict1["stu_id"] # 删除键为"stu_id"的键值对 print(dict1) dict1.clear() # 删除所有键值对 print(dict1) D:\工作空间\Python\venv\Scripts\python.exe D:/工作空间/Python/main.py -------------创建字典------------- {} {"id": 10, "tel": 123456, "name": "小明"} -------------访问字典------------- 计算机 小明 -------------修改添加字典------------- dict1["Age"]: 18 dict1["college"]: Tech -------------删除字典------------- {"stu_name": "小明", "stu_age": 24} {} 进程已结束,退出代码0
四、集合
print("-------------创建集合-------------"); s = set() # 空集 print(s) print(type(s)) s = {1,2,3} # 直接写入集合元素 print(type(s)) s=set(["ABC","XYZ","xyz","123","1",1,1.0]) print(s) s=set(i for i in range(10)) print(s) s=frozenset("Python 3.3.3") print(s) s= dict((i,0) for i in {1, "ABC", "XYZ", "xyz", "1", "123"}) print(s) s= dict((i,0) for i in frozenset({"n", "o", "h", " ", ".", "y", "t", "P", "3"})) print(s) print("-------------访问集合-------------"); s = set(["A", "B", "C", "D"]) # s = {"A", "B", "C", "D"} print("A" in s) print("a" not in s) for i in s: print(i,end="\t") print("-------------更新集合-------------"); s = set(["A", "B", "C", "D"]) s = s|set("Python") # 使用操作符"|" print(s) s.add("ABC") # add()方法 print(s) s.remove("ABC") # remove()方法 s.update("JAVAEF") # update()方法 print(s)
D:\工作空间\Python\venv\Scripts\python.exe D:/工作空间/Python/main.py -------------创建集合------------- set(){1, "1", "xyz", "XYZ", "123", "ABC"} {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} frozenset({"t", "o", "y", "n", "3", ".", "h", " ", "P"}) {1: 0, "1": 0, "xyz": 0, "XYZ": 0, "123": 0, "ABC": 0} {"t": 0, "o": 0, "n": 0, ".": 0, "y": 0, "3": 0, "h": 0, " ": 0, "P": 0} -------------访问集合------------- True True B D C A -------------更新集合------------- {"B", "t", "o", "y", "n", "D", "C", "h", "P", "A"} {"B", "t", "o", "ABC", "y", "n", "D", "C", "h", "P", "A"} {"B", "E", "t", "o", "y", "n", "J", "D", "C", "h", "F", "P", "V", "A"} 进程已结束,退出代码0 D:\工作空间\Python\venv\Scripts\python.exe D:/工作空间/Python/main.py -------------创建字典------------- {} {"id": 10, "tel": 123456, "name": "小明"} -------------访问字典------------- 计算机 小明 -------------修改添加字典------------- dict1["Age"]: 18 dict1["college"]: Tech -------------删除字典------------- {"stu_name": "小明", "stu_age": 24} {} 进程已结束,退出代码0
五、总节
今天的基础知识就到这里了,一次性分享太多大家也记不住多少
到此这篇关于Python基础学习列表+元组+字典+集合的文章就介绍到这了,更多相关Python基础内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
X 关闭
X 关闭