Python入门基础学习四

Posted zhangzanyao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python入门基础学习四相关的知识,希望对你有一定的参考价值。

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

集合

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入门基础学习四的主要内容,如果未能解决你的问题,请参考以下文章

python基础入门(语法基础)

python基础入门(语法基础)

python基础入门(语法基础)

python基础入门(语法基础)

python基础入门(语法基础)

python基础入门(语法基础)