集合 唯一性 表示方法有三种
>>> {"FishC","Python"}
{'Python', 'FishC'}
>>> {s for s in "FishC"}
{'F', 'h', 'i', 's', 'C'} ------------随机 无序性
>>> set("FishC")
{'F', 'h', 'i', 's', 'C'}
不能用下标索引的方式访问集合
>>> s=set("FishC")
>>> "C"in s
True
>>> "C"not in s
False
>>> for each in s:
print (each)
F
h
i
s
C
>>> set([1,1,2,2,3,5]) -------看出唯一性
{1, 2, 3, 5}
>>> s=[1,1,2,2,3,5]
>>> len(s)==len(set(s)) -------检查集合中是否有相同元素
False
>>> t=s.copy() 同样可以实现浅拷贝
>>> t
[1, 1, 2, 2, 3, 5]
>>> s=set("FishC")
>>> s
{'F', 'h', 'i', 's', 'C'}
>>> s.isdisjoint(set("Python")) -----判断两个对象是否毫不相关
False
>>> s.isdisjoint(set("JAVA")) ---没有共同元素
True
>>> s.issubset("FishC.com.cn")---检查是否为它的子集
True
>>> s.union({1,2,3,}) 并集
{'i', 1, 'F', 2, 3, 's', 'C', 'h'}
>>> s.intersection("Fish") 交集
{'i', 'F', 's', 'h'}
>>> s.difference("Fish") 差集
{'C'}
>>> t=frozenset("FishC")
>>> t
frozenset({'F', 'h', 'i', 's', 'C'})
>>> s.update([1,1],"23")
>>> s
{1, 'F', 'h', '2', 'i', 's', 'C', '3'} ------只会传入一个1
>>> s.discard("一")
>>> s
{1, 'F', 'h', '2', 'i', 's', 'C', '3'}
>>> s.remove("一")
Traceback (most recent call last):
File "<pyshell#40>", line 1, in <module> 报错
s.remove("一")
KeyError: '一'
函数
创建和调用函数
>>> def myfunc():
for i in range(3):
print("i love fishc")
>>> myfunc()
i love fishc
i love fishc
i love fishc
可以通过函数的参数,来实现功能的定制
>>> def myfunc(name):
for i in range(3):
print(f"i love {name}")
>>> myfunc("python")
i love python
i love python
i love python
>>> def myfunc(name,times):
for i in range(times):
print(f"i love {name}")
>>> myfunc("Python",5)
i love Python
i love Python
i love Python
i love Python
i love Python
函数的返回值
>> def div(x,y):
z=x/y
return z
>>> div(4,2)
2.0
>>> def div(x,y):
return x/y
>>> div(4,2)
2.0