This is the sixth day of my participation in the November Gwen Challenge. See details: The Last Gwen Challenge 2021.

Runtime environment

Python: 3.8.3 jupyter-notebook: 6.4.0

Note: This example can be run directly on jupyter-Notebook, but PyCharm requires print at the end of the code.


tuples

tuple()

Tuple () converts an iterable to a tuple.

tuple([0.1.2]) + tuple(range(3)) + tuple({0.1.2}) + tuple('012')
Copy the code
Output :(0, 1, 2, 0, 2, 0, 1, 1, 2, ‘0’, ‘1’, ‘2’)
Iterables are converted into lists and concatenated by operators.


The dictionary

dict.clear()

Clear () clears all the contents of the dictionary.

dic = {'CSDN': 'Dream, killer'.'Public Account': 'Python New Horizons'}
dic.clear()
dic
Copy the code
Output: {}


dict.fromkeys()

Fromkeys () creates a new dictionary using elements from iterable as the dictionary keys, and values as the initial values of all the keys in the dictionary.

  • Iterable: iterable, the key of a new dictionary.
  • Value:This parameter is optional. The default value isNone.
dict.fromkeys(['CSDN'.'Public Account'].'Python New Horizons')
Copy the code
Output: {‘CSDN’: ‘Python new Horizons ‘, ‘public id ‘: ‘Python new Horizons ‘}


dict.get()

Get (key, default=None) Finds the specified key value, returns the value of key if it is in the dictionary, and None otherwise.

Sample 🅰 ️

dic = {'CSDN': 'Dream, killer'.'Public Account': 'Python New Horizons'}
dic.get('CSDN')
Copy the code
Output: ‘Dream, killer’


Sample 🅱 ️

dic = {'CSDN': 'Dream, killer'.'Public Account': 'Python New Horizons'}
print(dic.get('WeChat'))
Copy the code
Output: None
It’s not in the dictionarykeyIs equal to the'WeChat'To return toNone, Jupyter Notebook forNoneIf you don’t addprintIt doesn’t output by default, so I’m going to addprintTo print the results


dict.items()

Items () returns a view object, which is a traversable key/value pair that can be converted to a list using list().

Sample 🅰 ️

dic = {'CSDN': 'Dream, killer'.'Public Account': 'Python New Horizons'}
list(dic.items())
Copy the code
Output: [(‘ CSDN ‘, ‘Dream, apparently), (‘ the public’, ‘a new vision of Python)]


Sample 🅱 ️

dic = {'CSDN': 'Dream, killer'.'Public Account': 'Python New Horizons'}
for key, value in dic.items():
    print('key: ', key, 'value: ', value)

# Key: CSDN value: Dream, killer
# key: public number value: Python new horizons
Copy the code


dict.keys()

Keys () returns a view object with a dictionary key that can be converted to a list.

dic = {'CSDN': 'Dream, killer'.'Public Account': 'Python New Horizons'}
dic.keys()
Copy the code
Output: dict_keys([‘CSDN’, ‘CSDN’])


dict.setdefault()

Setdefault (key, default=None) Inserts a key with a value of None if the key is not in the dictionary. Returns the value of the key if it is in the dictionary.

Sample 🅰 ️

dic = {'CSDN': 'Dream, killer'.'Public Account': 'Python New Horizons'}
dic.setdefault('CSDN'.'python-sun')
Copy the code
Output: ‘Dream, killer’
The dictionary isCSDNthiskeyValue, returnCSDNThe corresponding value, no need to insert


Sample 🅱 ️

dic = {'CSDN': 'Dream, killer'.'Public Account': 'Python New Horizons'}
dic.setdefault('WeChat'.'python-sun')
dic
Copy the code
Output: {‘CSDN’: ‘Dream, killer’, ‘public id ‘: ‘Python new Vision ‘,’ wechat ‘: ‘python-sun’}
It’s not in the dictionaryWeChatthiskeyValue, returnNone, perform the insert according to the set parameterspython-sunTo do the assignment.


dict.update()

Dict. update(dict1) Update the dictionary dict1’s key/value pair to the dict. When the dict1’s key appears in the dict, modify the dict value. Add the key/value pair.

dic = {'CSDN': 'Dream, killer'.'Public Account': 'Python New Horizons'}
dic1 = {'CSDN': 'new_name'.'WeChat': 'python-sun'}
dic.update(dic1)
dic
Copy the code
Output: {‘ CSDN ‘:’ new_name ‘, ‘public’ : ‘Python new horizons’,’ WeChat ‘:’ the Python – sun ‘}


dict.values()

Values () returns a view object with the value of a dictionary that can be converted to a list.

dic = {'CSDN': 'Dream, killer'.'Public Account': 'Python New Horizons'}
dic.values()
Copy the code
Output: dict_values([‘Dream, killer’, ‘Python ‘])


dict.pop()

Pop () removes the key/value of the specified key, or an error if the key is not found.

dic = {'CSDN': 'Dream, killer'.'Public Account': 'Python New Horizons'}
dic.pop('CSDN')
dic
Copy the code
Output: {‘ public ‘: ‘Python new Horizons ‘}


dict.popitem()

Popitem () removes the last element in the dictionary and returns a tuple of (key, value) pairs. An error is reported if the dictionary is empty.

dic = {'CSDN': 'Dream, killer'.'Public Account': 'Python New Horizons'}
dic.popitem()
Copy the code
Output :(‘ public ‘, ‘Python new horizons ‘)


A collection of

set.add()

Adding an element to the collection does not work if the element is already in the collection.

Sample 🅰 ️

set1 = {'Dream, Killer'.'Python New Horizons'.'python-sun'}
set1.add('python')
set1
Copy the code
Output: {‘Dream, Killer’, ‘Python ‘, ‘python-sun’}


Sample 🅱 ️

set1 = {'Dream, Killer'.'Python New Horizons'.'python-sun'}
set1.add('python-sun')
set1
Copy the code
Output: {‘Dream, Killer’, ‘Python ‘, ‘python-sun’}
Added elementspython-sunIt’s already in the set, soset1Do not change


set.clear()

Clear () removes all elements from the collection.

set1 = {'Dream, Killer'.'Python New Horizons'.'python-sun'}
set1.clear()
set1
Copy the code
Output: set ()


set.difference() & set.difference_update()

1️ difference() returns the difference set of multiple sets, which, in popular terms, is to return which elements in the first set do not appear in other sets.

Sample 🅰 ️

set1 = {'Dream, Killer'.'Python New Horizons'.'python-sun'}
set2 = {'python'.'Dream, Killer'}
set3 = {'Python New Horizons'}
set1.difference(set2, set3)
Copy the code
Output: {} ‘python – sun’
set1The elements in thepython-sunNot in theset2set3, so as well as the form of the collection{'python-sun'}


Sample 🅱 ️

set1 = {'Dream, Killer'.'Python New Horizons'.'python-sun'}
set2 = {'python'.'Dream, Killer'}
set3 = {'Python New Horizons'.'python-sun'}
set1.difference(set2, set3)
Copy the code
Output: set ()
set1All of the elements inset2set3, so return the empty collectionset()


2 the difference between ️ difference_update() method and difference() method is that difference() method returns a new set of removing the same element, while difference() method directly removes the elements in the original set, No return value.

Sample 🅰 ️

set1 = {'Dream, Killer'.'Python New Horizons'.'python-sun'}
set2 = {'python'.'Dream, Killer'}
set3 = {'Python New Horizons'}
set1.difference_update(set2, set3)
set1
Copy the code
Output: {} ‘python – sun’
set1The elements in theDream, KillerA new vision of PythonThere are inset2set3So fromset1To remove these values


Sample 🅱 ️

set1 = {'Dream, Killer'.'Python New Horizons'.'python-sun'}
set2 = {'python'.'Dream, Killer'}
set3 = {'Python New Horizons'.'python-sun'}
set1.difference_update(set2, set3)
set1
Copy the code
Output: set ()
set1All of the elements inset2set3Has appeared in,set1Empty set after removing all valuesset()


set.discard()

Discard () removes the specified element in the collection. If the element specified for removal is not in the collection, it is not removed.

Sample 🅰 ️

set1 = {'Dream, Killer'.'Python New Horizons'.'python-sun'}
set1.discard('Dream, Killer')
set1
Copy the code
Output: {‘Python new horizons ‘, ‘python-sun’}
The specified element exists inset1And remove it from set1


Sample 🅱 ️

set1 = {'Dream, Killer'.'Python New Horizons'.'python-sun'}
set1.discard('python')
set1
Copy the code
Output: {‘Dream, Killer’, ‘Python ‘, ‘python-sun’}
The specified element is not presentset1Inside,set1Do not change


set.intersection() & set.intersection_update()

1️ intersection() return the intersection of set. Return the empty set set() if there is no intersection.

set1 = {'Dream, Killer'.'Python New Horizons'.'python-sun'}
set2 = {'python-sun'.'Dream, Killer'}
set3 = {'Python New Horizons'.'python-sun'}
set1.intersection(set2, set3)
Copy the code
Output: {} ‘python – sun’
returnset1,set2,set3Elements that occur simultaneously in


2️ intersection_update() method and intersection() method differ in that intersection() method returns the intersection of a set as a new set, However, the intersection_update() method directly modifies elements in the original set and only retains intersection elements without returning values.

set1 = {'Dream, Killer'.'Python New Horizons'.'python-sun'}
set2 = {'python-sun'.'Dream, Killer'}
set3 = {'Python New Horizons'.'python-sun'}
set1.intersection_update(set2, set3)
set1
Copy the code
Output: {} ‘python – sun’
Only in the characters'python-sunAt the same timeset2set3, so removedset1Other elements in


set.isdisjoint()

Isdisjoint () determines whether two sets contain the same elements, returning False if they do and True if they do not.

Sample 🅰 ️

set1 = {'Dream, Killer'.'Python New Horizons'.'python-sun'}
set2 = {'python-sun'.'Dream, Killer'}
set1.isdisjoint(set2)
Copy the code
Output: False
set1set2If there are two identical elements inFalse


Sample 🅱 ️

set1 = {'Dream, Killer'.'Python New Horizons'.'python-sun'}
set2 = {'python'}
set1.isdisjoint(set2)
Copy the code
Output: True,
set1set2If there are no identical elements inTrue


set.issubset()

Set2. Isset1 Determines whether SET2 is a subset of set1. Returns True if it is, False otherwise.

set1 = {'Dream, Killer'.'Python New Horizons'.'python-sun'}
set2 = {'python-sun'.'Dream, Killer'}
set2.issubset(set1)
Copy the code
Output: True,
set2set1A subset of the collection, so returnsTrue, please pay attention when usingset1set2The order of position. If writtenset1.issubset(set2)Will returnFalse


set.issuperset()

Set1. issuperset(set2) Determines whether set2 is a subset of set1. Returns True if it is, False otherwise. It’s the same as issubset(), except that the parameters are in opposite positions.

set1 = {'Dream, Killer'.'Python New Horizons'.'python-sun'}
set2 = {'python-sun'.'Dream, Killer'}
set1.issuperset(set2)
Copy the code
Output: True,


set1.pop()

Pop () removes and returns any element in the collection. An error is reported if the set is empty.

set1 = {'Dream, Killer'.'Python New Horizons'.'python-sun'}
set1.pop()
Copy the code
Output: ‘python – sun’


set.remove()

Remove () removes the specified element from the collection, or an error occurs if the element is not in the collection.

set1 = {'Dream, Killer'.'Python New Horizons'.'python-sun'}
set1.remove('Dream, Killer')
set1
Copy the code
Output: {‘Python new horizons ‘, ‘python-sun’}


set.symmetric_difference()

Symmetric_difference () returns the set of non-repeating elements of two sets, which is the complement of the two sets, in the same way as ^.

Sample 🅰 ️

set1 = {'Dream, Killer'.'Python New Horizons'.'python-sun'}
set2 = {'python'.'python-sun'.'Dream, Killer'}
set1.symmetric_difference(set2)
Copy the code
Output: {‘Python new horizons ‘, ‘Python ‘}


Sample 🅱 ️

set1 = {'Dream, Killer'.'Python New Horizons'.'python-sun'}
set2 = {'python'.'python-sun'.'Dream, Killer'}
set1 ^ set2
Copy the code
Output: {‘Python new horizons ‘, ‘Python ‘}
The result is the same as above


set.symmetric_difference_update()

Symmetric_difference_update (set2) removes elements in set1 that are the same in set2 and inserts different elements in the set2 set into set1. In simple terms, set1 is assigned the complement of set1 and set2.

Sample 🅰 ️

set1 = {'Dream, Killer'.'Python New Horizons'.'python-sun'}
set2 = {'python'.'python-sun'.'Dream, Killer'}
set1.symmetric_difference_update(set2)
set1
Copy the code
Output: {‘Python new horizons ‘, ‘Python ‘}


Sample 🅱 ️

set1 = {'Dream, Killer'.'Python New Horizons'.'python-sun'}
set2 = {'python'.'python-sun'.'Dream, Killer'}
set1 = set1 ^ set2
set1
Copy the code
Output: {‘Python new horizons ‘, ‘Python ‘}


set.union()

Union () returns the union of multiple sets. The same as the | role.

Sample 🅰 ️

set1 = {'Dream, Killer'.'Python New Horizons'.'python-sun'}
set2 = {'python'.'python-sun'.'Dream, Killer'}
set3 = {'ABC'}
set1.union(set2, set3)
Copy the code
Output: {‘ABC’, ‘Dream, Killer’, ‘Python new Horizon ‘, ‘Python ‘, ‘python-sun’}


Sample 🅱 ️

set1 = {'Dream, Killer'.'Python New Horizons'.'python-sun'}
set2 = {'python'.'python-sun'.'Dream, Killer'}
set3 = {'ABC'}
set1 | set2 | set3
Copy the code
Output: {‘ABC’, ‘Dream, Killer’, ‘Python new Horizon ‘, ‘Python ‘, ‘python-sun’}


set.update()

Update () updates the collection using itself and other unions.

Sample 🅰 ️

set1 = {'Dream, Killer'.'Python New Horizons'.'python-sun'}
set1.update([1.2.3])
set1
Copy the code
Output: {1, 2, 3, ‘Dream, Killer’, ‘Python ‘, ‘python-sun’}


Sample 🅱 ️

set1 = {'Dream, Killer'.'Python New Horizons'.'python-sun'}
set1 = set1 | set([1.2.3])
set1
Copy the code
Output: {1, 2, 3, ‘Dream, Killer’, ‘Python ‘, ‘python-sun’}

Use | also can achieve the same effect.



That’s all for this article, if it feels good.❤ Just like it before you go!! ❤

For those who are new to Python or want to learn Python, you can search “Python New Horizons” on wechat to communicate and learn with others. They are all beginners. Sometimes a simple question is stuck for a long time, but others may realize it at a touch. There are also a variety of learning resources waiting for you to receive oh ~.