1.集合
2.字典
3.运算符优先级
1.集合
创建:() set() 注意:创建空的集合要用set()
特点:元素唯一,无序
运算: &(交集) |(并集) -(差集)
方法:
s.add(x) #添加单个元素
s.update() #添加多个元素
s.remove() #移除元素
s.clear() #清空集合
#集合创建
>>> se = {1,2,3}
>>> print(type(se))
>>> se = {1,2,2,3} #去重,唯一
>>> print(se)
{1, 2, 3}
>>> se = {1,"a",2,"b"} #无序
>>> se
{1, 2, "a", "b"}
#可hash的能放入集合
>>> hash([1,2])
Traceback (most recent call last):
File "", line 1, in
hash([1,2])
TypeError: unhashable type: "list"
>>> hash((a,2))
Traceback (most recent call last):
File "", line 1, in
hash((a,2))
NameError: name "a" is not defined
>>> hash((1,2))
1299869600
>>> hash("qwe")
-917773703
>>> a
Traceback (most recent call last):
File "", line 1, in
a
NameError: name "a" is not defined
>>> a="I love python"
>>> a
"I love python"
>>> hash(a)
-2061797837
>>>
#定义空集合(工厂模式定义集合)
>>> se = set()
>>> print(type(se))
#集合的运算
#属于
>>> a
{1, 2, 3, 4, "a"}
>>> "a" in a
True
>>> "b" not in a
True
#延伸
>>> se={1,2,3,4}
>>> se2={3,4,5,6}
>>> se < se2 #包含
False
>>> se <= se2 #包含
False
>>> se != se2 #不等于
True
#并集(两个集合相加并去重)
>>> se1={1,2,3};se2={2,3,4}
>>> se1|se2
{1, 2, 3, 4}
#交集(取两个集合重复的元素)
>>> se1&se2
{2, 3}
#差集(前面的集合减去后面的集合元素重复的部分)
>>> se1-se2
{1}
>>> se2-se1
{4}
#与非集(取各自集合独立的部分)
>>> se1^se2
{1, 4}
#集合的基本方法
查看集合的方法:
>>> dir(se)
["__and__", "__class__", "__contains__", "__delattr__", "__dir__", "__doc__", "__eq__", "__format__", "__ge__", "__getattribute__", "__gt__", "__hash__", "__iand__", "__init__", "__init_subclass__", "__ior__", "__isub__", "__iter__", "__ixor__", "__le__", "__len__", "__lt__", "__ne__", "__new__", "__or__", "__rand__", "__reduce__", "__reduce_ex__", "__repr__", "__ror__", "__rsub__", "__rxor__", "__setattr__", "__sizeof__", "__str__", "__sub__", "__subclasshook__", "__xor__", "add", "clear", "copy", "difference", "difference_update", "discard", "intersection", "intersection_update", "isdisjoint", "issubset", "issuperset", "pop", "remove", "symmetric_difference", "symmetric_difference_update", "union", "update"]
#添加单个元素(添加单个元素)
>>> se={1,2}
>>> se.add(3)
>>> print(se)
{1, 2, 3}
#添加多个元素(添加多个元素(可迭代的对象))
>>> se.update("4")
>>> print(se)
{1, 2, 3, "4"}
>>> se.update("456")
>>> print(se)
{1, 2, 3, "6", "4", "5"}
#移除元素(指定移除)
>>> se.remove("5")
>>> print(se)
{1, 2, 3, "6", "4"}
#随机移除
>>> se.pop()
1
#清空集合
>>> se.clear()
>>> print(se)
set()
2.字典
注:是python中唯一的一个映射类型
创建:{key:value} #大括号创建字典的键时要加引号
dict{key=value} #括号里赋值方式,名字=对象,不要引号
字典里的键和值用":’隔开,一对键和值组成一个项,项和项之间用",’隔开
特点:
键唯一,重复会被重新赋值
无序
key必须遵循python命名规则
添加和取值
cidt[key]=value #key存在则修改该值,没有则添加
属性方法:
.update({}) #在字典中添加多个项
.items() #返回字典的各个项
.keys() #返回字典的键
.values() #返回字典的值
.get(k) #如果键k在,返回k的值,不存在则返回None
.get(k,x) #如果键k在,返回键k的值,不存在则返回x
.pop(k) #返回并移除键k所对应的元素,不存在则抛出异常
.pop(k,x) #返回并移除键k所对应的元素,不存在则返回x
总结:
key唯一,故可以是数字,字符串,元祖
总结:
可变对象: list set dict
不可变对象: str tuple
#字典 唯一的映射类型,遵循hash,必须是不可变的对象
#定义字典
>>> di={"w":123,"l":456,"x":789}
>>> print(type(di))
>>> di=dict(_i=123)
>>> di
{"_i": 123}
>>> print(type(di))
>>> di={1:123,2:234}
>>> print(type(di))
>>> di1={"e":[123,456]}
>>> type(di1)
>>> di2={"e":(123,456)}
>>> di3={"e":"123"}
>>> type(di3)
#定义空字典
>>> di1=dict()
>>> print(type(di1))
#字典取值(利用键取值)
>>> di[1]
123
>>> di[2]
234
#字典修改
>>> di[1]="qwe"
>>> di
{1: "qwe", 2: 234}
#添加key:value(在修改key值得时候,key存在即修改否则添加)
>>> di[3]=890
>>> di
{1: "qwe", 2: 234, 3: 890}
>>> di={"q":1,"w":2,("q","w"):122}
>>> di
{"q": 1, "w": 2, ("q", "w"): 122}
#清空字典
>>> di.clear()
>>> print(di)
{}
#查看字典的属性方法
>>> dir(di)
["__class__", "__contains__", "__delattr__", "__delitem__", "__dir__", "__doc__", "__eq__", "__format__", "__ge__", "__getattribute__", "__getitem__", "__gt__", "__hash__", "__init__", "__init_subclass__", "__iter__", "__le__", "__len__", "__lt__", "__ne__", "__new__", "__reduce__", "__reduce_ex__", "__repr__", "__setattr__", "__setitem__", "__sizeof__", "__str__", "__subclasshook__", "clear", "copy", "fromkeys", "get", "items", "keys", "pop", "popitem", "setdefault", "update", "values"]
#fromkeys
#用给定的键建立新的字典,每个键默认为None(批量生产新的字典)
>>> di.fromkeys({"a","b","c"})
{"b": None, "c": None, "a": None}
#用给定的键建立新的字典,每个键自定义为123
>>> di.fromkeys({"a","b","c"},123)
{"b": 123, "c": 123, "a": 123}
>>> help(di.fromkeys)
Help on built-in function fromkeys:
fromkeys(iterable, value=None, /) method of builtins.type instance
Returns a new dict with keys from iterable and values equal to value.
#字典取值;值存在,则返回值,不存在默认返回None,也可自定义
>>> di
{"w": 123, "e": 456, "r": 789}
>>> di.get("w")
123
>>> di.get("q")
>>> di
{"w": 123, "e": 456, "r": 789}
>>> di.get("q","我不存在")
"我不存在"
#items,在列表中以元组的形式显示字典的每一项
>>> di.items()
dict_items([("w", 123), ("e", 456), ("r", 789)])
>>> list(di.items()) #查看字典的每一项
[("w", 123), ("e", 456), ("r", 789)]
#以列表的形式查看字典的所有键
>>> di.keys()
dict_keys(["w", "e", "r"])
#以列表的形式查看字典的所有值
>>> di.values()
dict_values([123, 456, 789])
#pop,指定键,删除对应的值。如果键不存在,可以自定义返回值
>>> help(di.pop)
Help on built-in function pop:
pop(...) method of builtins.dict instance
D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
If key is not found, d is returned if given, otherwise KeyError is raised
>>> di
{"w": 123, "e": 456, "r": 789}
>>> di.pop("e")
456
>>> di
{"w": 123, "r": 789}
>>> di.pop("w","r")
123
>>> di
{"r": 789}
>>> di.pop("q","我不存在")
"我不存在"
#popitem, 随机删除字典某一项(不需要对象)
>>> di
{"r": 789, "w": 123}
>>> di
{"r": 789, "w": 123}
>>> di.popitem()
("w", 123)
>>> di
{"r": 789}
#类似get,存在返回值,不存在就更新到字典,对应的值默认为None,也可自定义
>>> di.setdefault("r")
789
>>> di.setdefault("w",123)
123
>>> di
{"r": 789, "w": 123}
>>> di.setdefault("q")
>>> di
{"r": 789, "w": 123, "q": None
#将一个字典内容添加并更新覆盖到原来的字典
>>> di
{"r": 789, "w": 123, "q": None}
>>> di1={"p":234,"q":123}
>>> di.update(di1)
>>> di
{"p": 234, "r": 789, "w": 123, "q": 123}
>>> di={"x":[123,456]}
>>> di
{"x": [123, 456]}
>>> di["w"]=123
>>> di
{"x": [123, 456], "w": 123}
3.运算符
算数运算符: +,-,*,/,%,**,//
赋值运算符: = += -= *= /= %= **=
比较运算符: == != > < >= <=
成员运算符: in , not in
身份运算符: is , is not
判断两个名字是否指向同一个对象,当id相同时返回True(==比较运算时判断的值)
逻辑运算符: and or not
and(与) 两个条件都满足是才返回True
or(或) 有一个条件满足了就返回True
not(非) 取反
计算顺序:默认,运算符优先级表决定了那个运算符在别的运算符之前计算。然而,如果你想改变它们的顺序,你得使用圆括号
结合规律:运算符通常由左向右结合,及具有相同优先级的运算符按照从左向右的顺序计算
** #幂运算
= - * / % #算数运算符
< > <= >= #比较运算符
== != #比较运算符
= %= /= -= += *= **= #赋值运算符
is is not #身份运算符
in not in #成员运算符
not > and > or #逻辑运算符
>>> a=1;b=2
>>> a==b
False
>>> a!=b
True
>>> a is b
False
>>> a is not b
True
>>> a==1 and b==3
False
>>> a==1 or b==3
True
>>> not b==3
True
>>> a==1 and not b==3
True