Container data type
set
{}
nature :
Disorder : Each element has the same place in the set
The opposite sex : In a collection , An element can only exist once
deterministic : Given an element , This element belongs to or does not belong to this collection
# How collections are created
a = set()
print(type(a))
# How dictionaries are created
b = {
}
print(type(b))
# add()
a.add(33)
# a.add({12, 32}) # Report errors
print(a)
# updata
a.update({
1, 20, 3})
print(a)
add() You can only add one by one ,updata() Add elements can be directly added in two columns
# remove()
a.remove(33)
print(a)
# pop(): Randomly delete elements from a collection
# stay cmd Is to randomly delete an element , however Python No
# If it is a string, delete the leftmost element
# If it's a number , Delete the smallest value
a.pop()
print(a)
c = {
'Python', 'java', 'Go', 'C++', 'C#'}
print(c)
c.pop()
print(c)
c.clear()
print(c)
e = set('hello')
print(e)
for i in e:
print(i)
f = {
i for i in range(1, 11)}
print(f)
print(1 in f)
set1 = {
4, 7, 9, 12, 56}
set2 = {
4, 7, 12, 32}
print(set1 & set2)
print(set1.intersection(set2))
print(set1 | set2)
print(set1.union(set2))
print(set1 - set2)
print(set2 - set1)
print(set1.difference(set2))
print(set1 ^ set2)
print(set1.symmetric_difference(set2))
set3 = {
20, 15, 32, 42}
set4 = {
20, 15}
print(set4 < set3)
print(set4 <= set3)
Dictionaries
{}
dict
The elements in the dictionary exist as key value pairs ,key:value
dict_1 = {
}
The key in the dictionary must be an immutable type : character string 、 Numbers 、 Component
Dictionary values can be of any type
dict_2 = {
'name': ' Xiao Ming ', 'age': 20}
print(dict_2)
dict_3 = dict(name=' The mushroom ', age=30)
print(dict_3)
dict_4 = dict(zip('ABCDE', '12345'))
print(dict_4)
dict_5 = dict(zip('ABCDE', range(1, 11)))
print(dict_5)
dict_6 = {
i: i ** 3 for i in range(1, 6)}
print(dict_6)
a = dict_2['name']
print(a)
# If the key you are looking for does not exist , Report errors
# b = dict_2['zone']
# print(b)
dict_2['name'] = ' Little pig '
print(dict_2)
# If you replace the value of a key that does not exist in the dictionary , Equal to add
dict_2['zone'] = '123'
print(dict_2)
for i in dict_2:
print(i, '--->', dict_2[i])
print(len(dict_2))
print(dict_2.keys())
print(dict_2.values())
print(dict_2.items())
for key, value in dict_2.items():
print(key, '--->', value)
a = dict_2.pop('zone')
print(a)
print(dict_2)
# If the dictionary is empty , Report errors ,ketError
b = dict_2.popitem()
print(b)
print(dict_2)