Python字符串,元祖,列表,字典
可以用引号来创建字符串,python不支持单字符类型,单字符在python中也作为字符串使用;
python字符串可以修改,拼接;
#字符串创建和访问
var1 = 'Xiaobai'
var2 = "XiaobaiYa"
print("var1[0]",var1[0])
print("var2[1:5]",var2[1:5])
print(var1 + var2)
元祖(圆括号)
元祖由不同的元素组成,每个元素可以存储不同类型的数据,如字符串,数字甚至元祖;
元祖创建后不能再做任何修改,添加或者删除任何元素;
如果创建的元祖只包含一个元素,不要忘记逗号;
元祖的访问通过索引访问,索引就是下标:
#元祖的创建
tuple = (元素1,元素2,...)
tuple = ("apple","banana","grape","orange")
tuple1 = () #空元祖
tuple2 = ("apple",) #只包含一个元素的元祖
print(tuple[1])
print(tuple[-1]) #访问最后一个元素
print(tuple[0:2])
print(tuple[2:-2])
fruit1 = ("apple","banana") #复合元祖
fruit2 = ("grape","orange")
fruit = (fruit1,fruit2)
print(fruit[0][1])
print(fruit[1][1])
print(fruit[1][2]) #错误
'''元祖的打包和解包(了解)
创建元祖就相当于打包;
解包可以把元祖中的各个元素分别赋值给多个变量
'''
a,b,c,d = tuple
print(a,b,c,d)
#元祖的遍历
tuple = (("apple","banana"),("grape","orange"),("watermelon",),("grapefruit",))
for i in range(len(tuple)):
print("tuple[%d] : "%i," ",)
for j in range(len(tuple[i])):
print(tuple[i][j]," ")
print()
for i in tuple:
for j in i:
print(j)
列表(方括号)
列表也由一组元素组成,可以实现添加,删除,修改和查找操作;
添加可以调用appen();删除可以调用remove();
list = ["Xiaobai","baoxin",123,"BAOXIN"]
print(list)
print(list[1])
list.append("Hello")
list.insert(1,"World") #在指定元素下标位置插入
print(list)
list.remove(123)
list.remove("Hello")
print(list)
print(list.pop()) #打印列表中弹出的元素,即最后一个元素
print(list)
list1 = ["Xiaobai","Xiaobao"]
list2 = ["Baoxin","BaoBaoBao"]
list1.extend(list2) #list1连接list2
print(list1)
list3 = list1 + list2 #用+号连接list1和list2 并且赋值给list3
print(list3)
print(list3 * 2) #打印2次list3
#列表的查找,排序,反转
list = ["Xiaobai","baoxin",123,"BAOXIN"]
print(list.index("baoxin")) #打印baoxin的索引
print("BAOXIN" in list) #判断BAOXIN是不是在列表,返回True
list.sort() #元素按照首字母升序排列
list.reserve() #反转元素排列顺序
字典(花括号)
字典由一系列的“键-值(key-value)”,字典是“键-值“对组成的集合,字典中的值通过键来引用;
字典的键是区分大小写的;
创建字典时可以用数字作为索引;
字典通过key值获取对于的value值,如果访问的key值不在字典的key列表中,字典会自动添加一条新的映射;
字典没有remove操作,元素删除通过del()实现;
#字典
dictionary = {key1 : value1,key2 : value2,...}
dictionary = {}
dict = {"a":"apple","b":"Baoxin","g":"grape","d":"Doubi"}
print(dict)
print(dict["b"])
dict1 = {1 : "apple",2 : "Baoxin",3 : "grape", 4 :"Doubi"}
print(dict1)
print(dict1[2])
#字典的添加,删除,修改
dict = {"a":"apple","b":"Baoxin","g":"grape","d":"Doubi"}
dict["x"] = "Xiaobai" #添加
del(dict["a"]) #删除字典中键为a的元素
dict["g"] = "grapefruit" #修改字典中键为g的值
print(dict.pop("b")) #弹出字典中键为b的元素
print(dict)
dict.clear() #清除字典中所有元素
print(dict)
#字典的遍历
for i in dict:
print("dict[%s] = " %k,dict[k])
print(dict.items()) #使用items()遍历,返回若干个元祖组成的列表
for (k,v) in dict.items(): #k,v分别对应key和value
print("dict[%s] = " %k,v)
#混合型字典
dict = {"a":"apple","b":{"ba":"Baoxin","bo":"banana"},"g":["grape","Xiaobai"]}
print(dict["a"])
print(dict["a"][0])
print(dict["b"])
print(dict["b"]["ba"])
print(dict["g"][1])
#字典的方法
dict = {"a":"apple","b":"Baoxin","g":"grape","d":"Doubi"}
print(dict.keys()) #打印出字典的键,返回列表
print(dict.values())
print(dict)
print(dict.get("c","apple")) #使用get获取键为c的值,不存在就返回apple
print(sorted(dict.items(),key = lambda d:d[0])) #按照key排序,lambda可以创建匿名函数,后面会介绍
print(sorted(dict.items(),key = lambda d:d[1])) #按照value排序 |
|