Function one of the set: relational operation
The function of the set two: to go heavy
To define a collection:
The elements within the set must be unique;
The elements in the set must be hash, and also the immutable type;
The collection is unordered
123456 |
s
=
{
‘egon‘
,
123
,
‘egon‘
,
‘1‘
}
#s=set({‘egon‘,123,‘egon‘,‘1‘} )
print
(s,
type
(s))
s
=
{
‘1‘
,
1
,[
1
,
2
]}
s
=
{
‘1‘
,
1
,(
1
,
2
),
‘a‘
,
‘b‘
}
print
(s)
|
Cycle
123 |
s = { ' 1 ' 1 1 2 ' a ' ' B ' } for i in s: print (i) |
Relational operations
1234567891011121314151617181920 |
python_s
=
{
‘egon‘
,
‘alex‘
,
‘钢蛋‘
,
‘老王‘
}
linux_s
=
{
‘alex‘
,
‘钢蛋‘
,
‘欧德博爱‘
,
‘艾里科四‘
}
#取共同部分:交集
print
(python_s & linux_s)
#取老男孩所有报名学习的学生:并集
print
(python_s | linux_s)
#取只报名了python课程的学生:差集
print
(python_s
-
linux_s)
#取只报名了linux课程的学生:差集
print
(linux_s
-
python_s)
#取没有同时报名python和linux课程的学:对称差集
print
(linux_s ^ python_s)
|
Collection method
123456789101112131415161718 |
#集合方法
python_s
=
{
‘egon‘
,
‘alex‘
,
‘钢蛋‘
,
‘老王‘
}
linux_s
=
{
‘alex‘
,
‘钢蛋‘
,
‘欧德博爱‘
,
‘艾里科四‘
}
print
(python_s.intersection(linux_s))
# 交集:python_s & linux_s
print
(python_s.union(linux_s))
#并集:|
print
(python_s.difference(linux_s))
#python_s-linux_s
print
(python_s.symmetric_difference(linux_s))
# 对称差集,
python_s ^ linux_s
python_s
=
{
‘egon‘
,
‘alex‘
,
‘钢蛋‘
,
‘老王‘
}
linux_s
=
{
‘alex‘
,
‘钢蛋‘
,
‘欧德博爱‘
,
‘艾里科四‘
}
python_s.difference_update(linux_s)
print
(python_s)
|
Collection operation type
1234567891011121314 |
s1
=
{
‘a‘
,
1
}
s1.add(
1
)
#追加一项
print
(s1)
s1.discard(
‘a‘
)
#如果在 set “s”中存在元素 x, 则删除
s1.discard(
‘b‘
)
print
(s1)
s1.remove(
‘bbbbbbb‘
)
#移除
print
(s1)
s1
=
{
‘a‘
,
1
,
‘b‘
,
‘c‘
,
‘d‘
}
print
(s1.pop())
#随机删除
|
Summarize
+ View Code
Boolean type:
True and False
Print (Type (True))
count=10
Print (BOOL (Count > 10))
#所有的数据类型自带布尔值, only 0,none, empty Boolean value is False
1234567 |
l
=
[]
if
len
(l)
=
=
0
:
print
(
‘列表为空‘
)
print
(
bool
(l))
if
not
l:
#bool(l)
print
(
‘列表为空‘
)
|
String
Definition: It is a collection of ordered characters used to store and represent basic textual information, "' or" "or" ' "" that contains what is called a string
Characteristics:
1. Only one value can be stored
2. Non-volatile
3. Define the character set in left-to-right order, the subscript is accessed sequentially from 0, ordered
Add:
1. Both single and double quotation marks cannot suppress the meaning of special characters, if you want all the characters in quotation marks to nonspacing special meaning, precede the quotation marks with R, such as Name=r ' L\THF '
Python (Day4) collection, Boolean type