码迷,mamicode.com
首页 > 编程语言 > 详细

Python入门基础学习四

时间:2020-02-17 21:22:36      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:seve   eve   入门   字符串格式化   ber   enc   style   card   格式化   

------------恢复内容开始------------

集合

1、不同元素组成

2、无序

3.集合元素必须是不可变类型

 

补充:

可变不可变:

1、可变:列表,字典

2、不可变:字符串,数字,元组

 

访问顺序:

1、直接访问:数字

2、顺序访问:字符串,列表,元组

3、映射:字典

 

存放元素个数:

容器类型:列表,元组,字典

原子:数字,字符串

 

魔法

 

set = {1,2,3,4,5}
set.add("s")
print(set)
set.pop() #随机删
print(set)
set.remove(5) #指定删
print(set)
set.discard(11) #指定删,不存在不会报错
print(set)
python_1 = ["zhangzan","wangyi","zhanziqi"]
linux_1 = ["zhangzan","feiji","wuzhen"]
p_s = set(python_1)
l_s = set(linux_1)
print(p_s,l_s)
#求交集
print(p_s.intersection(l_s))
print(p_s&l_s)
#求并集
print(p_s.union(l_s))
print(p_s|l_s)
#求差集
print(p_s.difference(l_s))
print(p_s-l_s)
print(l_s.difference(p_s))
#求交叉补集
print(p_s.symmetric_difference(l_s))
print(p_s^l_s)
s1 = {3,5}
s2 = {3,4,5}
print(s1.issubset(s2)) #Ture  s1是s2的子集
print(s2.issubset(s1))#False 

 

字符串格式化

%

tpl = "i am %s age %d"%("alex",13)
print(tpl)
tpl = "i am %(name)s age %(age)d"%{"name":"abc","age":13}
print(tpl)
tpl = "percent %.2f%%"%99.9787
print(tpl)
tpl = "percent %.2f"%99.9787
print(tpl)
tpl = "percent %(a).2f" %{"a": 99.95421}
print(tpl)

 format

tpl = "i am {},age {},{}.".format("alex",13,"seven")
print(tpl)
tpl = "i am {} , age {},{}".format(*["alex",13,"even"])
print(tpl)
tpl = "i am {0} , age{1} , {2}".format("alex",13,"seven")
print(tpl)
tpl = "i am {0},age{1},really{2}".format(*["alex",13,"seven"])
print(tpl)
tpl = "i am {name},age {age},really{name}".format(name="alex",age=13)
print(tpl)
tpl = "i am {name},age{age},really{name}".format(**{"name":"alex","age":15})
print(tpl)
tpl = "i am {0[1]},age{0[1]},really{1[2]}".format([1,2,3],[4,5,6])
print(tpl)
tpl = "i am {:s},age{:d},pencent{:f}".format("alex",13,9.999)
print(tpl)
tpl = "i am {:s},age{:d}".format("alex",13)
print(tpl)
tpl = "i am {:s}, age {:d}, money {:f}".format("seven", 18, 88888.1)
print(tpl)
tpl = "i am {:s}, age {:d}".format(*["seven", 18])
print(tpl)
tpl = "i am {name:s}, age {age:d}".format(name="seven", age=18)
print(tpl)
tpl = "i am {name:s}, age {age:d}".format(**{"name": "seven", "age": 18})
print(tpl)
tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2)
print(tpl)
tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2)
print(tpl)
tpl = "numbers: {0:b},{0:o},{0:d},{0:x},{0:X}, {0:%}".format(15)
print(tpl)
tpl = "numbers: {num:b},{num:o},{num:d},{num:x},{num:X}, {num:%}".format(num=15)
print(tpl)

 

 

 

 

------------恢复内容结束------------

Python入门基础学习四

标签:seve   eve   入门   字符串格式化   ber   enc   style   card   格式化   

原文地址:https://www.cnblogs.com/zhangzanyao/p/12323692.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!