‘False’, ‘None’, ‘True’, ‘and’, ‘as’, ‘assert’, ‘async’, ‘await’, ‘break’, ‘class’, ‘continue’, ‘def’, ‘del’, ‘elif’, ‘else’, ‘except’, ‘finally’, ‘for’, ‘from’, ‘global’, ‘if’, ‘import’, ‘in’, ‘is’, ‘lambda’, ‘nonlocal’, ‘not’, ‘or’, ‘pass’, ‘raise’, ‘return’, ‘try’, ‘while’, ‘with’, ‘yield’
从左到右是:0、1、2、3、…、n,从右到左是:-1、-2、-3、-n
str = “helloword”
print(str[3:10:2])
输出:lwr
这里的3和10是指字符串中字符的小标索引,2是指步长,指隔1个输出一个,默认值为1。
animal.append("猴子")
print(animal)
输出结果:["老虎","狮子","大象","老鹰","猴子"]
animal.extend(["猴子","兔子"])
print(animal)
输出结果:["老虎","狮子","大象","老鹰","猴子","兔子"]
animal.inset(3,"猴子")
print(animal)
输出结果:["老虎","狮子","大象","猴子","老鹰"]
animal_set = animal + ["猴子","兔子"]
print(animal_set )
输出结果:["老虎","狮子","大象","老鹰","猴子","兔子"]
animal.clear()
print(animal)
输出结果:[ ]
animal.pop(2)
print(animal)
输出结果:["老虎","狮子","老鹰"]
animal.remove("大象")
print(animal)
输出结果:["老虎","狮子","老鹰"]
del animal[0:2]
print(animal)
输出结果:["老鹰"]
animal[1] = 12
print(animal)
输出结果:["老虎",12,"大象","老鹰"]
i = animal.index("狮子") #返回元素所在下标
#i = animal.index("老鹰",2) 从下标2开始查,返回3
print(i)
输出结果:1
exist_chick = "小鸡" in animal
print(exist_chick )
输出结果:False
for i in range animal:
if "小鸡" == i:
print("存在")
break
print("不存在")
输出结果:不存在
animal = ["老虎","狮子","大象","老鹰","猴子","兔子","山羊"]
new_animal = animal[2:5]
print(new_animal)
输出结果:["大象","老鹰","猴子"]
不可变
的序列,允许元素重复只能通过 del 函数对整个元组删除
del(animal)
a = animal[0]
print(a)
输出结果:"老虎"
newtuple = animal[2:4]
print(newtuple)
输出结果:("大象","老鹰")
for i in animal:
if "小鸟" == i:
print("存在")
break
animal = ("老虎","狮子","大象","老鹰","大象","大象","大象")
print(len(animal))
a = animal.count("大象")
print(a)
输出结果: 7
4
animal1 = ("老虎","狮子","大象","老鹰")
animal2 = ("小鸟","天鹅")
new_animal = animal1 + animal2
输出结果:("老虎","狮子","大象","老鹰","小鸟","天鹅")
键、值
dics["小赵"] = 4654653
print(dics)
输出结果:{"小张":123456555,"小明":25643868,"小王":352315645,"小赵":4654653}
dics.setdefault("小赵",4654653)
print(dics)
输出结果:{"小张":123456555,"小明":25643868,"小王":352315645,"小赵":4654653}
value = dics["小张"]
print(value)
输出结果:123456555
若是不存在会报错
value = dics.get("小张")
print(value)
输出结果:123456555
若是不存在不会报错
判断字典中的键是否存在
if "小张" in dics.keys():
print("这个键存在")
else
print("这个键不存在")
判断字典中的值是否存在
if 123456555 in dics.values():
print("这个值存在")
else
print("这个值不存在")
dics["小张"] = 00000000
print(dics)
输出结果:{"小张":00000000,"小明":25643868,"小王":352315645}
键存在就是修改,不存在就是添加
up_dic = {"小明":11111111,"小王":222222222,"小龙":35678949}
dics.update(up_dic)
print(dics)
输出结果:{"小张":123456555,"小明":11111111,"小王":222222222,"小龙":35678949}
键存在就是修改,不存在就是添加
del(dics["小张"])
print(dics)
输出结果:{"小明":25643868,"小王":352315645}
删除了这个键,键值对就不存在了
dics.pop("小张")
print(dics)
输出结果:{"小明":25643868,"小王":352315645}
dics.popitem()
print(dics)
输出结果:{"小张":123456555,"小明":25643868}
删除字典最后一对键和值
dics.clear()
print(dics)
输出结果:{ }
相当于清空里面所有的键值对
使用它 for 循环遍历字典
for key, value in dics.items():
print(key,value)
输出结果:小张 123456555
小明 25643868
小王 352315645
无序性、不重复性
set1.update({"12","23"})
set1.add("12")
set1.pop()
set1.remove("12")
使用 update() 函数,集合添加任何数据类型(不包含布尔类型),当为字典时,添加的是键,值会省略。
set1.update(任何数据类型,不包含布尔类型)
通过成员 in 判断元素是否在集合中
bools = "12" in set1
abs(a)
divmod(a,b)
#a除以b,输出商和余
pow(a,b)
#a的b次方
round(a,b)
#b代表是小数点保留位数,对a进行四舍五入
chr(x),x是整数,把该整数在ASCII表中对应的字符串输出
ord(x),x是字符串,把该字符串在ASCII表中对应得整数值输出
1+2
输出:3
5-3
输出:2
5*3
输出:15
5**3
#相当于5的3次方
输出:125
5/3
输出:1.6666666667
5%3
输出:2
5//3
输出:1