数据类型

数据类型概述

int 整型

float 浮点型

bool 布尔

string 字符串 -不可变

dict 字典 -可变类型

list 列表 [] -可变

tuple 元组 () -不可变类型

set 集合 {} -可变类型

字符串

举例说明

name="alex   \r \t"
print(name.strip())
'''
alex
'''
1. 移除空白 strip
bb="alex|egon|qiqi|huihui"
print(bb.split("|"))
'''
['alex', 'egon', 'qiqi', 'huihui'] #列表
'''
2. 切分 split
bb="alexegonqiqihuihui"
print("|".join(bb))
'''
a|l|e|x|e|g|o|n|q|i|q|i|h|u|i|h|u|i
'''
3. 组合join
aa="alex"
print(aa.index("a")) #没有就会报错
print(aa.find("a")) #没有显示-1
print(aa[0])
'''
0
0
a
'''
4. 按照索引正反取值 index find
aa="alex"
print(len(aa))
5. 长度 len
aa="qiqihuihui"

print(aa[0:4])
print(aa[0:-1])
'''
qiqi
qiqihuihu
'''
6. 切片 (顾头不顾尾)
aa="qiqiHUIhui"
print(aa.upper())
print(aa.lower())
'''
QIQIHUIHUI
qiqihuihui
'''
7. 大小写转换 lower upper
aa="qiqiHUIhui"
if "qiqi" in aa:
    print(True)
else:
    print(False)
8. in和not in 成员运算
aa="qiqi"
for i in aa:
    print(i)
9. 循环for
aa="qiqi"
print(aa.startswith("q"))
print(aa.endswith("i"))
10. 判断开始结束元素 startswith、endswith
aa="qiqi"
bb=aa.replace("qiqi","gege")
print(bb)
print(aa)
11. 替换 replace
age="18"
print(age.isdigit())
'''
True
'''
12. 是否是数字 isdigit
13. 格式化输出  format三种方式
13. 格式化输出 format三种方式

 字典

举例说明

dic={
    "name":"alex",
    "age":13,
    "hobby":"gril"
}

print(dic["name"])
print(dic.get("name"))
'''
alex
alex
'''
1. 根据key取值 可取可存
dic={
    "name":"alex",
    "age":13,
    "hobby":"gril"
}


print(len(dic)) #3
2. 长度 len
dic={
    "name":"alex",
    "age":13,
    "hobby":"gril"
}

if "alex" in dic.get("name"):
    print(True) #True
3. 成员判断 in和not in
#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'}
'''
4. 删除
dic={
    "name":"alex",
    "age":13,
    "hobby":"gril"
}

dic.setdefault("job","teacher")
print(dic)
'''
{'name': 'alex', 'age': 13, 'hobby': 'gril', 'job': 'teacher'}
'''
5. setdefault 存在不做处理,不存在增加
#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
'''
6. 循环

列表

lists=["alex","egon","qiqi",1,2,3]
print(lists.index("alex"))
print(lists[0])
'''
0
alex
'''
1. 按照索引正反取值 index
lists=["alex","egon","qiqi",1,2,3]
print(len(lists))
2. 长度 len
lists=["alex","egon","qiqi",1,2,3]
print(lists[0:3])
'''
['alex', 'egon', 'qiqi']
'''
3. 切片
lists=["alex","egon","qiqi",1,2,3]

if "alex" in lists:
    print(True)
4. 成员判断 in 和not in
lists=["alex","egon","qiqi",1,2,3]

lists.append("新加")
print(lists)
'''
['alex', 'egon', 'qiqi', 1, 2, 3, '新加']
'''
5. 追加 append
#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)
6. 删除
lists=["alex","egon","qiqi",1,2,3]

for i in lists:
    print(i)
'''
alex
egon
qiqi
1
2
3
'''
7. 循环

元组

参考列表的用法

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'}
'''
3. 添加 无则添加,有则不操作
#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'}
'''
4. 删除
#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'}

'''
5. 差集
se1 = {'alex','eric','qiqi','dh'}
se2 = {'alex','eric','gege','yiyi'}
v = se1.intersection(se2)
se1.intersection_update(se2) #将交集覆盖赋值给se1
print(se1)
print(v)
6. 交集
se1 = {'alex','eric','qiqi','dh'}
se2 = {'alex','eric','gege','yiyi'}
v = se1.union(se2)
print(v)
7. 并集
posted @ 2019-10-10 11:02  lazyball  阅读(253)  评论(0编辑  收藏  举报