練習-1 <<
Previous Next >> 練習-3
練習-2
# 集合的運用
|
1
2
3
4
|
s1={3,4,5}
print(3 in s1)
print(10 in s1)
print(10 not in s1)
|
True
False
True
|
1
2
3
4
5
6
7
8
9
10
|
s1={3,4,5}
s2={4,5,6,7}
s3=s1&s2
print(s3)
s3=s1|s2
print(s3)
s3=s1-s2
print(s3)
s3=s1^s2
print(s3)
|
{4, 5}
{3, 4, 5, 6, 7}
{3}
{3, 6, 7}
|
1
2
3
4
|
s=set("Hello")
print(s)
print("H" in s)
print("A" in s)
|
{'H', 'e', 'l', 'o'}
True
False
#字典的運用︰key-value 配對
|
1
2
3
4
5
|
dic={"apple":"蘋果","bug":"蟲蟲"}
print(dic)
print(dic["apple"])
dic["apple"]="小蘋果"
print(dic["apple"])
|
{'apple': '蘋果', 'bug': '蟲蟲'}
蘋果
小蘋果
|
1
2
3
4
5
6
|
dic={"apple":"蘋果","bug":"蟲蟲"}
print("apple" in dic)
print("test" in dic)
print("test" not in dic)
del dic["apple"]
print(dic)
|
True
False
True
{'bug': '蟲蟲'}
|
1
2
|
dic={x:x*2 for x in [3,4,5]}
print(dic)
|
{3: 6, 4: 8, 5: 10}
練習-1 <<
Previous Next >> 練習-3