数据类型
数据类型概述
int 整型
float 浮点型
bool 布尔
string 字符串 -不可变
dict 字典 -可变类型
list 列表 [] -可变
tuple 元组 () -不可变类型
set 集合 {} -可变类型
字符串
举例说明
name="alex \r \t" print(name.strip()) ''' alex '''
bb="alex|egon|qiqi|huihui" print(bb.split("|")) ''' ['alex', 'egon', 'qiqi', 'huihui'] #列表 '''
bb="alexegonqiqihuihui" print("|".join(bb)) ''' a|l|e|x|e|g|o|n|q|i|q|i|h|u|i|h|u|i '''
aa="alex" print(aa.index("a")) #没有就会报错 print(aa.find("a")) #没有显示-1 print(aa[0]) ''' 0 0 a '''
aa="alex" print(len(aa))
aa="qiqihuihui" print(aa[0:4]) print(aa[0:-1]) ''' qiqi qiqihuihu '''
aa="qiqiHUIhui" print(aa.upper()) print(aa.lower()) ''' QIQIHUIHUI qiqihuihui '''
aa="qiqiHUIhui" if "qiqi" in aa: print(True) else: print(False)
aa="qiqi" for i in aa: print(i)
aa="qiqi" print(aa.startswith("q")) print(aa.endswith("i"))
aa="qiqi" bb=aa.replace("qiqi","gege") print(bb) print(aa)
age="18" print(age.isdigit()) ''' True '''
13. 格式化输出 format三种方式
字典
举例说明
dic={ "name":"alex", "age":13, "hobby":"gril" } print(dic["name"]) print(dic.get("name")) ''' alex alex '''
dic={ "name":"alex", "age":13, "hobby":"gril" } print(len(dic)) #3
dic={ "name":"alex", "age":13, "hobby":"gril" } if "alex" in dic.get("name"): print(True) #True
#1. pop dic={ "name":"alex", "age":13, "hobby":"gril" } dic.pop("name") print(dic) ''' {'age': 13, 'hobby': 'gril'} ''' #2. popitem 随机删除一个键值对 dic={ "name":"alex", "age":13, "hobby":"gril" } v=dic.popitem() print(v) print(dic) ''' ('hobby', 'gril') {'name': 'alex', 'age': 13} ''' #3. del dic={ "name":"alex", "age":13, "hobby":"gril" } del dic["name"] print(dic) ''' {'age': 13, 'hobby': 'gril'} '''
dic={ "name":"alex", "age":13, "hobby":"gril" } dic.setdefault("job","teacher") print(dic) ''' {'name': 'alex', 'age': 13, 'hobby': 'gril', 'job': 'teacher'} '''
#keys() values() items() dic={ "name":"alex", "age":13, "hobby":"gril" } for k in dic.keys(): print(k) ''' name age hobby ''' for v in dic.values(): print(v) ''' alex 13 gril ''' for k,v in dic.items(): print(k,v) ''' name alex age 13 hobby gril '''
列表
lists=["alex","egon","qiqi",1,2,3] print(lists.index("alex")) print(lists[0]) ''' 0 alex '''
lists=["alex","egon","qiqi",1,2,3] print(len(lists))
lists=["alex","egon","qiqi",1,2,3] print(lists[0:3]) ''' ['alex', 'egon', 'qiqi'] '''
lists=["alex","egon","qiqi",1,2,3] if "alex" in lists: print(True)
lists=["alex","egon","qiqi",1,2,3] lists.append("新加") print(lists) ''' ['alex', 'egon', 'qiqi', 1, 2, 3, '新加'] '''
#1. pop 弹出 lists=["alex","egon","qiqi",1,2,3] pop_obj=lists.pop(0) print(pop_obj) print(lists) ''' alex ['egon', 'qiqi', 1, 2, 3] ''' #2. remove lists=["alex","egon","qiqi",1,2,3] lists.remove("alex") print(lists) ''' ['egon', 'qiqi', 1, 2, 3] ''' #3. del lists=["alex","egon","qiqi",1,2,3] del lists[0] print(lists)
lists=["alex","egon","qiqi",1,2,3] for i in lists: print(i) ''' alex egon qiqi 1 2 3 '''
元组
参考列表的用法
1. 按照索引取值 只能取
2. 切片
3. 长度 len
4. 成员判断 in 和 not in
5. 循环
集合
1. 长度 len
2. 成员判断 in 和not in
group1={"alex","egon","qiqi","binbin"} group1.add("xinjia") print(group1) ''' {'binbin', 'egon', 'alex', 'xinjia', 'qiqi'} '''
#1. pop 随机删除 group1={"alex","egon","qiqi","binbin"} group1.pop() #随机删除 print(group1) ''' {'binbin', 'qiqi', 'alex'} ''' #2. discard group1={"alex","egon","qiqi","binbin"} group1.discard("binbin") print(group1) ''' {'qiqi', 'alex', 'egon'} ''' #3. remove group1={"alex","egon","qiqi","binbin"} group1.remove("binbin") print(group1) ''' {'egon', 'qiqi', 'alex'} '''
#1. difference 输出 se1中有的,se2中无的 赋值给新的变量 se1 = {'alex','eric','qiqi','dh'} se2 = {'alex','eric','gege','yiyi'} v = se1.difference(se2) print(v)#{'qiqi', 'dh'} #2. difference_update 输出 将se1中有的,se2中无的 重新赋值给se1 (se1会被清空,变成se1中有的,se2中无的值) se1 = {'alex','eric','qiqi','dh'} se2 = {'alex','eric','gege','yiyi'} se1.difference_update(se2) print(se1) #{'qiqi', 'dh'} #3. symmetric_difference 将两者不同的 赋值给新的变量 se1 = {'alex','eric','qiqi','dh'} se2 = {'alex','eric','gege','yiyi'} v = se1.symmetric_difference(se2) se1.symmetric_difference_update(se2)#将两者不同的 覆盖赋值给se1 print(se1) print(v) ''' {'dh', 'qiqi', 'gege', 'yiyi'} {'dh', 'qiqi', 'gege', 'yiyi'} '''
se1 = {'alex','eric','qiqi','dh'} se2 = {'alex','eric','gege','yiyi'} v = se1.intersection(se2) se1.intersection_update(se2) #将交集覆盖赋值给se1 print(se1) print(v)
se1 = {'alex','eric','qiqi','dh'} se2 = {'alex','eric','gege','yiyi'} v = se1.union(se2) print(v)